• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pmd / pmd / 990

19 Nov 2026 03:39PM UTC coverage: 80.275% (+0.007%) from 80.268%
990

push

github

web-flow
[java] Fix #7070: CheckResultSet under unrelated if and while conditions (#7192)

20803 of 26894 branches covered (77.35%)

Branch coverage included in aggregate %.

18 of 19 new or added lines in 1 file covered. (94.74%)

44331 of 54245 relevant lines covered (81.72%)

0.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.39
/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/CheckResultSetRule.java
1
/*
2
 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3
 */
4

5
package net.sourceforge.pmd.lang.java.rule.bestpractices;
6

7
import static net.sourceforge.pmd.util.CollectionUtil.setOf;
8

9
import java.sql.ResultSet;
10
import java.util.List;
11
import java.util.Set;
12

13
import net.sourceforge.pmd.lang.java.ast.ASTAssignableExpr;
14
import net.sourceforge.pmd.lang.java.ast.ASTConditionalExpression;
15
import net.sourceforge.pmd.lang.java.ast.ASTExpression;
16
import net.sourceforge.pmd.lang.java.ast.ASTIfStatement;
17
import net.sourceforge.pmd.lang.java.ast.ASTMethodCall;
18
import net.sourceforge.pmd.lang.java.ast.ASTReturnStatement;
19
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator;
20
import net.sourceforge.pmd.lang.java.ast.ASTVariableId;
21
import net.sourceforge.pmd.lang.java.ast.ASTWhileStatement;
22
import net.sourceforge.pmd.lang.java.ast.JavaNode;
23
import net.sourceforge.pmd.lang.java.ast.ReturnScopeNode;
24
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
25
import net.sourceforge.pmd.lang.java.symbols.JVariableSymbol;
26
import net.sourceforge.pmd.lang.java.types.TypeTestUtil;
27
import net.sourceforge.pmd.reporting.RuleContext;
28

29
/**
30
 * Rule that verifies, that the return values of next(), first(), last(), etc.
31
 * calls to a java.sql.ResultSet are actually verified.
32
 */
33
public class CheckResultSetRule extends AbstractJavaRule {
1 ✔
34

35
    private static final Set<String> METHODS = setOf("next", "previous", "last", "first");
1 ✔
36

37
    @Override
38
    public Object visit(ASTWhileStatement node, Object data) {
39
        // Keep skipping checked navigation conditions and their bodies to avoid existing false positives.
40
        if (containsNavigationCall(node.getCondition())) {
1 ✔
41
            return null;
1 ✔
42
        }
43
        // A cached navigation result used as the loop condition also checks the navigation call.
44
        if (isCheckedNavigationVariable(node.getCondition())) {
1 ✔
45
            return null;
1 ✔
46
        }
47
        return super.visit(node, data);
1 ✔
48
    }
49

50
    // Intentionally broad: if any variable in the condition was declared with a navigation
51
    // call, the whole body is skipped, so `boolean h = rs.next(); while (h && cond) { rs.next(); }`
52
    // is not reported.
53
    private boolean isCheckedNavigationVariable(ASTExpression condition) {
54
        return condition.descendantsOrSelf().filterIs(ASTAssignableExpr.ASTNamedReferenceExpr.class).any(reference -> {
1 ✔
55
            JVariableSymbol symbol = reference.getReferencedSym();
1 ✔
56
            if (symbol == null) {
1 !
NEW
57
                return false;
×
58
            }
59
            ASTVariableId variableId = symbol.tryGetNode();
1 ✔
60
            if (variableId == null) {
1 ✔
61
                return false;
1 ✔
62
            }
63
            ASTExpression initializer = variableId.getInitializer();
1 ✔
64
            return initializer != null
1 ✔
65
                && initializer.descendantsOrSelf().filterIs(ASTMethodCall.class).any(this::isResultSetMethod);
1 ✔
66
        });
67
    }
68

69
    private boolean containsNavigationCall(ASTExpression condition) {
70
        return condition.descendantsOrSelf().filterIs(ASTMethodCall.class).any(this::isResultSetMethod);
1 ✔
71
    }
72

73
    @Override
74
    public Object visit(ASTReturnStatement node, Object data) {
75
        return null;
1 ✔
76
    }
77

78
    @Override
79
    public Object visit(ASTIfStatement node, Object data) {
80
        // Keep skipping checked navigation conditions and their bodies to avoid existing false positives.
81
        if (containsNavigationCall(node.getCondition())) {
1 ✔
82
            return null;
1 ✔
83
        }
84
        return super.visit(node, data);
1 ✔
85
    }
86

87
    @Override
88
    public Object visit(ASTMethodCall node, Object data) {
89
        RuleContext ctx = (RuleContext) data;
1 ✔
90

91
        if (isResultSetMethod(node) && !isInConditionOfTernary(node) && !isCheckedIndirectly(node)) {
1 ✔
92
            ctx.addViolation(node);
1 ✔
93
        }
94
        return super.visit(node, data);
1 ✔
95
    }
96

97
    private boolean isResultSetMethod(ASTMethodCall node) {
98
        return METHODS.contains(node.getMethodName())
1 ✔
99
            && TypeTestUtil.isDeclaredInClass(ResultSet.class, node.getMethodType());
1 ✔
100
    }
101

102
    private boolean isInConditionOfTernary(ASTMethodCall node) {
103
        JavaNode child = node;
1 ✔
104
        for (JavaNode parent : node.ancestors().takeWhile(n -> !(n instanceof ReturnScopeNode))) {
1 ✔
105
            if (parent instanceof ASTConditionalExpression
1 ✔
106
                && ((ASTConditionalExpression) parent).getCondition() == child) {
1 !
107
                return true;
1 ✔
108
            }
109
            child = parent;
1 ✔
110
        }
1 ✔
111
        return false;
1 ✔
112
    }
113

114
    private boolean isCheckedIndirectly(ASTMethodCall node) {
115
        final ASTVariableDeclarator variableDeclarator = node.ancestors()
1 ✔
116
                .takeWhile(n -> !(n instanceof ReturnScopeNode))
1 ✔
117
                .first(ASTVariableDeclarator.class);
1 ✔
118

119
        if (variableDeclarator == null) {
1 ✔
120
            return false;
1 ✔
121
        }
122

123
        final List<ASTAssignableExpr.ASTNamedReferenceExpr> usages = variableDeclarator.getVarId().getLocalUsages();
1 ✔
124
        //check that the result is used and its first usage is not overwriting the result
125
        return !usages.isEmpty() && usages.get(0).getAccessType() == ASTAssignableExpr.AccessType.READ;
1 ✔
126
    }
127
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc