Skip to content

[java] CheckResultSet: False positive with guava preconditions #5209

Open

Description

@hgschmie

Affects PMD Version: 7.5.0 (only version tested). Did work in 6.55.0

Rule: CheckResultSet

Please provide the rule name and a link to the rule documentation:
https://pmd.github.io/pmd/pmd_rules_java_bestpractices.html#checkresultset

Description:

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..

Code Sample demonstrating the issue:

https://github.com/hgschmie/pg-embedded/blob/master/src/main/java/de/softwareforge/testing/postgres/embedded/EmbeddedPostgres.java#L495

try (Connection c = createDefaultDataSource().getConnection();
        Statement s = c.createStatement();
        ResultSet rs = 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");
    return true;
}

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.

Running PMD through: Maven (as part of build)

[ This is a possible duplicate of #5031 ]

Activity

  1. added
    a:false-positive
    on Sep 12, 2024
  2. changed the title [-][java] False positive with guava preconditions[/-] [+][java] CheckResultSet: False positive with guava preconditions[/+] on Sep 12, 2024
  3. jsotuyod commented on Sep 12, 2024

    @jsotuyod
    Member

    @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.

  4. hgschmie commented on Sep 13, 2024

    @hgschmie
    ContributorAuthor

    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 ?

  5. jsotuyod commented on Sep 13, 2024

    @jsotuyod
    Member

    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…

  6. oowekyala commented on Sep 13, 2024

    @oowekyala
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positive

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions