Skip to content

Lint for unnecessary trait bounds #8366

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

Closed
huonw opened this issue Aug 7, 2013 · 4 comments
Closed

Lint for unnecessary trait bounds #8366

huonw opened this issue Aug 7, 2013 · 4 comments
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one. E-hard Call for participation: Hard difficulty. Experience needed to fix: A lot.

Comments

@huonw
Copy link
Member

huonw commented Aug 7, 2013

It's easy to accidentally leave an unnecessary (e.g.) Clone bound when refactoring a function, making the function less generic/useful than it otherwise could be.

This would warn for something like:

// warning: unnecessary trait bound. No method from `Clone` is used.
fn foo<T: Clone>(x: T) -> T { 
    x
}

It would presumably have to be very careful about super-traits i.e. using a function from a super-trait, but not one from the listed trait bound, would either not warn at all, or suggest that the constraints could be weakened to that trait (handling multiple supertraits would be even trickier, i.e. if a function from 2 different super-traits was called, is it sensible/useful to suggest weakening to a combination of those two traits?)

Presumably a far-future feature, if at all.

@thestinger
Copy link
Contributor

Sometimes the bounds are there only as a restriction, like most uses of Send and Freeze. I guess this would only apply to non-kinds, but that will get blurrier when we allow inheritance from kinds.

@huonw
Copy link
Member Author

huonw commented Aug 7, 2013

I was going to mention that, but clearly forgot. Presumably it could have some smarts like that, or just require #[allow(unused_bounds)] on those functions (less nice).

@bblum
Copy link
Contributor

bblum commented Aug 14, 2013

Any function that does unsafe code inside could be assumed to need all declared trait bounds. That would cover the cases of ARC and pipes and so forth.

@alexcrichton
Copy link
Member

I'm going to close this for now. This seems very similar to unsafe on a function that doesn't actually do anything unsafe. The trait bound may not be used, but it may be a bound for different reasons that the compiler cannot know about.

flip1995 pushed a commit to flip1995/rust that referenced this issue Feb 10, 2022
Don't lint `ptr_arg` for `&mut _` types in trait items

fixes rust-lang#8366

changelog: Don't lint `ptr_arg` for `&mut _` types in trait items
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one. E-hard Call for participation: Hard difficulty. Experience needed to fix: A lot.
Projects
None yet
Development

No branches or pull requests

4 participants