From 311b3aca6e87e8437ec82db2dda827b084fe2064 Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Fri, 17 Jun 2022 14:20:50 +0200 Subject: [PATCH] Introduce command-line flag `-Xep:AllSuggestionsAsWarnings` See google/error-prone#3301. --- .../google/errorprone/ErrorProneOptions.java | 19 ++++++++++++- .../errorprone/scanner/ScannerSupplier.java | 7 +++++ .../errorprone/ErrorProneOptionsTest.java | 7 +++++ .../scanner/ScannerSupplierTest.java | 27 +++++++++++++++++++ 4 files changed, 59 insertions(+), 1 deletion(-) 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 7e1989abd6bd..6d0c5cc421e9 100644 --- a/check_api/src/main/java/com/google/errorprone/ErrorProneOptions.java +++ b/check_api/src/main/java/com/google/errorprone/ErrorProneOptions.java @@ -52,8 +52,8 @@ public class ErrorProneOptions { private static final String PATCH_IMPORT_ORDER_PREFIX = "-XepPatchImportOrder:"; private static final String EXCLUDED_PATHS_PREFIX = "-XepExcludedPaths:"; private static final String IGNORE_LARGE_CODE_GENERATORS = "-XepIgnoreLargeCodeGenerators:"; - private static final String ERRORS_AS_WARNINGS_FLAG = "-XepAllErrorsAsWarnings"; + private static final String SUGGESTIONS_AS_WARNINGS_FLAG = "-XepAllSuggestionsAsWarnings"; private static final String ENABLE_ALL_CHECKS = "-XepAllDisabledChecksAsWarnings"; private static final String IGNORE_SUPPRESSION_ANNOTATIONS = "-XepIgnoreSuppressionAnnotations"; private static final String DISABLE_ALL_CHECKS = "-XepDisableAllChecks"; @@ -74,6 +74,7 @@ public static int isSupportedOption(String option) { || option.equals(IGNORE_UNKNOWN_CHECKS_FLAG) || option.equals(DISABLE_WARNINGS_IN_GENERATED_CODE_FLAG) || option.equals(ERRORS_AS_WARNINGS_FLAG) + || option.equals(SUGGESTIONS_AS_WARNINGS_FLAG) || option.equals(ENABLE_ALL_CHECKS) || option.equals(DISABLE_ALL_CHECKS) || option.equals(IGNORE_SUPPRESSION_ANNOTATIONS) @@ -147,6 +148,7 @@ abstract static class Builder { private final boolean disableWarningsInGeneratedCode; private final boolean disableAllWarnings; private final boolean dropErrorsToWarnings; + private final boolean suggestionsAsWarnings; private final boolean enableAllChecksAsWarnings; private final boolean disableAllChecks; private final boolean isTestOnlyTarget; @@ -163,6 +165,7 @@ private ErrorProneOptions( boolean disableWarningsInGeneratedCode, boolean disableAllWarnings, boolean dropErrorsToWarnings, + boolean suggestionsAsWarnings, boolean enableAllChecksAsWarnings, boolean disableAllChecks, boolean isTestOnlyTarget, @@ -177,6 +180,7 @@ private ErrorProneOptions( this.disableWarningsInGeneratedCode = disableWarningsInGeneratedCode; this.disableAllWarnings = disableAllWarnings; this.dropErrorsToWarnings = dropErrorsToWarnings; + this.suggestionsAsWarnings = suggestionsAsWarnings; this.enableAllChecksAsWarnings = enableAllChecksAsWarnings; this.disableAllChecks = disableAllChecks; this.isTestOnlyTarget = isTestOnlyTarget; @@ -211,6 +215,10 @@ public boolean isDropErrorsToWarnings() { return dropErrorsToWarnings; } + public boolean isSuggestionsAsWarnings() { + return suggestionsAsWarnings; + } + public boolean isTestOnlyTarget() { return isTestOnlyTarget; } @@ -240,6 +248,7 @@ private static class Builder { private boolean disableAllWarnings = false; private boolean disableWarningsInGeneratedCode = false; private boolean dropErrorsToWarnings = false; + private boolean suggestionsAsWarnings = false; private boolean enableAllChecksAsWarnings = false; private boolean disableAllChecks = false; private boolean isTestOnlyTarget = false; @@ -295,6 +304,10 @@ public void setDropErrorsToWarnings(boolean dropErrorsToWarnings) { this.dropErrorsToWarnings = dropErrorsToWarnings; } + public void setSuggestionsAsWarnings(boolean suggestionsAsWarnings) { + this.suggestionsAsWarnings = suggestionsAsWarnings; + } + public void setDisableAllWarnings(boolean disableAllWarnings) { severityMap.entrySet().stream() .filter(e -> e.getValue() == Severity.WARN) @@ -336,6 +349,7 @@ public ErrorProneOptions build(ImmutableList remainingArgs) { disableWarningsInGeneratedCode, disableAllWarnings, dropErrorsToWarnings, + suggestionsAsWarnings, enableAllChecksAsWarnings, disableAllChecks, isTestOnlyTarget, @@ -393,6 +407,9 @@ public static ErrorProneOptions processArgs(Iterable args) { case ERRORS_AS_WARNINGS_FLAG: builder.setDropErrorsToWarnings(true); break; + case SUGGESTIONS_AS_WARNINGS_FLAG: + builder.setSuggestionsAsWarnings(true); + break; case ENABLE_ALL_CHECKS: builder.setEnableAllChecksAsWarnings(true); break; 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 037b2c1f597e..cc08437fa98a 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 @@ -143,6 +143,7 @@ public ScannerSupplier applyOverrides(ErrorProneOptions errorProneOptions) { && errorProneOptions.getFlags().isEmpty() && !errorProneOptions.isEnableAllChecksAsWarnings() && !errorProneOptions.isDropErrorsToWarnings() + && !errorProneOptions.isSuggestionsAsWarnings() && !errorProneOptions.isDisableAllChecks()) { return this; } @@ -164,6 +165,12 @@ public ScannerSupplier applyOverrides(ErrorProneOptions errorProneOptions) { .forEach(c -> severities.put(c.canonicalName(), SeverityLevel.WARNING)); } + if (errorProneOptions.isSuggestionsAsWarnings()) { + getAllChecks().values().stream() + .filter(c -> c.defaultSeverity() == SeverityLevel.SUGGESTION) + .forEach(c -> severities.put(c.canonicalName(), SeverityLevel.WARNING)); + } + if (errorProneOptions.isDisableAllWarnings()) { getAllChecks().values().stream() .filter(c -> c.defaultSeverity() == SeverityLevel.WARNING) 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 370f6144f6cc..2dc6511ee4f7 100644 --- a/check_api/src/test/java/com/google/errorprone/ErrorProneOptionsTest.java +++ b/check_api/src/test/java/com/google/errorprone/ErrorProneOptionsTest.java @@ -147,6 +147,13 @@ public void recognizesDemoteErrorToWarning() { assertThat(options.isDropErrorsToWarnings()).isTrue(); } + @Test + public void recognizesAllSuggestionsAsWarnings() { + ErrorProneOptions options = + ErrorProneOptions.processArgs(new String[] {"-XepAllSuggestionsAsWarnings"}); + assertThat(options.isSuggestionsAsWarnings()).isTrue(); + } + @Test public void recognizesDisableAllChecks() { ErrorProneOptions options = diff --git a/core/src/test/java/com/google/errorprone/scanner/ScannerSupplierTest.java b/core/src/test/java/com/google/errorprone/scanner/ScannerSupplierTest.java index 38c4c176a6f1..332e3f02af8a 100644 --- a/core/src/test/java/com/google/errorprone/scanner/ScannerSupplierTest.java +++ b/core/src/test/java/com/google/errorprone/scanner/ScannerSupplierTest.java @@ -41,12 +41,15 @@ import com.google.errorprone.InvalidCommandLineOptionException; import com.google.errorprone.bugpatterns.ArrayEquals; import com.google.errorprone.bugpatterns.BadShiftAmount; +import com.google.errorprone.bugpatterns.BooleanParameter; import com.google.errorprone.bugpatterns.BugChecker; import com.google.errorprone.bugpatterns.ChainingConstructorIgnoresParameter; +import com.google.errorprone.bugpatterns.ConstantField; import com.google.errorprone.bugpatterns.DepAnn; import com.google.errorprone.bugpatterns.EqualsIncompatibleType; import com.google.errorprone.bugpatterns.LongLiteralLowerCaseSuffix; import com.google.errorprone.bugpatterns.MethodCanBeStatic; +import com.google.errorprone.bugpatterns.MissingBraces; import com.google.errorprone.bugpatterns.PackageLocation; import com.google.errorprone.bugpatterns.ReferenceEquality; import com.google.errorprone.bugpatterns.StaticQualifiedUsingExpression; @@ -556,6 +559,30 @@ public void allChecksAsWarningsWorks() { assertScanner(withOverrides).hasSeverities(expectedSeverities); } + @Test + public void allSuggestionsAsWarnings() { + ScannerSupplier ss = + ScannerSupplier.fromBugCheckerClasses( + BooleanParameter.class, ConstantField.class, MissingBraces.class); + + assertScanner(ss) + .hasEnabledChecks(BooleanParameter.class, ConstantField.class, MissingBraces.class); + + ErrorProneOptions epOptions = + ErrorProneOptions.processArgs(ImmutableList.of("-XepAllSuggestionsAsWarnings")); + + ImmutableMap expectedSeverities = + ImmutableMap.of( + "BooleanParameter", + SeverityLevel.WARNING, + "ConstantField", + SeverityLevel.WARNING, + "MissingBraces", + SeverityLevel.WARNING); + + assertScanner(ss.applyOverrides(epOptions)).hasSeverities(expectedSeverities); + } + @Test public void disablingPackageLocation_suppressible() { ScannerSupplier ss = ScannerSupplier.fromBugCheckerClasses(PackageLocation.class);