Skip to content

Commit

Permalink
Introduce command-line flag -Xep:AllSuggestionsAsWarnings
Browse files Browse the repository at this point in the history
  • Loading branch information
rickie committed Nov 18, 2022
1 parent c7fcb6e commit 8ed0de3
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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)
Expand Down Expand Up @@ -161,6 +162,7 @@ final PatchingOptions build() {
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;
Expand All @@ -177,6 +179,7 @@ private ErrorProneOptions(
boolean disableWarningsInGeneratedCode,
boolean disableAllWarnings,
boolean dropErrorsToWarnings,
boolean suggestionsAsWarnings,
boolean enableAllChecksAsWarnings,
boolean disableAllChecks,
boolean isTestOnlyTarget,
Expand All @@ -191,6 +194,7 @@ private ErrorProneOptions(
this.disableWarningsInGeneratedCode = disableWarningsInGeneratedCode;
this.disableAllWarnings = disableAllWarnings;
this.dropErrorsToWarnings = dropErrorsToWarnings;
this.suggestionsAsWarnings = suggestionsAsWarnings;
this.enableAllChecksAsWarnings = enableAllChecksAsWarnings;
this.disableAllChecks = disableAllChecks;
this.isTestOnlyTarget = isTestOnlyTarget;
Expand Down Expand Up @@ -225,6 +229,10 @@ public boolean isDropErrorsToWarnings() {
return dropErrorsToWarnings;
}

public boolean isSuggestionsAsWarnings() {
return suggestionsAsWarnings;
}

public boolean isTestOnlyTarget() {
return isTestOnlyTarget;
}
Expand Down Expand Up @@ -254,6 +262,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;
Expand Down Expand Up @@ -309,6 +318,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)
Expand Down Expand Up @@ -350,6 +363,7 @@ public ErrorProneOptions build(ImmutableList<String> remainingArgs) {
disableWarningsInGeneratedCode,
disableAllWarnings,
dropErrorsToWarnings,
suggestionsAsWarnings,
enableAllChecksAsWarnings,
disableAllChecks,
isTestOnlyTarget,
Expand Down Expand Up @@ -405,6 +419,9 @@ public static ErrorProneOptions processArgs(Iterable<String> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ public ScannerSupplier applyOverrides(ErrorProneOptions errorProneOptions) {
&& errorProneOptions.getFlags().isEmpty()
&& !errorProneOptions.isEnableAllChecksAsWarnings()
&& !errorProneOptions.isDropErrorsToWarnings()
&& !errorProneOptions.isDisableAllChecks()
&& !errorProneOptions.isDisableAllWarnings()) {
&& !errorProneOptions.isSuggestionsAsWarnings()
&& !errorProneOptions.isDisableAllChecks()) {
return this;
}

Expand All @@ -165,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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, SeverityLevel> 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);
Expand Down

0 comments on commit 8ed0de3

Please sign in to comment.