-
Notifications
You must be signed in to change notification settings - Fork 688
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SONARJAVA-4546 Support @org.jspecify.annotations.NullMarked in existi…
…ng nullability checks up to Package Level (#4896)
- Loading branch information
1 parent
09ec082
commit 6ec169b
Showing
39 changed files
with
1,473 additions
and
34 deletions.
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
4 changes: 2 additions & 2 deletions
4
its/autoscan/src/test/resources/autoscan/diffs/diff_S1128.json
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"ruleKey": "S1128", | ||
"hasTruePositives": true, | ||
"falseNegatives": 33, | ||
"falseNegatives": 34, | ||
"falsePositives": 0 | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
its/autoscan/src/test/resources/autoscan/diffs/diff_S1168.json
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"ruleKey": "S1168", | ||
"hasTruePositives": true, | ||
"falseNegatives": 6, | ||
"falseNegatives": 24, | ||
"falsePositives": 0 | ||
} | ||
} |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"ruleKey": "S1172", | ||
"hasTruePositives": true, | ||
"falseNegatives": 17, | ||
"falseNegatives": 21, | ||
"falsePositives": 0 | ||
} |
4 changes: 2 additions & 2 deletions
4
its/autoscan/src/test/resources/autoscan/diffs/diff_S2447.json
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"ruleKey": "S2447", | ||
"hasTruePositives": true, | ||
"falseNegatives": 0, | ||
"falseNegatives": 4, | ||
"falsePositives": 0 | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
its/autoscan/src/test/resources/autoscan/diffs/diff_S2583.json
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"ruleKey": "S2583", | ||
"hasTruePositives": true, | ||
"falseNegatives": 12, | ||
"falseNegatives": 20, | ||
"falsePositives": 0 | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
its/autoscan/src/test/resources/autoscan/diffs/diff_S2589.json
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"ruleKey": "S2589", | ||
"hasTruePositives": true, | ||
"falseNegatives": 4, | ||
"falseNegatives": 6, | ||
"falsePositives": 0 | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
its/autoscan/src/test/resources/autoscan/diffs/diff_S2637.json
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"ruleKey": "S2637", | ||
"hasTruePositives": true, | ||
"falseNegatives": 21, | ||
"falseNegatives": 87, | ||
"falsePositives": 0 | ||
} | ||
} |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"ruleKey": "S2638", | ||
"hasTruePositives": true, | ||
"falseNegatives": 12, | ||
"falseNegatives": 14, | ||
"falsePositives": 0 | ||
} |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"ruleKey": "S2789", | ||
"hasTruePositives": true, | ||
"falseNegatives": 17, | ||
"falseNegatives": 43, | ||
"falsePositives": 0 | ||
} |
4 changes: 2 additions & 2 deletions
4
its/autoscan/src/test/resources/autoscan/diffs/diff_S3516.json
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"ruleKey": "S3516", | ||
"hasTruePositives": true, | ||
"falseNegatives": 6, | ||
"falseNegatives": 8, | ||
"falsePositives": 0 | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
its/autoscan/src/test/resources/autoscan/diffs/diff_S3958.json
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"ruleKey": "S3958", | ||
"hasTruePositives": true, | ||
"falseNegatives": 2, | ||
"falseNegatives": 4, | ||
"falsePositives": 0 | ||
} | ||
} |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"ruleKey": "S4682", | ||
"hasTruePositives": true, | ||
"falseNegatives": 2, | ||
"falseNegatives": 10, | ||
"falsePositives": 0 | ||
} |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"ruleKey": "S4738", | ||
"hasTruePositives": false, | ||
"falseNegatives": 46, | ||
"falseNegatives": 70, | ||
"falsePositives": 0 | ||
} |
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
83 changes: 83 additions & 0 deletions
83
...est-sources/default/src/main/java/checks/jspecify/BooleanMethodReturnCheckNullMarked.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,83 @@ | ||
package checks.jspecify; | ||
|
||
import java.util.stream.Stream; | ||
import javax.annotation.CheckForNull; | ||
import javax.annotation.Nullable; | ||
import org.jspecify.annotations.NullMarked; | ||
import org.jspecify.annotations.NullUnmarked; | ||
|
||
@NullMarked | ||
class BooleanMethodReturnCheckJSpecifySampleA { | ||
public Boolean myMethod() { | ||
return null; // Noncompliant {{Null is returned but a "Boolean" is expected.}} | ||
} | ||
|
||
public Boolean myOtherMethod() { | ||
return Boolean.TRUE; // Compliant | ||
} | ||
|
||
BooleanMethodReturnCheckJSpecifySampleA() { | ||
// constructor (with null return type) are not covered by the rule | ||
return; | ||
} | ||
|
||
@Nullable | ||
public Boolean foo() { | ||
return null; // Compliant | ||
} | ||
|
||
@NullUnmarked | ||
public Boolean bar() { | ||
return null; // Compliant | ||
} | ||
} | ||
|
||
@NullMarked | ||
class BooleanMethodReturnCheckJSpecifySampleB { | ||
private class Boolean { | ||
} | ||
|
||
public Boolean myMethodFailing() { | ||
return null; // Compliant | ||
} | ||
|
||
public java.lang.Boolean myOtherMethod() { | ||
class BooleanMethodReturnCheckSampleC { | ||
private java.lang.Boolean myInnerMethod() { | ||
return null; // Noncompliant {{Null is returned but a "Boolean" is expected.}} | ||
} | ||
private BooleanMethodReturnCheckSampleC foo() { | ||
return null; // Compliant | ||
} | ||
} | ||
return null; // Noncompliant {{Null is returned but a "Boolean" is expected.}} | ||
// ^^^^ | ||
} | ||
|
||
@CheckForNull | ||
public java.lang.Boolean myMethod2() { | ||
return null; // compliant method is annotated with @CheckForNull | ||
} | ||
} | ||
|
||
@NullMarked | ||
class BooleanMethodReturnCheckJSpecifySampleD { | ||
public Boolean foo() { | ||
class BooleanMethodReturnCheckSampleE { | ||
void bar() { | ||
return; | ||
} | ||
} | ||
Stream.of("A").forEach(a -> { | ||
return; // Compliant | ||
}); | ||
return true; | ||
} | ||
} | ||
|
||
class BooleanMethodReturnCheckJSpecifySampleE { | ||
@NullMarked | ||
public Boolean myMethod() { | ||
return null; // Noncompliant {{Null is returned but a "Boolean" is expected.}} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...st-sources/default/src/main/java/checks/jspecify/ChangeMethodContractCheckNullMarked.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,22 @@ | ||
package checks.jspecify; | ||
|
||
import org.jspecify.annotations.NullMarked; | ||
import org.jspecify.annotations.NullUnmarked; | ||
|
||
@NullMarked | ||
class ChangeMethodContractCheck { | ||
|
||
@interface MyAnnotation {} | ||
|
||
@NullUnmarked | ||
String annotatedUnmarked(Object a) { return null; } | ||
} | ||
|
||
class ChangeMethodContractCheck_B extends ChangeMethodContractCheck { | ||
|
||
@NullMarked | ||
@Override | ||
String annotatedUnmarked(Object a) { return null; } // Noncompliant {{Fix the incompatibility of the annotation @NullMarked to honor @NullUnmarked of the overridden method.}} | ||
|
||
} | ||
|
23 changes: 23 additions & 0 deletions
23
...s/default/src/main/java/checks/jspecify/EqualsParametersMarkedNonNullCheckNullMarked.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,23 @@ | ||
package checks.jspecify; | ||
|
||
import org.jspecify.annotations.NullMarked; | ||
import org.jspecify.annotations.NullUnmarked; | ||
|
||
@NullMarked | ||
class EqualsParametersMarkedNonNullCheckSampleA { | ||
|
||
// @NullUnmarked not applicable to parameter | ||
public boolean equals(Object obj) { // Compliant | ||
return true; | ||
} | ||
} | ||
|
||
@NullMarked | ||
class EqualsParametersMarkedNonNullCheckSampleB { | ||
|
||
@NullUnmarked | ||
// @NullUnmarked not applicable to parameter | ||
public boolean equals(Object obj) { // Compliant | ||
return true; | ||
} | ||
} |
Oops, something went wrong.