diff --git a/check_api/src/main/java/com/google/errorprone/ErrorProneOptions.java b/check_api/src/main/java/com/google/errorprone/ErrorProneOptions.java index f317dab7956..49ebcba6395 100644 --- a/check_api/src/main/java/com/google/errorprone/ErrorProneOptions.java +++ b/check_api/src/main/java/com/google/errorprone/ErrorProneOptions.java @@ -56,6 +56,7 @@ public class ErrorProneOptions { private static final String ENABLE_ALL_CHECKS = "-XepAllDisabledChecksAsWarnings"; private static final String IGNORE_SUPPRESSION_ANNOTATIONS = "-XepIgnoreSuppressionAnnotations"; private static final String DISABLE_ALL_CHECKS = "-XepDisableAllChecks"; + private static final String DISABLE_ALL_WARNINGS = "-XepDisableAllWarnings"; private static final String IGNORE_UNKNOWN_CHECKS_FLAG = "-XepIgnoreUnknownCheckNames"; private static final String DISABLE_WARNINGS_IN_GENERATED_CODE_FLAG = "-XepDisableWarningsInGeneratedCode"; @@ -75,7 +76,8 @@ public static int isSupportedOption(String option) { || option.equals(ENABLE_ALL_CHECKS) || option.equals(DISABLE_ALL_CHECKS) || option.equals(IGNORE_SUPPRESSION_ANNOTATIONS) - || option.equals(COMPILING_TEST_ONLY_CODE); + || option.equals(COMPILING_TEST_ONLY_CODE) + || option.equals(DISABLE_ALL_WARNINGS); return isSupported ? 0 : -1; } @@ -156,6 +158,7 @@ final PatchingOptions build() { private final ImmutableMap severityMap; private final boolean ignoreUnknownChecks; private final boolean disableWarningsInGeneratedCode; + private final boolean disableAllWarnings; private final boolean dropErrorsToWarnings; private final boolean enableAllChecksAsWarnings; private final boolean disableAllChecks; @@ -171,6 +174,7 @@ private ErrorProneOptions( ImmutableList remainingArgs, boolean ignoreUnknownChecks, boolean disableWarningsInGeneratedCode, + boolean disableAllWarnings, boolean dropErrorsToWarnings, boolean enableAllChecksAsWarnings, boolean disableAllChecks, @@ -184,6 +188,7 @@ private ErrorProneOptions( this.remainingArgs = remainingArgs; this.ignoreUnknownChecks = ignoreUnknownChecks; this.disableWarningsInGeneratedCode = disableWarningsInGeneratedCode; + this.disableAllWarnings = disableAllWarnings; this.dropErrorsToWarnings = dropErrorsToWarnings; this.enableAllChecksAsWarnings = enableAllChecksAsWarnings; this.disableAllChecks = disableAllChecks; @@ -211,6 +216,10 @@ public boolean disableWarningsInGeneratedCode() { return disableWarningsInGeneratedCode; } + public boolean isDisableAllWarnings() { + return disableAllWarnings; + } + public boolean isDropErrorsToWarnings() { return dropErrorsToWarnings; } @@ -241,6 +250,7 @@ public Pattern getExcludedPattern() { private static class Builder { private boolean ignoreUnknownChecks = false; + private boolean disableAllWarnings = false; private boolean disableWarningsInGeneratedCode = false; private boolean dropErrorsToWarnings = false; private boolean enableAllChecksAsWarnings = false; @@ -298,6 +308,13 @@ public void setDropErrorsToWarnings(boolean dropErrorsToWarnings) { this.dropErrorsToWarnings = dropErrorsToWarnings; } + public void setDisableAllWarnings(boolean disableAllWarnings) { + severityMap.entrySet().stream() + .filter(e -> e.getValue() == Severity.WARN) + .forEach(e -> e.setValue(Severity.OFF)); + this.disableAllWarnings = disableAllWarnings; + } + public void setEnableAllChecksAsWarnings(boolean enableAllChecksAsWarnings) { // Checks manually disabled before this flag are reset to warning-level severityMap.entrySet().stream() @@ -330,6 +347,7 @@ public ErrorProneOptions build(ImmutableList remainingArgs) { remainingArgs, ignoreUnknownChecks, disableWarningsInGeneratedCode, + disableAllWarnings, dropErrorsToWarnings, enableAllChecksAsWarnings, disableAllChecks, @@ -395,6 +413,9 @@ public static ErrorProneOptions processArgs(Iterable args) { case COMPILING_TEST_ONLY_CODE: builder.setTestOnlyTarget(true); break; + case DISABLE_ALL_WARNINGS: + builder.setDisableAllWarnings(true); + break; default: if (arg.startsWith(SEVERITY_PREFIX)) { builder.parseSeverity(arg); diff --git a/check_api/src/main/java/com/google/errorprone/scanner/ScannerSupplier.java b/check_api/src/main/java/com/google/errorprone/scanner/ScannerSupplier.java index 92944da3689..452d8fd5120 100644 --- a/check_api/src/main/java/com/google/errorprone/scanner/ScannerSupplier.java +++ b/check_api/src/main/java/com/google/errorprone/scanner/ScannerSupplier.java @@ -166,6 +166,11 @@ public ScannerSupplier applyOverrides(ErrorProneOptions errorProneOptions) { .forEach(c -> severities.put(c.canonicalName(), SeverityLevel.WARNING)); } + if (errorProneOptions.isDisableAllWarnings()) { + getAllChecks().values().stream() + .filter(c -> c.defaultSeverity() == SeverityLevel.WARNING) + .forEach(c -> disabled.add(c.canonicalName())); + } if (errorProneOptions.isDisableAllChecks()) { getAllChecks().values().stream() .filter(c -> c.disableable()) diff --git a/check_api/src/test/java/com/google/errorprone/ErrorProneOptionsTest.java b/check_api/src/test/java/com/google/errorprone/ErrorProneOptionsTest.java index d413149eb16..26646b11ee9 100644 --- a/check_api/src/test/java/com/google/errorprone/ErrorProneOptionsTest.java +++ b/check_api/src/test/java/com/google/errorprone/ErrorProneOptionsTest.java @@ -161,6 +161,13 @@ public void recognizesCompilingTestOnlyCode() { assertThat(options.isTestOnlyTarget()).isTrue(); } + @Test + public void recognizesDisableAllWarnings() { + ErrorProneOptions options = + ErrorProneOptions.processArgs(new String[] {"-XepDisableAllWarnings"}); + assertThat(options.isDisableAllWarnings()).isTrue(); + } + @Test public void recognizesVisitSuppressedCode() { ErrorProneOptions options =