-
Notifications
You must be signed in to change notification settings - Fork 578
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE
false positive when code is …
…formatted on different lines (#2874) * test: sample for issue #782 * fix: keep looking for an assertion even if we move to a different line * test: updated bug sample comments @nullable is @nonnull(when = When.UNKNOWN) so it seems OK that we're not reporting a bug (i.e. some of the cases do not seem to be false negatives) * docs: added changelog entry
- Loading branch information
Showing
4 changed files
with
119 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
spotbugs-tests/src/test/java/edu/umd/cs/findbugs/detect/Issue782Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package edu.umd.cs.findbugs.detect; | ||
|
||
import static edu.umd.cs.findbugs.test.CountMatcher.containsExactly; | ||
import static org.hamcrest.MatcherAssert.*; | ||
|
||
import edu.umd.cs.findbugs.AbstractIntegrationTest; | ||
import edu.umd.cs.findbugs.test.matcher.BugInstanceMatcher; | ||
import edu.umd.cs.findbugs.test.matcher.BugInstanceMatcherBuilder; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public class Issue782Test extends AbstractIntegrationTest { | ||
|
||
@Test | ||
void testIssue() { | ||
performAnalysis( | ||
"ghIssues/Issue782.class", | ||
"ghIssues/Issue782$MyNullable.class"); | ||
|
||
assertBugCount("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE", 1); | ||
|
||
assertBugAtLine("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE", 27); | ||
} | ||
|
||
private void assertBugCount(String type, int expectedCount) { | ||
BugInstanceMatcher bugMatcher = new BugInstanceMatcherBuilder() | ||
.bugType(type) | ||
.build(); | ||
|
||
assertThat(getBugCollection(), containsExactly(expectedCount, bugMatcher)); | ||
} | ||
|
||
private void assertBugAtLine(String type, int line) { | ||
BugInstanceMatcher bugMatcher = new BugInstanceMatcherBuilder() | ||
.bugType(type) | ||
.atLine(line) | ||
.build(); | ||
|
||
assertThat(getBugCollection(), containsExactly(1, bugMatcher)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package ghIssues; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
|
||
import javax.annotation.Nullable; | ||
import javax.annotation.meta.TypeQualifierNickname; | ||
import javax.annotation.meta.When; | ||
|
||
/** | ||
* False negatives and positives of NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE. | ||
* Results surprisingly depends on the actual formatting of the source code. | ||
*/ | ||
public class Issue782 { | ||
|
||
/** | ||
* Invoking get() which is annotated with a custom Nullable. | ||
*/ | ||
public void func() { | ||
Object a = get(); // true positive | ||
Object b = checkNotNull(get()); // true negative | ||
Object c = checkNotNull( | ||
get()); // false positive (fixed by the change) | ||
System.out.println(a.toString()); | ||
System.out.println(b.toString()); | ||
System.out.println(c.toString()); | ||
} | ||
|
||
/** | ||
* Invoking get2() which is annotated with a javax Nullable, verify return value with Guava checkNotNull. | ||
*/ | ||
public void func2() { | ||
Object a = get2(); // @Nullable is @Nonnull(when = When.UNKNOWN) so it seems OK that we're not reporting a bug | ||
Object b = checkNotNull(get2()); // true negative | ||
Object c = checkNotNull( | ||
get2()); // true negative | ||
System.out.println(a.toString()); | ||
System.out.println(b.toString()); | ||
System.out.println(c.toString()); | ||
} | ||
|
||
/** | ||
* Invoking get2() which is annotated with a javax Nullable, verify return value with requireNonNull. | ||
*/ | ||
public void func3() { | ||
Object a = get2(); // @Nullable is @Nonnull(when = When.UNKNOWN) so it seems OK that we're not reporting a bug | ||
Object b = requireNonNull(get2()); // true negative | ||
Object c = requireNonNull( | ||
get2()); // true negative | ||
System.out.println(a.toString()); | ||
System.out.println(b.toString()); | ||
System.out.println(c.toString()); | ||
} | ||
|
||
@MyNullable | ||
public Object get() { | ||
return null; | ||
} | ||
|
||
@Nullable | ||
public Object get2() { | ||
return null; | ||
} | ||
|
||
@TypeQualifierNickname | ||
@javax.annotation.Nonnull(when = When.MAYBE) | ||
@Retention(RetentionPolicy.CLASS) | ||
public @interface MyNullable { | ||
} | ||
} |