-
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
New Lint: needless_deref
#7577
New Lint: needless_deref
#7577
Conversation
r? @Manishearth (rust-highfive has picked a reviewer for you, use r? to override) |
Thinking back I think there are lots of cases where this doesn't work, I find myself explicitly derefing pretty often. @rust-lang/clippy what do y'all think about adding this? Maybe only nursery? |
I think there are two distinct things linted here:
The first case could be The second case is what we're mainly concerned with here, I think. I would maybe call this |
I didn't lint on |
r? @camsteffen (might not have time to go through this rn) |
I do have a version of this mostly working in any case auto-deref works (except method receivers). Fixing it has been on my list of things to do for a while. There's a version of it in #6837. |
@lengyijun Can you clarify what you mean by that? I don't think we should have a lint for coercable derefs without also having a lint for noop derefs. Simpler case first. The implementations should probably be together.
@Jarcho can you try to summarize what is different between this PR and what you have? Then we can decide how to reconcile this. Strictly speaking, since there is no open issue assigned to you (AFAIK), I think it is fair for someone else to open a PR. Perhaps we can move forward with this and you can add enhancements afterwards? |
The only difference is in the scope of what is checked for. This is limited to specific expressions (
The Be careful with linting fn foo(x: &mut &u32) {
if **x == 0 {
*x = &1;
}
}
fn main() {
let mut x = &0;
foo(&mut &*x);
assert_eq!(*x, 0);
foo(&mut x);
assert_eq!(*x, 1);
} Also if |
I see, thanks!
Yes that is what I was alluding to. You're right - it's a minor, subtle, non-correctness issue. But it is something that people (rightly) care about. |
In
So I give up to lint |
Luckily, I exclude this problem because this pr doesn't deal with function with generic parameters. though I didn't have this case in mind. |
What is the type of |
Yes, it's a reference. |
Can you reproduce in a playground? Did you remove both the ref and deref operators? |
Oh sorry I'm just now understanding - the lint is triggering on the expanded |
@camsteffen Thanks. My plan to
|
Sounds good. I don't think the function parameters part is necessary. You can just look for a |
After some experiments, I found I couldn't deal with it unfortunately. |
changelog: [needless_deref]
changelog: fixes #234
This pr only lints on function(no generic type) parameters. It only includes two cases: