-
Notifications
You must be signed in to change notification settings - Fork 29
Add instanceof.unsafe lint option
#1209
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
Changes from all commits
edf711e
2d05deb
f87d228
e57ef74
30220ed
33c692b
a12fb71
d1f6652
ff6445d
93650de
f7b35f6
5bbdb1d
f112f83
f4df421
c98e9c2
75221b3
fc6acdd
e50a7d9
727e532
651af78
4bbb1b3
6e44d24
531531b
92cdf81
52b175e
b1a2a8d
a6d2165
1f0c852
c16a0fa
c35d301
47a87f0
ff83cb7
97807bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| /* | ||
| * @test | ||
| * @summary Test case for instanceof lint option: -Alint=-instanceof | ||
| * @requires jdk.version >= 17 | ||
| * @compile -processor org.checkerframework.checker.tainting.TaintingChecker InstanceofLintOptionEnabled.java -Alint=-instanceof | ||
| * @compile -processor org.checkerframework.checker.tainting.TaintingChecker InstanceofLintOptionEnabled.java -Alint=-instanceof:unsafe | ||
| * @compile -processor org.checkerframework.checker.tainting.TaintingChecker InstanceofLintOptionEnabled.java -Alint=instanceof,-instanceof:unsafe | ||
| */ | ||
|
|
||
| import org.checkerframework.checker.tainting.qual.Untainted; | ||
|
|
||
| public class InstanceofLintOptionDisabled { | ||
| void bar(Object o) { | ||
| if (o instanceof @Untainted String s) {} | ||
| if (o instanceof @Untainted String) {} | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| /* | ||
| * @test | ||
| * Test case for instanceof lint option: -Alint=instanceof | ||
| * @requires jdk.version >= 17 | ||
| * @compile/ref=InstanceofLintOptionEnabled.out -XDrawDiagnostics -processor org.checkerframework.checker.tainting.TaintingChecker InstanceofLintOptionEnabled.java | ||
| * @compile/ref=InstanceofLintOptionEnabled.out -XDrawDiagnostics -processor org.checkerframework.checker.tainting.TaintingChecker InstanceofLintOptionEnabled.java -Alint=instanceof | ||
| * @compile/ref=InstanceofLintOptionEnabled.out -XDrawDiagnostics -processor org.checkerframework.checker.tainting.TaintingChecker InstanceofLintOptionEnabled.java -Alint=instanceof:unsafe | ||
| * @compile/ref=InstanceofLintOptionEnabled.out -XDrawDiagnostics -processor org.checkerframework.checker.tainting.TaintingChecker InstanceofLintOptionEnabled.java -Alint=-instanceof,instanceof:unsafe | ||
| */ | ||
|
|
||
| import org.checkerframework.checker.tainting.qual.Untainted; | ||
|
|
||
| public class InstanceofLintOptionEnabled { | ||
| void bar(Object o) { | ||
| if (o instanceof @Untainted String s) {} | ||
| if (o instanceof @Untainted String) {} | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| InstanceofLintOptionEnabled.java:15:15: compiler.warn.proc.messager: [instanceof.pattern.unsafe] instanceof pattern binding '@Tainted Object' to '@Untainted | ||
| String s' cannot be statically verified. | ||
| InstanceofLintOptionEnabled.java:16:15: compiler.warn.proc.messager: [instanceof.unsafe] '@Tainted Object' instanceof '@Untainted String' cannot be statically verified. | ||
| 2 warnings |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -289,6 +289,15 @@ public class BaseTypeVisitor<Factory extends GenericAnnotatedTypeFactory<?, ?, ? | |
| /** True if "-AcheckEnclosingExpr" was passed on the command line. */ | ||
| private final boolean checkEnclosingExpr; | ||
|
|
||
| /** True if "-Alint=cast:redundant" was passed on the command line. */ | ||
| private final boolean lintCastRedundantEnabled; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm wondering whether we should call all these fields "xxxDisabled" and not have the negation in every check. Does it make the code easier to read either way?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lint option sounds I think it would be nicer to keep these as |
||
|
|
||
| /** True unless "-Alint=-cast:unsafe" was passed on the command line. */ | ||
| private final boolean lintCastUnsafeEnabled; | ||
|
|
||
| /** True unless "-Alint=-instanceof:unsafe" was passed on the command line. */ | ||
| private final boolean lintInstanceofUnsafeEnabled; | ||
|
|
||
| /** The tree of the enclosing method that is currently being visited, if any. */ | ||
| protected @Nullable MethodTree methodTree = null; | ||
|
|
||
|
|
@@ -348,6 +357,9 @@ protected BaseTypeVisitor(BaseTypeChecker checker, @Nullable Factory typeFactory | |
| checker.hasOption("assumeDeterministic") || checker.hasOption("assumePure"); | ||
| assumePureGetters = checker.hasOption("assumePureGetters"); | ||
| checkCastElementType = checker.hasOption("checkCastElementType"); | ||
| lintCastRedundantEnabled = checker.getLintOption("cast:redundant", false); | ||
| lintCastUnsafeEnabled = checker.getLintOption("cast:unsafe", true); | ||
| lintInstanceofUnsafeEnabled = checker.getLintOption("instanceof:unsafe", true); | ||
| } | ||
|
|
||
| /** An array containing just {@code BaseTypeChecker.class}. */ | ||
|
|
@@ -2713,9 +2725,11 @@ public Void visitNewArray(NewArrayTree tree, Void p) { | |
| /** | ||
| * If the lint option "cast:redundant" is set, this method issues a warning if the cast is | ||
| * redundant. | ||
| * | ||
| * @param typeCastTree the TypeCastTree to check | ||
| */ | ||
| protected void checkTypecastRedundancy(TypeCastTree typeCastTree) { | ||
| if (!checker.getLintOption("cast:redundant", false)) { | ||
| if (!lintCastRedundantEnabled) { | ||
| return; | ||
| } | ||
|
|
||
|
|
@@ -2729,13 +2743,13 @@ protected void checkTypecastRedundancy(TypeCastTree typeCastTree) { | |
|
|
||
| /** | ||
| * Issues a warning if the given explicitly-written typecast is unsafe. Does nothing if the lint | ||
| * option "cast:unsafe" is not set. Only primary qualifiers are checked unless the command line | ||
| * option "-cast:unsafe" is set. Only primary qualifiers are checked unless the command line | ||
| * option "checkCastElementType" is supplied. | ||
| * | ||
| * @param typeCastTree an explicitly-written typecast | ||
| */ | ||
| protected void checkTypecastSafety(TypeCastTree typeCastTree) { | ||
| if (!checker.getLintOption("cast:unsafe", true)) { | ||
| if (!lintCastUnsafeEnabled) { | ||
| return; | ||
| } | ||
| AnnotatedTypeMirror castType = atypeFactory.getAnnotatedType(typeCastTree); | ||
|
|
@@ -2917,6 +2931,9 @@ public Void visitTypeCast(TypeCastTree tree, Void p) { | |
|
|
||
| @Override | ||
| public Void visitInstanceOf(InstanceOfTree tree, Void p) { | ||
| if (!lintInstanceofUnsafeEnabled) { | ||
| return super.visitInstanceOf(tree, p); | ||
| } | ||
| // The "reference type" is the type after "instanceof". | ||
| Tree patternTree = InstanceOfUtils.getPattern(tree); | ||
| if (patternTree != null) { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.