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 Jun 7, 2023
1 parent 0a8926e commit 5cd3aa1
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 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 @@ -75,6 +75,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 @@ -163,6 +164,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 @@ -180,6 +182,7 @@ private ErrorProneOptions(
boolean disableWarningsInGeneratedCode,
boolean disableAllWarnings,
boolean dropErrorsToWarnings,
boolean suggestionsAsWarnings,
boolean enableAllChecksAsWarnings,
boolean disableAllChecks,
boolean isTestOnlyTarget,
Expand All @@ -195,6 +198,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 @@ -230,6 +234,10 @@ public boolean isDropErrorsToWarnings() {
return dropErrorsToWarnings;
}

public boolean isSuggestionsAsWarnings() {
return suggestionsAsWarnings;
}

public boolean isTestOnlyTarget() {
return isTestOnlyTarget;
}
Expand Down Expand Up @@ -263,6 +271,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 @@ -319,6 +328,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 @@ -364,6 +377,7 @@ public ErrorProneOptions build(ImmutableList<String> remainingArgs) {
disableWarningsInGeneratedCode,
disableAllWarnings,
dropErrorsToWarnings,
suggestionsAsWarnings,
enableAllChecksAsWarnings,
disableAllChecks,
isTestOnlyTarget,
Expand Down Expand Up @@ -420,6 +434,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 @@ -147,7 +147,8 @@ public ScannerSupplier applyOverrides(ErrorProneOptions errorProneOptions) {
&& !errorProneOptions.isEnableAllChecksAsWarnings()
&& !errorProneOptions.isDropErrorsToWarnings()
&& !errorProneOptions.isDisableAllChecks()
&& !errorProneOptions.isDisableAllWarnings()) {
&& !errorProneOptions.isDisableAllWarnings()
&& !errorProneOptions.isSuggestionsAsWarnings()) {
return this;
}

Expand All @@ -168,6 +169,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()) {
checks.values().stream()
.filter(c -> c.defaultSeverity() == SeverityLevel.WARNING)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,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 @@ -42,12 +42,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 @@ -557,6 +560,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 canSuppressViaAltName() {
ScannerSupplier ss = ScannerSupplier.fromBugCheckerClasses(WithAltName.class);
Expand Down

0 comments on commit 5cd3aa1

Please sign in to comment.