-
Notifications
You must be signed in to change notification settings - Fork 746
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ErrorProneInjector incorrectly picks up the no-args constructor #3931
Comments
@ksobolew this sounds similar to something that initially puzzled me when upgrading to Error Prone 2.19+. Then I realized that just annotating the |
You're right @Stephan202, there's also some support for diff --git a/check_api/src/main/java/com/google/errorprone/scanner/ErrorProneInjector.java b/check_api/src/main/java/com/google/errorprone/scanner/ErrorProneInjector.java
index 7a3c79cee5..afd2570d87 100644
--- a/check_api/src/main/java/com/google/errorprone/scanner/ErrorProneInjector.java
+++ b/check_api/src/main/java/com/google/errorprone/scanner/ErrorProneInjector.java
@@ -18,6 +18,7 @@ package com.google.errorprone.scanner;
import static com.google.common.collect.Lists.reverse;
import static java.util.Arrays.stream;
+import static java.util.Comparator.comparingInt;
import static java.util.stream.Collectors.joining;
import com.google.common.collect.ClassToInstanceMap;
@@ -106,7 +107,7 @@ public final class ErrorProneInjector {
return stream(clazz.getDeclaredConstructors())
.filter(predicate)
.map(c -> (Constructor<T>) c)
- .findFirst();
+ .max(comparingInt(Constructor::getParameterCount));
}
private static String printPath(List<Class<?>> path) { |
Check; I suppose that's something for @graememorgan to have an opinion on :) |
Hah, I think I thought of that edge case when I wrote it, but decided it wouldn't matter for some reason. :) I would expect the inject constructor to be annotated
Could you provide a minimal example which is a problem? |
IIRC ~any test that provides a non-vacuous set of flags using One example: git clone [email protected]:PicnicSupermarket/error-prone-support.git
cd error-prone-support
sed -i '/Inject/d' error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/RedundantStringConversion.java
mvn clean install -Dverification.skip -DskipTests
mvn test -Dtest='RedundantStringConversionTest#identificationOfCustomConversionMethod' -pl error-prone-contrib |
But can it be fixed by just including the (I'm not suggesting not fixing this to match the original behaviour, but chucking some |
Indeed, at least in our project adding |
Yes, it can be fixed this way. I originally didn't realize that, so the severity is much lower than I thought. Now it's just mostly a source of confusion :) |
The warning-level |
I've mailed a change internally to favour an EPF constructor over a no-arg one. |
I was a bit lazy and figured this edge case wouldn't matter too much, but it turns out to annoy people. Fixes external #3931 PiperOrigin-RevId: 535251926
I was a bit lazy and figured this edge case wouldn't matter too much, but it turns out to annoy people. Fixes external #3931 PiperOrigin-RevId: 535251926
I was a bit lazy and figured this edge case wouldn't matter too much, but it turns out to annoy people. Fixes external #3931 PiperOrigin-RevId: 535569770
Should be fixed now; give me a shout about any further problems. |
This MR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [com.google.errorprone:error_prone_core](https://errorprone.info) ([source](https://github.com/google/error-prone)) | | minor | `2.19.1` -> `2.20.0` | | [com.google.errorprone:error_prone_annotations](https://errorprone.info) ([source](https://github.com/google/error-prone)) | compile | minor | `2.19.1` -> `2.20.0` | --- ### Release Notes <details> <summary>google/error-prone</summary> ### [`v2.20.0`](https://github.com/google/error-prone/releases/tag/v2.20.0): Error Prone 2.20.0 [Compare Source](google/error-prone@v2.19.1...v2.20.0) Changes: - This release is compatible with early-access builds of JDK 21. New Checkers: - [`InlineTrivialConstant`](https://errorprone.info/bugpattern/InlineTrivialConstant) - [`UnnecessaryStringBuilder`](https://errorprone.info/bugpattern/UnnecessaryStringBuilder) - [`BanClassLoader`](https://errorprone.info/bugpattern/BanClassLoader) - [`DereferenceWithNullBranch`](https://errorprone.info/bugpattern/DereferenceWithNullBranch) - [`DoNotUseRuleChain`](https://errorprone.info/bugpattern/DoNotUseRuleChain) - [`LockOnNonEnclosingClassLiteral`](https://errorprone.info/bugpattern/LockOnNonEnclosingClassLiteral) - [`MissingRefasterAnnotation`](https://errorprone.info/bugpattern/MissingRefasterAnnotation) - [`NamedLikeContextualKeyword`](https://errorprone.info/bugpattern/NamedLikeContextualKeyword) - [`NonApiType`](https://errorprone.info/bugpattern/NonApiType) Fixes issues: [#​2232](google/error-prone#2232), [#​2243](google/error-prone#2243), [#​2997](google/error-prone#2997), [#​3301](google/error-prone#3301), [#​3843](google/error-prone#3843), [#​3903](google/error-prone#3903), [#​3918](google/error-prone#3918), [#​3923](google/error-prone#3923), [#​3931](google/error-prone#3931), [#​3945](google/error-prone#3945), [#​3946](google/error-prone#3946) **Full Changelog**: google/error-prone@v2.19.1...v2.20.0 </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Enabled. ♻ **Rebasing**: Whenever MR is behind base branch, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this MR and you won't be reminded about these updates again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box --- This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNC4yNC4wIiwidXBkYXRlZEluVmVyIjoiMzQuMjQuMCJ9-->
The logic of determining the constructor of a checker class changed in commit 1c3c09f. Previously it explicitly checked for a constructor with a single parameter of type
ErrorProneFlags
; now it looks for a constructor with all parameters having this type. The issue is that the condition used -allMatch
- also returnstrue
if there are no parameters at all, so the no-args constructor also matches the test.The impact is that when I have a test for a checker which needs to take additional flags, the
CompilationTestHelper
uses the wrong constructor to create the checker instance, making it impossible to pass flags in a test.The text was updated successfully, but these errors were encountered: