Skip to content

Commit de6b680

Browse files
authored
Add AdditionalSuppressionNames configuration option (#1231)
Prior to this change, NullAway could be skipped only with an explicit `@SuppressWarnings("NullAway")` annotation. However, existing inspections like `DataFlowIssue` by JetBrains imply that NullAway should be skipped too. With this change, NullAway can be configured to back off when additional names are present, via the `SuppressionNameAliases` configuration option. * Closes #1230
1 parent 10d4fee commit de6b680

5 files changed

Lines changed: 65 additions & 2 deletions

File tree

nullaway/src/main/java/com/uber/nullaway/Config.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,13 @@ public interface Config {
240240
*/
241241
@Nullable String getCastToNonNullMethod();
242242

243+
/**
244+
* Gets the suppression name aliases.
245+
*
246+
* @return the name aliases that should be honored as part of a @SuppressWarnings annotation.
247+
*/
248+
Set<String> getSuppressionNameAliases();
249+
243250
/**
244251
* Gets an optional comment to add to auto-fix suppressions.
245252
*

nullaway/src/main/java/com/uber/nullaway/DummyOptionsConfig.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ public Set<String> getOptionalClassPaths() {
179179
throw new IllegalStateException(ERROR_MESSAGE);
180180
}
181181

182+
@Override
183+
public Set<String> getSuppressionNameAliases() {
184+
throw new IllegalStateException(ERROR_MESSAGE);
185+
}
186+
182187
@Override
183188
public String getAutofixSuppressionComment() {
184189
throw new IllegalStateException(ERROR_MESSAGE);

nullaway/src/main/java/com/uber/nullaway/ErrorProneCLIFlagsConfig.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ final class ErrorProneCLIFlagsConfig implements Config {
7979
static final String FL_OPTIONAL_CLASS_PATHS =
8080
EP_FL_NAMESPACE + ":CheckOptionalEmptinessCustomClasses";
8181
static final String FL_SUPPRESS_COMMENT = EP_FL_NAMESPACE + ":AutoFixSuppressionComment";
82+
static final String FL_SUPPRESS_NAMES = EP_FL_NAMESPACE + ":SuppressionNameAliases";
8283

8384
static final String FL_SKIP_LIBRARY_MODELS = EP_FL_NAMESPACE + ":IgnoreLibraryModelsFor";
8485

@@ -228,6 +229,7 @@ final class ErrorProneCLIFlagsConfig implements Config {
228229
private final ImmutableSet<String> contractAnnotations;
229230
private final @Nullable String castToNonNullMethod;
230231
private final String autofixSuppressionComment;
232+
private final ImmutableSet<String> suppressionNameAliases;
231233
private final ImmutableSet<String> skippedLibraryModels;
232234
private final ImmutableSet<String> extraFuturesClasses;
233235

@@ -313,6 +315,7 @@ final class ErrorProneCLIFlagsConfig implements Config {
313315
throw new IllegalStateException(
314316
"Invalid -XepOpt:" + FL_SUPPRESS_COMMENT + " value. Comment must be single line.");
315317
}
318+
suppressionNameAliases = getFlagStringSet(flags, FL_SUPPRESS_NAMES);
316319
skippedLibraryModels = getFlagStringSet(flags, FL_SKIP_LIBRARY_MODELS);
317320
extraFuturesClasses = getFlagStringSet(flags, FL_EXTRA_FUTURES);
318321

@@ -534,6 +537,11 @@ public boolean assertsEnabled() {
534537
return castToNonNullMethod;
535538
}
536539

540+
@Override
541+
public ImmutableSet<String> getSuppressionNameAliases() {
542+
return suppressionNameAliases;
543+
}
544+
537545
@Override
538546
public String getAutofixSuppressionComment() {
539547
if (autofixSuppressionComment.trim().length() > 0) {

nullaway/src/main/java/com/uber/nullaway/NullAway.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
package com.uber.nullaway;
2424

2525
import static com.google.errorprone.BugPattern.SeverityLevel.WARNING;
26-
import static com.sun.source.tree.Tree.Kind.IDENTIFIER;
2726
import static com.sun.source.tree.Tree.Kind.OTHER;
2827
import static com.uber.nullaway.ASTHelpersBackports.hasDirectAnnotationWithSimpleName;
2928
import static com.uber.nullaway.ASTHelpersBackports.isStatic;
@@ -306,7 +305,14 @@ public NullAway(ErrorProneFlags flags) {
306305
config = new ErrorProneCLIFlagsConfig(flags);
307306
handler = Handlers.buildDefault(config);
308307
nonAnnotatedMethod = this::isMethodUnannotated;
309-
errorBuilder = new ErrorBuilder(config, canonicalName(), allNames());
308+
Set<String> allSuppressionNames =
309+
config.getSuppressionNameAliases().isEmpty()
310+
? allNames()
311+
: ImmutableSet.<String>builder()
312+
.addAll(allNames())
313+
.addAll(config.getSuppressionNameAliases())
314+
.build();
315+
errorBuilder = new ErrorBuilder(config, canonicalName(), allSuppressionNames);
310316
}
311317

312318
private boolean isMethodUnannotated(MethodInvocationNode invocationNode) {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.uber.nullaway;
2+
3+
import java.util.Arrays;
4+
import org.junit.Test;
5+
6+
public class SuppressionNameAliasesTests extends NullAwayTestsBase {
7+
8+
@Test
9+
public void additionalSuppressionNamesTest() {
10+
makeTestHelperWithArgs(
11+
Arrays.asList(
12+
"-d",
13+
temporaryFolder.getRoot().getAbsolutePath(),
14+
"-XepOpt:NullAway:AnnotatedPackages=com.uber",
15+
"-XepOpt:NullAway:SuppressionNameAliases=Foo,Bar"))
16+
.addSourceLines(
17+
"Test.java",
18+
"package com.uber;",
19+
"import org.jspecify.annotations.Nullable;",
20+
"class Test {",
21+
" @SuppressWarnings(\"Foo\")",
22+
" void foo(@Nullable Object o) {",
23+
" o.getClass();",
24+
" }",
25+
" @SuppressWarnings(\"Bar\")",
26+
" void bar(@Nullable Object o) {",
27+
" o.getClass();",
28+
" }",
29+
" @SuppressWarnings(\"Baz\")",
30+
" void baz(@Nullable Object o) {",
31+
" // BUG: Diagnostic contains: dereferenced expression o is @Nullable",
32+
" o.getClass();",
33+
" }",
34+
"}")
35+
.doTest();
36+
}
37+
}

0 commit comments

Comments
 (0)