Skip to content

Commit

Permalink
Add -XepDisableAllWarnings flag
Browse files Browse the repository at this point in the history
This flag turns off all WARNING-level checks, while leaving on those at ERROR level. This is useful for those who want to compile with -Werror but don't want any EP WARNING-level issue to block the build. Individual WARNING level checks can still be set to ERROR level as desired.

Fixes #785

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=311831028
  • Loading branch information
msridhar authored and kluever committed May 16, 2020
1 parent 544b2b7 commit 0202733
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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;
}

Expand Down Expand Up @@ -156,6 +158,7 @@ final PatchingOptions build() {
private final ImmutableMap<String, Severity> 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;
Expand All @@ -171,6 +174,7 @@ private ErrorProneOptions(
ImmutableList<String> remainingArgs,
boolean ignoreUnknownChecks,
boolean disableWarningsInGeneratedCode,
boolean disableAllWarnings,
boolean dropErrorsToWarnings,
boolean enableAllChecksAsWarnings,
boolean disableAllChecks,
Expand All @@ -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;
Expand Down Expand Up @@ -211,6 +216,10 @@ public boolean disableWarningsInGeneratedCode() {
return disableWarningsInGeneratedCode;
}

public boolean isDisableAllWarnings() {
return disableAllWarnings;
}

public boolean isDropErrorsToWarnings() {
return dropErrorsToWarnings;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -330,6 +347,7 @@ public ErrorProneOptions build(ImmutableList<String> remainingArgs) {
remainingArgs,
ignoreUnknownChecks,
disableWarningsInGeneratedCode,
disableAllWarnings,
dropErrorsToWarnings,
enableAllChecksAsWarnings,
disableAllChecks,
Expand Down Expand Up @@ -395,6 +413,9 @@ public static ErrorProneOptions processArgs(Iterable<String> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down

0 comments on commit 0202733

Please sign in to comment.