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 authored and Stephan202 committed Oct 12, 2022
1 parent f05c968 commit 311b3ac
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
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 @@ -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;
Expand All @@ -163,6 +165,7 @@ private ErrorProneOptions(
boolean disableWarningsInGeneratedCode,
boolean disableAllWarnings,
boolean dropErrorsToWarnings,
boolean suggestionsAsWarnings,
boolean enableAllChecksAsWarnings,
boolean disableAllChecks,
boolean isTestOnlyTarget,
Expand All @@ -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;
Expand Down Expand Up @@ -211,6 +215,10 @@ public boolean isDropErrorsToWarnings() {
return dropErrorsToWarnings;
}

public boolean isSuggestionsAsWarnings() {
return suggestionsAsWarnings;
}

public boolean isTestOnlyTarget() {
return isTestOnlyTarget;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -336,6 +349,7 @@ public ErrorProneOptions build(ImmutableList<String> remainingArgs) {
disableWarningsInGeneratedCode,
disableAllWarnings,
dropErrorsToWarnings,
suggestionsAsWarnings,
enableAllChecksAsWarnings,
disableAllChecks,
isTestOnlyTarget,
Expand Down Expand Up @@ -393,6 +407,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,6 +143,7 @@ public ScannerSupplier applyOverrides(ErrorProneOptions errorProneOptions) {
&& errorProneOptions.getFlags().isEmpty()
&& !errorProneOptions.isEnableAllChecksAsWarnings()
&& !errorProneOptions.isDropErrorsToWarnings()
&& !errorProneOptions.isSuggestionsAsWarnings()
&& !errorProneOptions.isDisableAllChecks()) {
return this;
}
Expand All @@ -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)
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 311b3ac

Please sign in to comment.