Skip to content
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

idea: check proper use of declaration-site type variance #3711

Open
vlsi opened this issue Jan 10, 2023 · 2 comments
Open

idea: check proper use of declaration-site type variance #3711

vlsi opened this issue Jan 10, 2023 · 2 comments

Comments

@vlsi
Copy link

vlsi commented Jan 10, 2023

See:

Currently, Java requires use-site type variance, so if someone has Function<IN, OUT> method parameter, it should rather be Function<? super IN, ? extends OUT>.

Unfortunately, it is not easy to notice that ? super and ? extends is missing, so it would be nice if there was a tool that could detect missing variance and suggest adding it.

The list of well-known classes could be hard-coded within ErrorProne: Function, Predicate, BiFunction, Consumer, Supplier, and so on.

Here a recent case:

WDYT?

See also:

/cc @Stephan202

@kevinb9n
Copy link
Contributor

+1 there is a well-known set of known-co(ntra)variant types and, at least for public methods, skipping the wildcard is virtually never okay.

An exception could be made for Predicate<Object>, but ironically, adopting JSpecify-type nullness analysis messes that one up! (You do need Predicate<? super Object> sometimes, since Object is no longer the top type, @Nullable Object is).

@graememorgan
Copy link
Member

I really like this idea. Perhaps we'd want to exempt private method arguments for the sake of noise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants