Skip to content

[java] CheckResultSet: False positive for rs.next() ? x : y #7063

Closed

Description

@fabian-hahn

Affects PMD Version: 7.27

Rule: CheckResultSet
https://docs.pmd-code.org/pmd-doc-7.27.0/pmd_rules_java_bestpractices.html#checkresultset

Description:

Calling java.sql.ResultSet#next() in a ternary condition flags an error which it probably shouldn't.

Code Sample demonstrating the issue:

    int getCount(int id) throws SQLException {
        ResultSetHandler<Integer> countHandler =
            (rs) -> rs.next()? rs.getInt(1) : 0;

        String sql = """
            select count(*)
              from TABLE
             where ID = ?
        """;
        return runner.query(sql, countHandler, id);
    }

Expected outcome:

PMD reports a violation at line 3, but that's wrong. That's a false positive.

Running PMD through: Maven

Activity

  1. added
    a:false-positive
    on Sep 12, 2026
  2. UncleOwen commented on Sep 12, 2026

    @UncleOwen
    Member

    Yes, that rule needs an overhaul. See also #5031 and #5209

  3. changed the title [-][java] rs.next() ? x : y fires CheckResultSet[/-] [+][java] ResultSet.next() ? x : y fires CheckResultSet[/+] on Sep 12, 2026
  4. UncleOwen commented on Sep 12, 2026

    @UncleOwen
    Member

    Note that this rule is a more complicated case than UnusedReturnValue. It's not really about rs.next() either. It's about what you're allowed to do afterwards.

    There's one set of functions (rs.next(), rs.previous(), rs.first(), rs.last(), rs.absolute(), rs.relative()) that move the cursor. The return values of these functions tell you whether you're allowed to use rs.getRow(), rs.getInt(), etc. at the new position. If the return value is true you are allowed, if it's false (or before moving the cursor at all), you're not allowed. That's what this rule is (or at least should be) about.

    (There's also rs.beforeFirst() and rs.beforeLast() that always move the cursor into an invalid position, functions that query the current position, etc. - but before we think about those we should get the basics I outlined above correct.)

  5. fabian-hahn commented on Sep 12, 2026

    @fabian-hahn
    Author

    Thanks, @UncleOwen for the fast reply.

    Hmm, I do not completely understand.

    int c = 0;
    if (rs.next()) c = rs.getInt();     // A
    
    c = rs.next() ? rs.getInt() : 0;    // B

    A works just fine. B flags a violation. Shouldn't they behave the same?

  6. UncleOwen commented on Sep 12, 2026

    @UncleOwen
    Member

    Yes, they should behave the same. What I'm saying is, that in my opinion that rule has deeper issues than that.

  7. changed the title [-][java] ResultSet.next() ? x : y fires CheckResultSet[/-] [+][java] CheckResultSet: FalsePositive for rs.next() ? x : y[/+] on Sep 13, 2026
  8. changed the title [-][java] CheckResultSet: FalsePositive for rs.next() ? x : y[/-] [+][java] CheckResultSet: False positive for rs.next() ? x : y[/+] on Sep 13, 2026
  9. added a commit that references this issue on Sep 25, 2026
    ed473dc
  10. added 2 commits that reference this issue on Oct 4, 2026
    15e0f88
    fe0dffd
  11. added this to the 7.29.0 milestone on Oct 8, 2026
  12. added a commit that references this issue on Oct 8, 2026
    cfee948
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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions