forked from typetools/checker-framework
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Instanceof pattern variable nullness annotation checks (#1062)
- Loading branch information
1 parent
4935c5d
commit 730914a
Showing
3 changed files
with
75 additions
and
3 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
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,53 @@ | ||
// @below-java14-jdk-skip-test | ||
import org.checkerframework.checker.nullness.qual.NonNull; | ||
import org.checkerframework.checker.nullness.qual.Nullable; | ||
|
||
public class NullnessInstanceOf { | ||
|
||
public void testClassicInstanceOfNullable(Object x) { | ||
// :: error: (instanceof.nullable) | ||
if (x instanceof @Nullable String) { | ||
System.out.println("Nullable String instanceof check."); | ||
} | ||
} | ||
|
||
public void testClassicInstanceOfNonNull(Object x) { | ||
// :: warning: (instanceof.nonnull.redundant) | ||
if (x instanceof @NonNull Number) { | ||
System.out.println("NonNull Number instanceof check."); | ||
} | ||
} | ||
|
||
public void testPatternVariableNullable(Object x) { | ||
// :: error: (instanceof.nullable) | ||
if (x instanceof @Nullable String n) { | ||
System.out.println("Length of String: " + n.length()); | ||
} | ||
} | ||
|
||
public void testPatternVariableNonNull(Object x) { | ||
// :: warning: (instanceof.nonnull.redundant) | ||
if (x instanceof @NonNull Number nn) { | ||
System.out.println("Number's hashCode: " + nn.hashCode()); | ||
} | ||
} | ||
|
||
public void testUnannotatedClassic(Object x) { | ||
if (x instanceof String) { | ||
System.out.println("Unannotated String instanceof check."); | ||
} | ||
} | ||
|
||
public void testUnannotatedPatternVariable(Object x) { | ||
if (x instanceof String unannotatedString) { | ||
System.out.println("Unannotated String length: " + unannotatedString.length()); | ||
} | ||
} | ||
|
||
public void testUnusedPatternVariable(Object x) { | ||
// :: error: (instanceof.nullable) | ||
if (x instanceof @Nullable String unusedString) {} | ||
// :: warning: (instanceof.nonnull.redundant) | ||
if (x instanceof @NonNull Number unusedNumber) {} | ||
} | ||
} |
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