Merge branch 'bug-1484'

This commit is contained in:
Andreas Dangel
2016-05-21 11:45:09 +02:00
4 changed files with 50 additions and 0 deletions

View File

@ -1,4 +1,10 @@
/**
* Fix for an expression within an additive expression that was
* wrongly taken as a cast expression.
* Bug #1484
*
* Andreas Dangel 05/2016
*====================================================================
* Fix for Lambda expression with one variable
* Bug #1470
*
@ -1755,6 +1761,7 @@ void UnaryExpressionNotPlusMinus() #UnaryExpressionNotPlusMinus((jjtn000.getImag
{}
{
( "~" {jjtThis.setImage("~");} | "!" {jjtThis.setImage("!");} ) UnaryExpression()
| LOOKAHEAD("(" <IDENTIFIER> ")" "+") PostfixExpression()
| LOOKAHEAD( CastExpression() ) CastExpression()
| LOOKAHEAD("(" Type() ")" "(") CastExpression()
| PostfixExpression()

View File

@ -131,6 +131,21 @@ public class ParserCornersTest extends ParserTst {
parseJava18(c);
}
/**
* This triggered bug #1484 UnusedLocalVariable - false positive - parenthesis
* @throws Exception
*/
@Test
public void stringConcatentationShouldNotBeCast() throws Exception {
String code = "public class Test {\n" +
" public static void main(String[] args) {\n" +
" System.out.println(\"X\" + (args) + \"Y\");\n" +
" }\n" +
"}";
ASTCompilationUnit cu = parseJava18(code);
Assert.assertEquals(0, cu.findDescendantsOfType(ASTCastExpression.class).size());
}
private String readAsString(String resource) {
InputStream in = ParserCornersTest.class.getResourceAsStream(resource);
try {

View File

@ -342,6 +342,32 @@ public class Foo {
// do something
}
}
}
]]></code>
</test-code>
<test-code>
<description>#1484 UnusedLocalVariable - false positive - parenthesis</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("first");
list.add("second");
int notEmpty = 0;
for (String string : list) {
if (!string.isEmpty()) {
notEmpty++;
}
}
System.out.println(list.size() + " (" + (notEmpty) + " not empty)");
}
}
]]></code>
</test-code>

View File

@ -122,6 +122,8 @@ you'll need a java8 runtime environment.
* [#1422](https://sourceforge.net/p/pmd/bugs/1422/): UselessQualifiedThis: False positive with Java 8 Function
* java-unusedcode/UnusedFormalParameter:
* [#1456](https://sourceforge.net/p/pmd/bugs/1456/): UnusedFormalParameter should ignore overriding methods
* java-unusedcode/UnusedLocalVariable
* [#1484](https://sourceforge.net/p/pmd/bugs/1484/): UnusedLocalVariable - false positive - parenthesis
* java-unusedcode/UnusedPrivateField
* [#1428](https://sourceforge.net/p/pmd/bugs/1428/): False positive in UnusedPrivateField when local variable
hides member variable