Merge branch 'issue-219' into pmd/5.5.x

This commit is contained in:
Andreas Dangel
2017-02-06 20:02:10 +01:00
3 changed files with 23 additions and 5 deletions

View File

@ -6,8 +6,8 @@ package net.sourceforge.pmd.lang.java.rule.design;
import java.util.List;
import java.util.Map;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.java.ast.ASTAssertStatement;
import net.sourceforge.pmd.lang.java.ast.ASTBlock;
import net.sourceforge.pmd.lang.java.ast.ASTBlockStatement;
import net.sourceforge.pmd.lang.java.ast.ASTExpression;
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
@ -94,22 +94,22 @@ public class UnnecessaryLocalBeforeReturnRule extends AbstractJavaRule {
* @return
*/
private boolean hasAssertStatement(VariableNameDeclaration variableDeclaration, ASTReturnStatement rtn) {
boolean hasAssert = false;
ASTBlockStatement blockStatement = variableDeclaration.getAccessNodeParent().getFirstParentOfType(ASTBlockStatement.class);
int startIndex = blockStatement.jjtGetChildIndex() + 1;
int endIndex = rtn.getFirstParentOfType(ASTBlockStatement.class).jjtGetChildIndex();
ASTBlock block = (ASTBlock)blockStatement.jjtGetParent();
Node block = blockStatement.jjtGetParent();
for (int i = startIndex; i < endIndex; i++) {
List<ASTAssertStatement> asserts = block.jjtGetChild(i).findDescendantsOfType(ASTAssertStatement.class);
for (ASTAssertStatement assertStatement : asserts) {
List<ASTName> names = assertStatement.findDescendantsOfType(ASTName.class);
for (ASTName n : names) {
if (n.hasImageEqualTo(variableDeclaration.getName())) {
hasAssert = true;
return true;
}
}
}
}
return hasAssert;
return false;
}
}

View File

@ -75,6 +75,22 @@ public class Foo {
int res = 2; assert res>=0;
return res;
}
}
]]></code>
</test-code>
<test-code>
<description>#219 ClassCastException in switch case with local variable returned</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class Foo {
public int m(int val) {
switch (vl) {
default:
int i = 0;
return i;
}
}
}
]]></code>
</test-code>