-
Notifications
You must be signed in to change notification settings - Fork 134
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
Rule to detect map.get(wrongType) #1025
Comments
👍 I'll take a look |
I think this is already implemented: https://errorprone.info/bugpattern/CollectionIncompatibleType I wrote the following code and error-prone refused to compile it: ImmutableMap<String, String> of = ImmutableMap.of("foo", "bar");
System.out.println(of.get(1));
|
I don't think interface FirstIface {}
interface SecondIface {}
String read(Map<FirstIface, String> map, SecondIface key) {
// it's possible the SecondIface happens to implement FirstIface and this works correctly,
// but not likely. Idea warns in this case. It would be better to leverage the type system.
return map.get(key);
} |
To clarify, we enforce CollectionIncompatibleType in the project where @akroy discovered the bug which prompted this issue! |
Maybe it’d make sense for the fix should be pushed upstream, then?
|
Absolutely, though I’ve had less engagement on the upstream error-prone project than others like guava, conscrypt, etc.
…-ck
On Nov 6, 2019, at 8:39 AM, Mark Elliot ***@***.***> wrote:
Maybe it’d make sense for the fix should be pushed upstream, then?
—
You are receiving this because you were assigned.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
…lection usage This check is an improvement over `CollectionIncompatibleType` because it validates that values exist in the same type hierarchy, where it could theoretically be possible the input implements the collection type, but the type system doesn't have enough information to be confident. This check allows both subtypes and supertypes, but does not check for shared supertypes, which are not common.
What happened?
It's currently easy to write code like:
and not realize you have a bug. This is particularly insidious with things like wrapped strings, where you
get
with the right "thing", you just have to wrap or unwrap it.For example, see commit 331070 in the major internal repo I work on.
What did you want to happen?
Errorprone to flag that this can't work. IntelliJ does, so I expect this is doable.
CC @carterkozak
The text was updated successfully, but these errors were encountered: