-
Notifications
You must be signed in to change notification settings - Fork 1.6k
new lint: needless traits in scope #10503
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
Conversation
r? @llogiq (rustbot has picked a reviewer for you, use r? to override) |
2642bb4
to
fb4bacb
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// ``` | ||
#[clippy::version = "1.69.0"] | ||
pub NEEDLESS_TRAITS_IN_SCOPE, | ||
pedantic, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be a restriction lint. It's nothing that should be frowned upon in normal Rust code and makes the code longer, but it could bring benefits in certain situations (those you mentioned).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, better than pedantic
. Fixed in 02db985.
/// It also helps identify the traits in `use` statements. | ||
/// | ||
/// ### Why is this bad? | ||
/// This needlessly brings a trait in trait's namespace. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// This needlessly brings a trait in trait's namespace. | |
/// This needlessly brings a trait in trait's namespace, where it could | |
/// shadow other things. This is not really a problem, this lint is just | |
/// for those who like to keep things tidy. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in ef7fac2, thank you for the improvement.
pedantic, | ||
"trait is needlessly imported in trait's namespace, and can be anonymously imported" | ||
} | ||
declare_lint_pass!(NeedlessTraitsInScope => [NEEDLESS_TRAITS_IN_SCOPE]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'll want to write your own struct so you can impl_lint_pass!
here instead. Then you can add a Visitor
to your LintPass that you use on check_crate
to run through the code and collect any trait use. Please leave a comment or ping me on zulip if you need help with that.
☔ The latest upstream changes (presumably #10481) made this pull request unmergeable. Please resolve the merge conflicts. |
Co-authored-by: Ruby Lazuli <[email protected]>
If you need further assistance for implementing the visitor, feel free to ping us here or on zulip. |
Thanks for getting back to me. To be honest, I've had little time since I opened the PR to explore the visitor solution. But the little I looked, I'm sure some help might help me. Let met start with what I (think I) understood. I also understand that instead of
I can write the following
Now, it's fuzzy what I'm missing, but I'll try to make some questions:
In any case, I'll probably be slow since I don't have a lot of personal time these days to work on this. I hope it's not a problem |
No problem. That's a good start; everything you wrote seems correct to me. Now the way using a Visitor works is: in For the visitor itself, you'll want to override the |
I tried implementing the I started implementing Parsing through the documentation, I actually found a Note: I usually do not like pushing broken code, but tell me if that can help. |
Sorry, my bad. I hadn't looked into the intravisit code in a while. |
Hi, thanks for the help. I actually didn't mention it in my previous comment, but I did already explore After digging, I'll try to explore the following option:
That said, this does not look like a general solution. If that does work, I might also need to explore other |
You can just implement |
Hi @woshilapin , just a friendly reminder that this PR is being inactive for a while now (just pass its anniversary 😄). So, if you're still interested in working on this, plz let us know. And if you have any further questions, don't hesitate to ask people in this zulip channel, cheers! |
@J-ZhengLi Thank you for following-up. I don't really plan to work on it anymore, I don't really have the time. And the complexity of the task is a bit more than I had in mind when I started this. I'll close it. Feel free to re-open if you prefer to keep it open. |
For those interested in this work, someone else actually did land a new clippy lint in 1.83 (see #13322). |
changelog: [
needless_traits_in_scope
]: trait is needlessly imported in trait's namespaceThis is my first contribution to
rust-clippy
.I propose a lint to find traits that are imported with a
use
but trait's name is not used afterwards (only methods). Importing traits withas _
gives 2 advantages:struct
) in theuse
statements>I do not consider this PR finished, because I'm looking for first feedbacks... and a little help for the
FIXME
in the UI test case (how to check the trait's name is actually not used in the rest of the scope).And don't worry about telling me the lint is dumb, the journey trying stuff with
rust-clippy
so far has been enlightening anyway.