-
Notifications
You must be signed in to change notification settings - Fork 745
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Discourage multiple nullness annotations
PiperOrigin-RevId: 579321387
- Loading branch information
Showing
4 changed files
with
200 additions
and
0 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
77 changes: 77 additions & 0 deletions
77
...src/main/java/com/google/errorprone/bugpatterns/nullness/MultipleNullnessAnnotations.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,77 @@ | ||
/* | ||
* Copyright 2023 The Error Prone Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.errorprone.bugpatterns.nullness; | ||
|
||
import static com.google.errorprone.BugPattern.SeverityLevel.WARNING; | ||
import static com.google.errorprone.matchers.Description.NO_MATCH; | ||
|
||
import com.google.common.collect.ImmutableSet; | ||
import com.google.errorprone.BugPattern; | ||
import com.google.errorprone.VisitorState; | ||
import com.google.errorprone.bugpatterns.BugChecker; | ||
import com.google.errorprone.bugpatterns.BugChecker.AnnotatedTypeTreeMatcher; | ||
import com.google.errorprone.bugpatterns.BugChecker.MethodTreeMatcher; | ||
import com.google.errorprone.bugpatterns.BugChecker.VariableTreeMatcher; | ||
import com.google.errorprone.dataflow.nullnesspropagation.NullnessAnnotations; | ||
import com.google.errorprone.matchers.Description; | ||
import com.google.errorprone.util.ASTHelpers; | ||
import com.sun.source.tree.AnnotatedTypeTree; | ||
import com.sun.source.tree.MethodTree; | ||
import com.sun.source.tree.Tree; | ||
import com.sun.source.tree.VariableTree; | ||
import com.sun.tools.javac.code.Symbol; | ||
import java.util.Collection; | ||
import javax.lang.model.element.AnnotationMirror; | ||
|
||
/** A {@link BugChecker}; see the associated {@link BugPattern} annotation for details. */ | ||
@BugPattern(summary = "This type use has conflicting nullness annotations", severity = WARNING) | ||
public class MultipleNullnessAnnotations extends BugChecker | ||
implements AnnotatedTypeTreeMatcher, MethodTreeMatcher, VariableTreeMatcher { | ||
@Override | ||
public Description matchAnnotatedType(AnnotatedTypeTree tree, VisitorState state) { | ||
return match(tree, ASTHelpers.getType(tree).getAnnotationMirrors()); | ||
} | ||
|
||
@Override | ||
public Description matchMethod(MethodTree tree, VisitorState state) { | ||
return match(tree, ASTHelpers.getSymbol(tree), tree.getReturnType()); | ||
} | ||
|
||
@Override | ||
public Description matchVariable(VariableTree tree, VisitorState state) { | ||
return match(tree, ASTHelpers.getSymbol(tree), tree.getType()); | ||
} | ||
|
||
private Description match(Tree tree, Symbol symbol, Tree type) { | ||
if (type == null) { | ||
return NO_MATCH; | ||
} | ||
return match( | ||
tree, | ||
ImmutableSet.<AnnotationMirror>builder() | ||
.addAll(symbol.getAnnotationMirrors()) | ||
.addAll(ASTHelpers.getType(type).getAnnotationMirrors()) | ||
.build()); | ||
} | ||
|
||
private Description match(Tree tree, Collection<? extends AnnotationMirror> annotations) { | ||
if (NullnessAnnotations.annotationsAreAmbiguous(annotations)) { | ||
return describeMatch(tree); | ||
} | ||
return NO_MATCH; | ||
} | ||
} |
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
109 changes: 109 additions & 0 deletions
109
...test/java/com/google/errorprone/bugpatterns/nullness/MultipleNullnessAnnotationsTest.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,109 @@ | ||
/* | ||
* Copyright 2023 The Error Prone Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.errorprone.bugpatterns.nullness; | ||
|
||
import com.google.errorprone.CompilationTestHelper; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
@RunWith(JUnit4.class) | ||
public class MultipleNullnessAnnotationsTest { | ||
private final CompilationTestHelper testHelper = | ||
CompilationTestHelper.newInstance(MultipleNullnessAnnotations.class, getClass()); | ||
|
||
@Test | ||
public void positive() { | ||
testHelper | ||
.addSourceLines( | ||
"Test.java", | ||
"import org.checkerframework.checker.nullness.compatqual.NonNullDecl;", | ||
"import org.checkerframework.checker.nullness.compatqual.NullableDecl;", | ||
"import org.checkerframework.checker.nullness.qual.NonNull;", | ||
"import org.checkerframework.checker.nullness.qual.Nullable;", | ||
"import java.util.List;", | ||
"abstract class Test {", | ||
" // BUG: Diagnostic contains:", | ||
" @Nullable @NonNull Object x;", | ||
" // BUG: Diagnostic contains:", | ||
" @NullableDecl static @NonNull Object y;", | ||
" // BUG: Diagnostic contains:", | ||
" List<@Nullable @NonNull String> z;", | ||
" // BUG: Diagnostic contains:", | ||
" @NullableDecl abstract @NonNull Object f();", | ||
" // BUG: Diagnostic contains:", | ||
" abstract void f(@NullableDecl Object @NonNull[] x);", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void negative() { | ||
testHelper | ||
.addSourceLines( | ||
"Test.java", | ||
"import org.checkerframework.checker.nullness.compatqual.NonNullDecl;", | ||
"import org.checkerframework.checker.nullness.compatqual.NullableDecl;", | ||
"import org.checkerframework.checker.nullness.qual.NonNull;", | ||
"import org.checkerframework.checker.nullness.qual.Nullable;", | ||
"import java.util.List;", | ||
"abstract class Test {", | ||
" @NonNullDecl @NonNull Object x;", | ||
" @NullableDecl static @Nullable Object y;", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void disambiguation() { | ||
testHelper | ||
.addSourceLines( | ||
"Test.java", | ||
"import org.checkerframework.checker.nullness.qual.Nullable;", | ||
"import java.util.List;", | ||
"abstract class Test {", | ||
" @Nullable Object @Nullable [] x;", | ||
" abstract void f(@Nullable Object @Nullable ... x);", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void declarationAndType() { | ||
testHelper | ||
.addSourceLines( | ||
"Nullable.java", | ||
"import java.lang.annotation.Target;", | ||
"import java.lang.annotation.ElementType;", | ||
"@Target({", | ||
" ElementType.METHOD,", | ||
" ElementType.FIELD,", | ||
" ElementType.PARAMETER,", | ||
" ElementType.LOCAL_VARIABLE,", | ||
" ElementType.TYPE_USE", | ||
"})", | ||
"public @interface Nullable {}") | ||
.addSourceLines( | ||
"Test.java", | ||
"abstract class Test {", | ||
" abstract void f(@Nullable Object x);", | ||
" abstract @Nullable Object g();", | ||
" @Nullable Object f;", | ||
"}") | ||
.doTest(); | ||
} | ||
} |