From 8eb481d7c441b7080259370c09c2c24f30771e12 Mon Sep 17 00:00:00 2001 From: Gijs de Jong Date: Thu, 7 Sep 2023 11:16:41 -0700 Subject: [PATCH] Ignore disabled checks passed to `-XepPatchChecks` Rather than throwing an `NoSuchElementException`. Fixes #3908. Fixes #4028 FUTURE_COPYBARA_INTEGRATE_REVIEW=https://github.com/google/error-prone/pull/4028 from PicnicSupermarket:gdejong/remove-disabled-checks 4d502e0599fa59fe440fb29f2a21cdbaed89da9d PiperOrigin-RevId: 563489997 --- .../errorprone/BaseErrorProneJavaCompiler.java | 6 ++---- .../google/errorprone/scanner/ScannerTest.java | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/check_api/src/main/java/com/google/errorprone/BaseErrorProneJavaCompiler.java b/check_api/src/main/java/com/google/errorprone/BaseErrorProneJavaCompiler.java index 70faabf96a5..808a73a72bd 100644 --- a/check_api/src/main/java/com/google/errorprone/BaseErrorProneJavaCompiler.java +++ b/check_api/src/main/java/com/google/errorprone/BaseErrorProneJavaCompiler.java @@ -227,14 +227,12 @@ static ErrorProneAnalyzer createAnalyzer( .customRefactorer() .or( () -> { - ScannerSupplier toUse = - ErrorPronePlugins.loadPlugins(scannerSupplier, context) - .applyOverrides(epOptions); + ScannerSupplier toUse = ErrorPronePlugins.loadPlugins(scannerSupplier, context); ImmutableSet namedCheckers = epOptions.patchingOptions().namedCheckers(); if (!namedCheckers.isEmpty()) { toUse = toUse.filter(bci -> namedCheckers.contains(bci.canonicalName())); } - return ErrorProneScannerTransformer.create(toUse.get()); + return ErrorProneScannerTransformer.create(toUse.applyOverrides(epOptions).get()); }) .get(); diff --git a/core/src/test/java/com/google/errorprone/scanner/ScannerTest.java b/core/src/test/java/com/google/errorprone/scanner/ScannerTest.java index 39648b552df..0a1a1899c8f 100644 --- a/core/src/test/java/com/google/errorprone/scanner/ScannerTest.java +++ b/core/src/test/java/com/google/errorprone/scanner/ScannerTest.java @@ -95,6 +95,22 @@ public void suppressionAnnotationIgnoredWithOptions() { .doTest(); } + @Test + public void dontRunPatchForDisabledChecks() { + compilationHelper + .addSourceLines( + "Test.java", + "import com.google.errorprone.scanner.ScannerTest.Foo;", + "class Test {", + " Foo foo;", + "}") + .setArgs( + "-XepPatchLocation:IN_PLACE", + "-XepPatchChecks:ShouldNotUseFoo", + "-Xep:ShouldNotUseFoo:OFF") + .doTest(); + } + @OkToUseFoo // Foo can use itself. But this shouldn't suppress errors on *usages* of Foo. public static final class Foo {}