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
proposal: cmd/vet: report unused type parameters #50914
Comments
Thanks @seankhliao! |
That would make it difficult to write code like type Zero[T any] struct{}
func (Zero[T]) Get() T {
var zero T
return zero
} That's a fairly simple version but one can imagine more complicated ones. For functions, it's possible to imagine having a family of functions that all share the same type parameters, even if they don't all use them. Giving an error for unused local variables is helpful because they normally indicate a bug in the program. That is less clear for unused type parameters. |
Thanks for the quick reply @ianlancetaylor!
That makes sense. I can see that it would be annoying to get an error from vet in such cases. And as you say, there's probably other scenarios where reporting an error would have a negative effect. I don't quite see how the example you posted with a type is the same kind as with the function example above. You don't have to, of course, but could you explain why this is similar? I would think that there are no unused type parameters in the method As for this issue, I was probably too quick! I'm fine with this being closed. |
I think in the spirit of the suggestion for named types with methods, the vet check would likely require checking if the type parameter type is used in the underlying type or within any method for the named type.
AFAICT a pattern this does rule out is to use generic functions for type assertions for tilde and bar types.
Seems somewhat reasonable? |
This proposal seems analogous to reporting unused function arguments, which we also don't do today. I don't think we should report on unused type parameters going forward, either. It may be that the implementation of a struct or function initially depends on all of its type parameters but is then refactored such that one or more of the parameters is no longer needed. However, the extra parameter cannot be removed without breaking compatibility: existing users may already instantiate it explicitly. This seems like a fine check for more aggressive linters, but I think the false-positive rate will be too high in the long run for |
Based on the discussion above, this proposal seems like a likely decline. |
No change in consensus, so declined. |
What version of Go are you using (
go version
)?What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
File
main.go
containsand run
go1.18beta1 vet main.go
.What did you expect to see?
Something like "
f: type parameter T is not used
"What did you see instead?
No output; exit code 0 indicating no issues found in file.
Rationale
go vet
doesn't report on unused arguments.This is nice, because you may need to conform to a certain interface even though you don't use all arguments.
In this example program, we specify a type parameter, but we don't apply the argument to anything.
This is different, in my opinion, because the type parameter list is simply redundant in this case.
Implementing this may help for example where a user has started out implementing a function as generic, decided not to, but forgot to remove the type parameter list.
I apologize if this has been discussed already! I didn't find anything on here.
The text was updated successfully, but these errors were encountered: