From 176d8efe66eece5c5c761f2b644ff5e625212c54 Mon Sep 17 00:00:00 2001 From: Gijs de Jong Date: Thu, 7 Sep 2023 11:11: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 5865373adabf4492a0b72e9bb54dcd20a12d0b2b PiperOrigin-RevId: 563488388 --- .../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 eff5d443bc91..51cf7c94f95b 100644 --- a/check_api/src/main/java/com/google/errorprone/BaseErrorProneJavaCompiler.java +++ b/check_api/src/main/java/com/google/errorprone/BaseErrorProneJavaCompiler.java @@ -224,14 +224,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 39648b552df1..0a1a1899c8f7 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 {}