You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[java] CheckResultSet: False positive with guava preconditions #5209
Calling ResultSet#next() in a Guava precondition reports Rule:CheckResultSet Priority:3 Always check the return of one of the navigation method (next,previous,first,last) of a ResultSet..
try (Connectionc = createDefaultDataSource().getConnection();
Statements = c.createStatement();
ResultSetrs = s.executeQuery("SELECT 1")) {
checkState(rs.next(), "expecting single row");
checkState(rs.getInt(1) == 1, "expecting 1 as result");
checkState(!rs.next(), "expecting single row");
returntrue;
}
Expected outcome:
code is correct.
[WARNING] PMD Failure: de.softwareforge.testing.postgres.embedded.EmbeddedPostgres:498 Rule:CheckResultSet Priority:3 Always check the return of one of the navigation method (next,previous,first,last) of a ResultSet..
[WARNING] PMD Failure: de.softwareforge.testing.postgres.embedded.EmbeddedPostgres:500 Rule:CheckResultSet Priority:3 Always check the return of one of the navigation method (next,previous,first,last) of a ResultSet..
498 is the first checkState(rs.next(), 500 is the second.
changed the title [-][java] False positive with guava preconditions[/-][+][java] CheckResultSet: False positive with guava preconditions[/+]on Sep 12, 2024
The short answer is, because PMD 7 is far more powerful and capable than PMD 6 ever was…
Prior to PMD 7, PMD's Java grammar was very cumbersome and somewhat distant to the Java Language Specification, and our symbol table and type resolution capabilities were limited. These factors combined into making a lot of rules really struggle to properly capture all scenarios. In this particular rule's case, the rule would not consider ResultSet instances that were declared in a try-with-resources (as is your case). So it's not that PMD 6.55 properly handled this scenario, it was on the contrary, completely ignoring it, and would not have flagged even if you had called rs.next() outside of checkState.
Maybe it's worth extending the rule to allow ignoring calls being passed to methods matching a given signature…this would support Guava, Apache Commons Lang's Validate, and any other there may be…
I think probably it's enough to whitelist any call that is used as a method argument. If it's passed as a method argument then it's obviously not ignored, whatever the method is doing. We don't really need to match specific signatures, because that's bound to become unmanageable over time.
Activity
[-][java] False positive with guava preconditions[/-][+][java] CheckResultSet: False positive with guava preconditions[/+]jsotuyod commentedon Sep 12, 2024
@hgschmie no, this is not a duplicate of that issue. This is something else entirely.
The rule expects the call to be within a return statement / if / while. #5031 fails because do-while is not currently considered.
This case on the other hand fails because detecting this would require PMD to know the semantics of Guava's
Preconditions.checkState.hgschmie commentedon Sep 13, 2024
Thanks for looking into this so quickly. So is this basically a "works as expected" because it does not adhere to the pattern that pmd understands.
And why did it not flag a problem in 6.55 ?
jsotuyod commentedon Sep 13, 2024
The short answer is, because PMD 7 is far more powerful and capable than PMD 6 ever was…
Prior to PMD 7, PMD's Java grammar was very cumbersome and somewhat distant to the Java Language Specification, and our symbol table and type resolution capabilities were limited. These factors combined into making a lot of rules really struggle to properly capture all scenarios. In this particular rule's case, the rule would not consider
ResultSetinstances that were declared in a try-with-resources (as is your case). So it's not that PMD 6.55 properly handled this scenario, it was on the contrary, completely ignoring it, and would not have flagged even if you had calledrs.next()outside ofcheckState.Maybe it's worth extending the rule to allow ignoring calls being passed to methods matching a given signature…this would support Guava, Apache Commons Lang's
Validate, and any other there may be…oowekyala commentedon Sep 13, 2024
I think probably it's enough to whitelist any call that is used as a method argument. If it's passed as a method argument then it's obviously not ignored, whatever the method is doing. We don't really need to match specific signatures, because that's bound to become unmanageable over time.