Update rule CheckSkipResult

This commit is contained in:
Clément Fournier
2021-09-18 16:38:33 +02:00
parent cbe1f3bef8
commit 0c44c1b6bd
4 changed files with 25 additions and 48 deletions

View File

@ -191,7 +191,7 @@
<rule ref="category/java/errorprone.xml/BrokenNullCheck"/>
<!-- <rule ref="category/java/errorprone.xml/CallSuperFirst"/> -->
<!-- <rule ref="category/java/errorprone.xml/CallSuperLast"/> -->
<!-- <rule ref="category/java/errorprone.xml/CheckSkipResult"/> -->
<rule ref="category/java/errorprone.xml/CheckSkipResult"/>
<!-- <rule ref="category/java/errorprone.xml/ClassCastExceptionWithToArray"/> -->
<!-- <rule ref="category/java/errorprone.xml/CloneMethodMustBePublic"/> -->
<rule ref="category/java/errorprone.xml/CloneMethodMustImplementCloneable"/>

View File

@ -4,56 +4,28 @@
package net.sourceforge.pmd.lang.java.rule.errorprone;
import java.io.InputStream;
import net.sourceforge.pmd.lang.java.ast.ASTExpressionStatement;
import net.sourceforge.pmd.lang.java.ast.ASTMethodCall;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRulechainRule;
import net.sourceforge.pmd.lang.java.types.InvocationMatcher;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.java.ast.ASTExpression;
import net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression;
import net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix;
import net.sourceforge.pmd.lang.java.ast.ASTStatementExpression;
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence;
import net.sourceforge.pmd.lang.java.types.TypeTestUtil;
import net.sourceforge.pmd.lang.symboltable.NameOccurrence;
public class CheckSkipResultRule extends AbstractJavaRulechainRule {
public class CheckSkipResultRule extends AbstractJavaRule {
private static final InvocationMatcher SKIP_METHOD = InvocationMatcher.parse("java.io.InputStream#skip(_*)");
public CheckSkipResultRule() {
super(ASTMethodCall.class);
}
@Override
public Object visit(ASTVariableDeclaratorId node, Object data) {
if (!TypeTestUtil.isA(InputStream.class, node.getTypeNode())) {
return data;
public Object visit(ASTMethodCall call, Object data) {
if (SKIP_METHOD.matchesCall(call) && !isResultUsed(call)) {
addViolation(data, call);
}
for (NameOccurrence occ : node.getUsages()) {
JavaNameOccurrence jocc = (JavaNameOccurrence) occ;
NameOccurrence qualifier = jocc.getNameForWhichThisIsAQualifier();
if (qualifier != null && "skip".equals(qualifier.getImage())) {
Node loc = jocc.getLocation();
if (loc != null) {
ASTPrimaryExpression exp = loc.getFirstParentOfType(ASTPrimaryExpression.class);
while (exp != null) {
if (exp.getParent() instanceof ASTStatementExpression) {
// if exp is in a bare statement,
// the returned value is not used
addViolation(data, occ.getLocation());
break;
} else if (exp.getParent() instanceof ASTExpression
&& exp.getParent().getParent() instanceof ASTPrimaryPrefix) {
// if exp is enclosed in a pair of parenthesis
// let's have a look at the enclosing expression
// we'll see if it's in a bare statement
exp = exp.getFirstParentOfType(ASTPrimaryExpression.class);
} else {
// if exp is neither in a bare statement
// or between a pair of parentheses,
// it's in some other kind of statement
// or assignment so the returned value is used
break;
}
}
}
}
}
return data;
return null;
}
private boolean isResultUsed(ASTMethodCall call) {
return !(call.getParent() instanceof ASTExpressionStatement);
}
}

View File

@ -6,7 +6,6 @@ package net.sourceforge.pmd.lang.java.rule.errorprone;
import net.sourceforge.pmd.testframework.PmdRuleTst;
@org.junit.Ignore("Rule has not been updated yet")
public class CheckSkipResultTest extends PmdRuleTst {
// no additional unit tests
}

View File

@ -9,6 +9,7 @@
<expected-problems>1</expected-problems>
<code><![CDATA[
import java.io.FileInputStream;
import java.io.IOException;
public class Foo {
@ -26,6 +27,7 @@ public class Foo {
<expected-problems>1</expected-problems>
<code><![CDATA[
import java.io.FileInputStream;
import java.io.IOException;
public class Foo {
@ -43,6 +45,8 @@ public class Foo {
<expected-problems>0</expected-problems>
<code><![CDATA[
import java.io.FileInputStream;
import java.io.IOException;
import java.io.EOFException;
public class Foo {
@ -65,6 +69,7 @@ public class Foo {
<expected-problems>0</expected-problems>
<code><![CDATA[
import java.io.FileInputStream;
import java.io.IOException;
public class Foo {
@ -82,6 +87,7 @@ public class Foo {
<expected-problems>0</expected-problems>
<code><![CDATA[
import java.io.FileInputStream;
import java.io.IOException;
public class Foo {