diff --git a/pmd-java/etc/grammar/Java.jjt b/pmd-java/etc/grammar/Java.jjt index 0962322888..7fc17b1edf 100644 --- a/pmd-java/etc/grammar/Java.jjt +++ b/pmd-java/etc/grammar/Java.jjt @@ -1754,11 +1754,23 @@ void UnaryExpressionNotPlusMinus() #UnaryExpressionNotPlusMinus((jjtn000.getImag * meaning we can't be explicit as to what can be casted depending on the cast type (primitive or otherwise) */ | LOOKAHEAD("(" (Annotation())* PrimitiveType() ")") CastExpression() -| LOOKAHEAD("(" (Annotation())* Type() ( "&" ReferenceType() )* ")" UnaryExpressionNotPlusMinus()) CastExpression() -| PostfixExpression() +| LOOKAHEAD("(" (Annotation())* Type() ( "&" ReferenceType() )* ")" UnaryExprNotPmStart()) CastExpression() +| PostfixExpression() // this may be a parenthesized expr, which is why we have lookaheads | SwitchExpression() } + +private void UnaryExprNotPmStart() #void: +{} +{ + // Condensed FIRST set of UnaryExpressionNotPlusMinus + // Avoid looking ahead for a whole UnaryExpressionNotPlusMinus, but just for a token + + "~" | "!" | "(" | "switch" | "new" | "this" | "super" | Literal() | "@" + | + | "void" | PrimitiveType() +} + void PostfixExpression() #PostfixExpression((jjtn000.getImage() != null)): {} { diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/ParserCornersTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/ParserCornersTest.java index 4af25e38db..eab5faaaf4 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/ParserCornersTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/ParserCornersTest.java @@ -177,6 +177,11 @@ public class ParserCornersTest { java8.parseResource("GitHubBug207.java"); } + @Test + public void testLambda2783() { + java8.parseResource("LambdaBug2783.java"); + } + @Test public void testGitHubBug2767() { // PMD fails to parse an initializer block. diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/LambdaBug2783.java b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/LambdaBug2783.java new file mode 100644 index 0000000000..425e262c28 --- /dev/null +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/LambdaBug2783.java @@ -0,0 +1,31 @@ +/* + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +import java.util.List; + +public class LambdaBug2783 { + // https://github.com/pmd/pmd/issues/2783 + + public Spec test() { + // cast, block body (the failing case) + Spec result = (Spec) (a, b) -> { + return a.toArray(String[]::new); + }; + // no cast, block body + result = (a, b) -> { + return a.toArray(String[]::new); + }; + // cast, expression body + result = (Spec) (a, b) -> a.toArray(String[]::new); + + // return position? + return (Spec) (a, b) -> { + return a.toArray(String[]::new); + }; + } + + interface Spec { + String[] process(List var1, List var2); + } +}