-
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
fix needless_borrow
suggestion
#7105
Conversation
r? @camsteffen (rust-highfive has picked a reviewer for you, use r? to override) |
8b641ae
to
484a2af
Compare
I think that would be a good idea since
Consider the case where you have a variable |
I'm fine with splitting this between patterns and expressions. I don't see it as particularly beneficial, but I also don't see it as particularly harmful to do so either. In both cases a value is borrowed when there was no need to do so. References to other copy types aren't linted at all right now, but would definitely fit the current name. For the pattern part of this lint, however, it currently triggers on any reference-to-reference created, even when it's actually needed (see the final example). It's this that I believe should definitely be split out as the borrow isn't needless. The choice of where the borrow is created is more stylistic than anything. The split can be done in a different PR anyways, This is currently in the nursery, and this is behaviour that already existed. |
I don't think that case should lint at all. That is, only lint if every usage of the binding has an implicit or explicit deref. |
Hmm I see now the value in making a separate lint for that. Like |
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 a quick comment
The case where the reference is needed has been split into it's own lint. Should the pattern part of needless borrow still be separated? Would probably be called something like |
96ee5a6
to
d6ab6f5
Compare
I'm in favor. But I'd rather split lints in a separate PR. |
Sounds good. |
ping from triage @Jarcho. There seems to be some fixes left to be done. Can you have any updates on this? |
0bcb17a
to
5ee2fc7
Compare
Ended up requiring the pattern to not come from a macro. There's a lot of ways for that to be a false positive. |
33cd006
to
bc84d30
Compare
Implementation is looking good. Nice work @Jarcho! I just want to consider the lint categories before merging... I think this resolves the only major bug for needless_borrow, so it can be moved out of nursery in this PR? I'm a bit worried about how noisy the lint will be, but otherwise it seems to fit style since it strictly simplifies code. So I think it's okay to move it to style if there are no other concerns. For ref_binding_to_reference, I'm just not sure if it should warn-by-default. For example, the lint will suggest - ref x => x,
+ x => &x, The change seems pedantic. But on the other hand, I like the idea of banning double references by default. It gives me peace of mind to know that code doesn't have any double referenced bindings. But maybe that's just me worrying about a non-issue. That is - I am pedantic? Would like to hear other opinions. Are double referenced bindings decidedly bad style? CC @rust-lang/clippy |
For About the |
I agree with @flip1995 on both counts here |
27da6a1
to
9132e4d
Compare
I thought I did put it in
|
Okay. Please squash commits. |
9132e4d
to
7f40dc0
Compare
☔ The latest upstream changes (presumably #7253) made this pull request unmergeable. Please resolve the merge conflicts. |
The spans given for derived traits used to not indicate they were from a macro expansion.
Suggest changing usages of ref bindings to match the new type Split out some cases into new lint `ref_binding_to_reference`
7f40dc0
to
6d4dc35
Compare
Thanks! @bors r+ |
📌 Commit 6d4dc35 has been approved by |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
fixes: #2610
While I'm working on this, should needless_borrow be split into two? One lint for expressions and another for patterns. In expression it only lints when the compiler inserts a dereference, but for patterns it's whenever a double reference is created. I think at least the case where a double reference is needed should be split into a new lint as it's not 'needless', it can just be done without a ref binding.
For illustration:
changelog: Improve the suggestion for
needless_borrow
in patterns to change all usage sites as needed.changelog: Add lint
ref_binding_to_reference