Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive with dereference.of.nullable #6760

Closed
mahfouz72 opened this issue Aug 13, 2024 · 4 comments
Closed

False positive with dereference.of.nullable #6760

mahfouz72 opened this issue Aug 13, 2024 · 4 comments

Comments

@mahfouz72
Copy link

The Checker Framework is flagging a possible null dereference in the below code snippet, where lambdaParameters.peek() is being checked for null before it is dereferenced. This seems to be a false positive, as the code should not cause a null dereference.

Code:

while (lambdaParameters.peek() != null
        && ast.equals(lambdaParameters.peek().enclosingLambda)) {

    final Optional<LambdaParameterDetails> unusedLambdaParameter =
            Optional.ofNullable(lambdaParameters.peek())
                    .filter(parameter -> !parameter.isUsed())
                    .filter(parameter -> !"_".equals(parameter.getName()));

    ....
}

Output:

New surviving error(s) found:

  <checkerFrameworkError unstable="false">
    <fileName>src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/UnusedLambdaParameterShouldBeUnnamedCheck.java</fileName>
    <specifier>dereference.of.nullable</specifier>
    <message>dereference of possibly-null reference lambdaParameters.peek()</message>
    <lineContent>&amp;&amp; ast.equals(lambdaParameters.peek().enclosingLambda)) {</lineContent>
  </checkerFrameworkError>

Expectations:

This issue appears to be a false positive because lambdaParameters.peek() is explicitly checked for nullity before any dereferencing occurs. The error persists even though the code logic should prevent a null dereference.


Build tool: maven
checker version: 3.46.0

@smillst
Copy link
Member

smillst commented Aug 15, 2024

You've not given me enough of the code to reproduce this error. It could be that peek is not marked @SideEffectFree so lambdaParameters.peek() is unrefined.

@mahfouz72
Copy link
Author

You can find the file here

peek() is a method in the Deque class from Java collections

@smillst
Copy link
Member

smillst commented Aug 15, 2024

Thanks! I can reproduce this:

package typearginfer;

import java.util.ArrayDeque;
import java.util.Deque;

public class Issue6760 {
  private final Deque<LambdaParameterDetails> lambdaParameters = new ArrayDeque<>();
  void method(DetailAST ast) {
    while (lambdaParameters.peek() != null
        && ast.equals(lambdaParameters.peek().enclosingLambda)) {
    }
  }
  static class LambdaParameterDetails {
    private  DetailAST enclosingLambda = new DetailAST();
  }
  static class DetailAST{}
}

Yes, It seems like peek needs to be marked sideeffect free in the annotated jdk.

@mernst
Copy link
Member

mernst commented Aug 15, 2024

Fixed by typetools/jdk#219.

@mernst mernst closed this as completed Aug 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants