-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
add unnecessary_self_imports lint #7072
Conversation
r? @camsteffen (rust-highfive has picked a reviewer for you, use r? to override) |
6b5d46a
to
76db21e
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.
The functions you implement to check if something is in scope seem really resource intensive to me. Have you looked in rustc
if there is a query to determine if a item is in scope?
TL;DR: Implement this lint on the AST, not the HIR. It'll be way easier. I just noticed that you can't really implement this lint on the HIR, since the information if a use path::to::module::{};
use path::to::module; However on the AST you should be able to determine if it is an import of just AST `use` expansion
You'll then have the problem to determine if the imported module is already there in form of another item (at least I think that is what you try to do with the other 2 functions?). But this isn't really a problem. |
It's a bit more complicated.
So changing to the latter may cause "b is defined multiple times". (playground) I think a late pass will be necessary to detect this. And |
Checking another case which shouldn't be a concern for this lint: Playground So you only have to check which items are loaded in the current module. So instead of checking every (That is on the AST. I really don't think we can implement this lint on the HIR reliably) |
Couldn't |
Oh yeah, your right. The item could still be in scope from the surrounding scope... I'm starting to think that there is no feasible way to implement this. Your method works, but I hope that you agree that going over all defined items multiple times is too performance heavy. Determining what would happen if we change code without recompiling is just really hard in general. I see two ways going forward:
|
I'm not following... |
I think @flip1995 is right. On the HIR, this lint is too resource intensive and it might not be possible on the AST. I don't know if it's worthwhile to have. |
Hrrrmmm. Yeeahh. mrr |
Or maybe not... Playground Anyway, this is way too scuffed to figure out what works and what doesn't when the |
Yes too scuffed! I can imagine the restriction lint begin useful as in "if removing |
Good point. I'll change this to a restriction lint and put a warning in the documentation. |
cf4223e
to
cf2b2b9
Compare
I think we should also
|
cf2b2b9
to
83fa54e
Compare
/// **Why is this bad?** In most cases, this can be written much more cleanly by omitting `self`. | ||
/// | ||
/// **Known problems:** Sometimes removing `::{self}` will break code (https://github.com/rust-lang/rustfmt/issues/3568). | ||
/// This lint should not be permanently enabled. |
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.
It's not necessarily wrong to enable the lint for a project.
83fa54e
to
b9cf008
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.
Just some nits.
e597832
to
88d60ed
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.
One more minor suggestion that you can take or leave. Otherwise looks good!
Thanks! I like that. |
88d60ed
to
224881b
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.
LGTM now. @camsteffen if you're also ok with this PR, please go ahead and merge it.
@ebobrow Thanks for all your work on it, even though you had to delete most of it again. But now we learned what information about imports are available where, which will help us advising on issues about imports in the future,, which is definitely a win.
@bors r+ |
📌 Commit 224881b has been approved by |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
fixes #6552
changelog: add
unnecessary_self_imports
lint