-
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 used_underscore_binding lint #499
Add used_underscore_binding lint #499
Conversation
Add macro checking, and only lint for single leading underscores
One thing I noticed is that this lint, as it currently stands, is not quite compatible with the
Here, we consider the variable |
Looks good so far. I'd like to do something other than checking for |
/// **Known problems:** This lint's idea of a "used" variable is not quite the same as in the | ||
/// built-in `unused_variables` lint. For example, in the following code | ||
/// ``` | ||
/// fn foo(_y: u32) -> u32) { |
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 should be y
, not _y
Okay, I made @oli-obk's suggested changes. I have a couple questions about @Manishearth's comments:
I think we should still only lint single underscores, but I agree it would be more satisfying to have a better check here. Is there some way to check to see if something appears in a macro definition vs. in its expansion? I think it would make sense to lint the use of underscore-prefixed bindings within a macro definition, but not within its expansion.
I think we probably want to do something more advanced than just checking to make sure the binding never appears on the LHS of an
we probably want to lint, but |
Exactly! I was saying that we change the heuristics to:
All other Exprs "use" the variable, so we should lint in that case. The built in analysis is in librustc/middle/liveness.rs (It's a hardwired lint), but IIRC it's intertwined with other analyses, so it's harder to figure out. If you want to try ExprUseVisitor, you basically create a Delegate (see the escape analysis lint) impl, and check for uses of a variable with the borrow/consume methods. I don't think this is necessary here.
If an Ident's name and hygenic name match then it wasn't created by a macro. I think. This stuff is in flux right now. I'm fine with checking for __ for the scope of this PR, but if you can find something better that works, that would be great 😄 |
Ah okay that makes sense! I'll go implement that.
I'll go make this change, but I think the fact that I didn't already catch that I'm doing this wrong means my test coverage isn't good enough. I expanded test coverage by adding underscore-prefixed functions, structs, enums, and enum variants. All the above were correctly not linted with the code as is, so could you give me an example of something that the code as-is would lint (due to the |
Probably something like |
I added a test for this, and it seems to work with the code as-is. It also seems to work with that check taken out completely. |
Meh, get rid of that check then :)Manishearth/rust-clippy wrote: I added a test for this, and it seems to work with the code as-is. It also seems to work with that check taken out completely. —Reply to this email directly or view it on GitHub. |
Okay, I think the most recent pull requests address all the above issues. If this looks good to you too, I'll go ahead and rebase this all into one commit. |
match parent.node { | ||
ExprAssign(_, ref rhs) => **rhs == *expr, | ||
ExprAssignOp(_, _, ref rhs) => **rhs == *expr, | ||
_ => is_used(cx, &parent) |
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.
Oh I like this way of calculating is_used; smart 😄 !
Add used_underscore_binding lint
Great work! Thanks! |
I think we may want to reuse some of the techniques in other lints. Great work indeed! 👍 |
Implement #488