rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

Lint for unused type parameters on functions

Open mkrasnitski opened this issue 2 years ago • 5 comments

What it does

Similar to extra_unused_lifetimes, checks for unused type parameters on functions and warns that this is likely unintended, suggesting that they be removed. (Note: should this only apply to functions, or are there other places it could be run?)

Lint Name

extra_unused_type_parameters

Category

suspicious

Advantage

  • Calling functions with unused type parameters requires adding a turbofish, since inferring them is impossible. The turbofish itself is useless other than to satisfy the compiler.
  • If refactoring a function with a long signature and removing the use of generics, it's easy to miss removing the corresponding type parameter, especially if the function is pub and isn't used elsewhere. This accidentally exposes downstream users to the author's mistake, which is bad news.

Drawbacks

I can't think of any besides maybe the lint name, which might need some bikeshedding. Also, maybe a extra_unused_generics lint could be introduced that implies both extra_unused_lifetimes and extra_unused_type_params?

Example

pub fn foo<T>(x: u8) {
    // T unused in function body as well
    // ..
}

Could be written as:

pub fn foo(x: u8) {
    // ..
}

mkrasnitski avatar Jul 24 '22 22:07 mkrasnitski

@rustbot claim

macovedj avatar Jul 25 '22 00:07 macovedj

@macovedj If it's alright, I'd like to try implementing this myself (since I'd like to start contributing, also unsure if there can be multiple assignees), would you mind unclaiming?

mkrasnitski avatar Jul 25 '22 00:07 mkrasnitski

@rustbot release-assignment

macovedj avatar Jul 25 '22 00:07 macovedj

I appreciate it!

mkrasnitski avatar Jul 25 '22 01:07 mkrasnitski

@rustbot claim

mkrasnitski avatar Jul 25 '22 01:07 mkrasnitski

@rustbot claim

mkrasnitski avatar Oct 19 '22 00:10 mkrasnitski