Search in sources :

Example 1 with ErrorProneOptions

use of com.google.errorprone.ErrorProneOptions in project error-prone by google.

the class ScannerSupplier method applyOverrides.

/**
   * Applies options to this {@link ScannerSupplier}.
   *
   * <p>Command-line options to override check severities may do any of the following:
   * <ul>
   * <li>Enable a check that is currently off</li>
   * <li>Disable a check that is currently on</li>
   * <li>Change the severity of a check that is on, promoting a warning to an error or demoting
   * an error to a warning</li>
   * </ul>
   *
   * @param errorProneOptions an {@link ErrorProneOptions} object that encapsulates the overrides
   * for this compilation
   * @throws InvalidCommandLineOptionException if the override map attempts to disable a check
   * that may not be disabled
   */
@CheckReturnValue
public ScannerSupplier applyOverrides(ErrorProneOptions errorProneOptions) throws InvalidCommandLineOptionException {
    Map<String, Severity> severityOverrides = errorProneOptions.getSeverityMap();
    if (severityOverrides.isEmpty() && !errorProneOptions.isEnableAllChecks() && !errorProneOptions.isDropErrorsToWarnings() && !errorProneOptions.isDisableAllChecks()) {
        return this;
    }
    // Initialize result allChecks map and enabledChecks set with current state of this Supplier.
    ImmutableBiMap<String, BugCheckerInfo> checks = getAllChecks();
    Map<String, SeverityLevel> severities = new LinkedHashMap<>(severities());
    Set<String> disabled = new HashSet<>(disabled());
    if (errorProneOptions.isEnableAllChecks()) {
        disabled.forEach(c -> severities.put(c, checks.get(c).defaultSeverity()));
        disabled.clear();
    }
    if (errorProneOptions.isDropErrorsToWarnings()) {
        getAllChecks().values().stream().filter(c -> c.defaultSeverity() == SeverityLevel.ERROR && c.suppressibility().disableable()).forEach(c -> severities.put(c.canonicalName(), SeverityLevel.WARNING));
    }
    if (errorProneOptions.isDisableAllChecks()) {
        getAllChecks().values().stream().filter(c -> c.suppressibility().disableable()).forEach(c -> disabled.add(c.canonicalName()));
    }
    // Process overrides
    severityOverrides.forEach((checkName, newSeverity) -> {
        BugCheckerInfo check = getAllChecks().get(checkName);
        if (check == null) {
            if (errorProneOptions.ignoreUnknownChecks()) {
                return;
            }
            throw new InvalidCommandLineOptionException(checkName + " is not a valid checker name");
        }
        switch(newSeverity) {
            case OFF:
                if (!check.suppressibility().disableable()) {
                    throw new InvalidCommandLineOptionException(check.canonicalName() + " may not be disabled");
                }
                severities.remove(check.canonicalName());
                disabled.add(check.canonicalName());
                break;
            case DEFAULT:
                severities.put(check.canonicalName(), check.defaultSeverity());
                disabled.remove(check.canonicalName());
                break;
            case WARN:
                // Demoting an enabled check from an error to a warning is a form of disabling
                if (!disabled().contains(check.canonicalName()) && !check.suppressibility().disableable() && check.defaultSeverity() == SeverityLevel.ERROR) {
                    throw new InvalidCommandLineOptionException(check.canonicalName() + " is not disableable and may not be demoted to a warning");
                }
                severities.put(check.canonicalName(), SeverityLevel.WARNING);
                disabled.remove(check.canonicalName());
                break;
            case ERROR:
                severities.put(check.canonicalName(), SeverityLevel.ERROR);
                disabled.remove(check.canonicalName());
                break;
            default:
                throw new IllegalStateException("Unexpected severity level: " + newSeverity);
        }
    });
    return new ScannerSupplierImpl(checks, ImmutableMap.copyOf(severities), ImmutableSet.copyOf(disabled));
}
Also used : Arrays(java.util.Arrays) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Supplier(com.google.common.base.Supplier) BugChecker(com.google.errorprone.bugpatterns.BugChecker) Set(java.util.Set) Severity(com.google.errorprone.ErrorProneOptions.Severity) Sets(com.google.common.collect.Sets) ImmutableBiMap(com.google.common.collect.ImmutableBiMap) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) CheckReturnValue(javax.annotation.CheckReturnValue) ImmutableList(com.google.common.collect.ImmutableList) Predicate(com.google.common.base.Predicate) BugCheckerInfo(com.google.errorprone.BugCheckerInfo) ErrorProneOptions(com.google.errorprone.ErrorProneOptions) Map(java.util.Map) BugPattern(com.google.errorprone.BugPattern) VisibleForTesting(com.google.common.annotations.VisibleForTesting) InvalidCommandLineOptionException(com.google.errorprone.InvalidCommandLineOptionException) SeverityLevel(com.google.errorprone.BugPattern.SeverityLevel) BugCheckerInfo(com.google.errorprone.BugCheckerInfo) InvalidCommandLineOptionException(com.google.errorprone.InvalidCommandLineOptionException) Severity(com.google.errorprone.ErrorProneOptions.Severity) LinkedHashMap(java.util.LinkedHashMap) SeverityLevel(com.google.errorprone.BugPattern.SeverityLevel) HashSet(java.util.HashSet) CheckReturnValue(javax.annotation.CheckReturnValue)

Example 2 with ErrorProneOptions

use of com.google.errorprone.ErrorProneOptions in project error-prone by google.

the class ScannerSupplierTest method applyOverridesDisablesChecks.

@Test
public void applyOverridesDisablesChecks() throws Exception {
    ScannerSupplier ss = ScannerSupplier.fromBugCheckerClasses(ChainingConstructorIgnoresParameter.class, DepAnn.class, LongLiteralLowerCaseSuffix.class);
    ErrorProneOptions epOptions = ErrorProneOptions.processArgs(ImmutableList.of("-Xep:LongLiteralLowerCaseSuffix:OFF", "-Xep:ChainingConstructorIgnoresParameter:OFF"));
    assertScanner(ss.applyOverrides(epOptions)).hasEnabledChecks(DepAnn.class);
}
Also used : ErrorProneOptions(com.google.errorprone.ErrorProneOptions) ErrorProneJavaCompilerTest(com.google.errorprone.ErrorProneJavaCompilerTest) Test(org.junit.Test)

Example 3 with ErrorProneOptions

use of com.google.errorprone.ErrorProneOptions in project error-prone by google.

the class ScannerSupplierTest method applyOverridesThrowsExceptionWhenDisablingNonDisablableCheck.

@Test
public void applyOverridesThrowsExceptionWhenDisablingNonDisablableCheck() throws Exception {
    ScannerSupplier ss = ScannerSupplier.fromBugCheckerClasses(ErrorProneJavaCompilerTest.UnsuppressibleArrayEquals.class);
    ErrorProneOptions epOptions = ErrorProneOptions.processArgs(ImmutableList.of("-Xep:ArrayEquals:OFF"));
    InvalidCommandLineOptionException exception = expectThrows(InvalidCommandLineOptionException.class, () -> ss.applyOverrides(epOptions));
    assertThat(exception.getMessage()).contains("may not be disabled");
}
Also used : ErrorProneOptions(com.google.errorprone.ErrorProneOptions) InvalidCommandLineOptionException(com.google.errorprone.InvalidCommandLineOptionException) ErrorProneJavaCompilerTest(com.google.errorprone.ErrorProneJavaCompilerTest) ErrorProneJavaCompilerTest(com.google.errorprone.ErrorProneJavaCompilerTest) Test(org.junit.Test)

Example 4 with ErrorProneOptions

use of com.google.errorprone.ErrorProneOptions in project error-prone by google.

the class ScannerSupplierTest method applyOverridesDisableAllChecks.

@Test
public void applyOverridesDisableAllChecks() throws Exception {
    // Create new scanner.
    ScannerSupplier ss = ScannerSupplier.fromBugCheckerClasses(ArrayEquals.class, BadShiftAmount.class, StaticQualifiedUsingExpression.class);
    // Make sure all checks in the scanner start enabled.
    assertScanner(ss).hasEnabledChecks(ArrayEquals.class, BadShiftAmount.class, StaticQualifiedUsingExpression.class);
    // Apply the 'DisableAllChecks' flag, make sure all checks are disabled.
    ErrorProneOptions epOptions = ErrorProneOptions.processArgs(ImmutableList.of("-XepDisableAllChecks"));
    assertScanner(ss.applyOverrides(epOptions)).hasEnabledChecks();
    // Don't suppress unsuppressible checks.
    ss = ss.plus(ScannerSupplier.fromBugCheckerClasses(RestrictedApiChecker.class));
    assertScanner(ss.applyOverrides(epOptions)).hasEnabledChecks(RestrictedApiChecker.class);
    // Apply 'DisableAllChecks' flag with another -Xep flag
    epOptions = ErrorProneOptions.processArgs(ImmutableList.of("-XepDisableAllChecks", "-Xep:ArrayEquals:ERROR"));
    // Make sure the severity flag overrides the global disable flag.
    assertScanner(ss.applyOverrides(epOptions)).hasEnabledChecks(RestrictedApiChecker.class, ArrayEquals.class);
    // Order matters. The DisableAllChecks flag should override all severities that come before it.
    epOptions = ErrorProneOptions.processArgs(ImmutableList.of("-Xep:ArrayEquals:ERROR", "-XepDisableAllChecks"));
    // Make sure the global disable flag overrides flags that come before it.
    assertScanner(ss.applyOverrides(epOptions)).hasEnabledChecks(RestrictedApiChecker.class);
    // The 'DisableAllChecks' flag doesn't populate through to additional plugins.
    assertScanner(ss.applyOverrides(epOptions).plus(ScannerSupplier.fromBugCheckerClasses(DivZero.class))).hasEnabledChecks(RestrictedApiChecker.class, DivZero.class);
}
Also used : DivZero(com.google.errorprone.bugpatterns.DivZero) ErrorProneOptions(com.google.errorprone.ErrorProneOptions) ErrorProneJavaCompilerTest(com.google.errorprone.ErrorProneJavaCompilerTest) Test(org.junit.Test)

Example 5 with ErrorProneOptions

use of com.google.errorprone.ErrorProneOptions in project error-prone by google.

the class ScannerSupplierTest method applyOverridesSucceedsWhenDisablingUnknownCheckAndIgnoreUnknownCheckNamesIsSet.

@Test
public void applyOverridesSucceedsWhenDisablingUnknownCheckAndIgnoreUnknownCheckNamesIsSet() throws Exception {
    ScannerSupplier ss = ScannerSupplier.fromBugCheckerClasses(ArrayEquals.class);
    ErrorProneOptions epOptions = ErrorProneOptions.processArgs(ImmutableList.of("-XepIgnoreUnknownCheckNames", "-Xep:foo:OFF"));
    assertScanner(ss.applyOverrides(epOptions)).hasEnabledChecks(ArrayEquals.class);
}
Also used : ErrorProneOptions(com.google.errorprone.ErrorProneOptions) ErrorProneJavaCompilerTest(com.google.errorprone.ErrorProneJavaCompilerTest) Test(org.junit.Test)

Aggregations

ErrorProneOptions (com.google.errorprone.ErrorProneOptions)12 ErrorProneJavaCompilerTest (com.google.errorprone.ErrorProneJavaCompilerTest)11 Test (org.junit.Test)11 InvalidCommandLineOptionException (com.google.errorprone.InvalidCommandLineOptionException)4 SeverityLevel (com.google.errorprone.BugPattern.SeverityLevel)3 UnsuppressibleArrayEquals (com.google.errorprone.ErrorProneJavaCompilerTest.UnsuppressibleArrayEquals)3 BadShiftAmount (com.google.errorprone.bugpatterns.BadShiftAmount)3 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 BugCheckerInfo (com.google.errorprone.BugCheckerInfo)2 ArrayEquals (com.google.errorprone.bugpatterns.ArrayEquals)2 BugChecker (com.google.errorprone.bugpatterns.BugChecker)2 DivZero (com.google.errorprone.bugpatterns.DivZero)2 EqualsIncompatibleType (com.google.errorprone.bugpatterns.EqualsIncompatibleType)2 StaticQualifiedUsingExpression (com.google.errorprone.bugpatterns.StaticQualifiedUsingExpression)2 Map (java.util.Map)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Predicate (com.google.common.base.Predicate)1 Predicates (com.google.common.base.Predicates)1 Supplier (com.google.common.base.Supplier)1