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 about the rule NP_EQUALS_SHOULD_HANDLE_NULL_ARGUMENT #2558

Open
LynnBroe opened this issue Sep 11, 2023 · 1 comment
Open

Comments

@LynnBroe
Copy link

LynnBroe commented Sep 11, 2023

Version: 4.7.3

I found a false positive about the rule NP_EQUALS_SHOULD_HANDLE_NULL_ARGUMENT.

In the example below, SpotBugs reports no warning. The equals method takes a parameter that is marked as @javax.annotation.Nonnull . So it obeys this rule correctly.

public class Test {
    public boolean equals(@javax.annotation.Nonnull Object a) {
        return a.hashCode() == this.hashCode();
    }
}

However, in the example below, SpotBugs reports a warning at line 3 if we use @org.netbeans.api.annotations.common.NonNull instead. Actually, @javax.annotation.Nonnull and @org.netbeans.api.annotations.common.NonNull are equivalent. So, this exmaple does not violate the rule NP_EQUALS_SHOULD_HANDLE_NULL_ARGUMENT and the warning is a false positive.

public class Test {
    public boolean equals(@org.netbeans.api.annotations.common.NonNull Object a) { //report a warning, but it is an FP
        return a.hashCode() == this.hashCode();
    }
}

I use the cmd spotbugs -textui -low -xml -output= report.xml -auxclasspath org.eclipse.jdt.annotation-2.2.700.jar Test.class

@LynnBroe
Copy link
Author

LynnBroe commented Sep 12, 2023

I find that @lombok.NonNull also has the same problem. Please see the code example below:

public class Test {
    public boolean equals(@lombok.NonNull Object a) { //report a warning, but it is an FP
        return a.hashCode() == this.hashCode();
    }
}

gtoison added a commit to gtoison/spotbugs that referenced this issue Nov 10, 2023
hazendaz pushed a commit that referenced this issue Mar 1, 2024
* test: reproducer for issue #2558

* Recognize more nullability annotations

* fix: added back check for Eclipse's NonNullByDefault

* fix: made eclipseNonNullByDefault a field again since used externally

* fix: with the other changes the smoke tests flag that field as NonNull

LogicalLocation.properties should be annotated NonNull

* doc: added changelog entry

* fix: updated the comment now that the false positive is fixed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant