-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[filter_next
]: suggest making binding mutable if it needs to be
#11016
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? @dswij (rustbot has picked a reviewer for you, use r? to override) |
&& let Some(hir::Node::Pat(pat)) = cx.tcx.hir().find(id) | ||
&& let hir::PatKind::Binding(BindingAnnotation(_, Mutability::Not), _, ident, _) = pat.kind | ||
{ | ||
(Applicability::Unspecified, Some((pat.span, ident))) |
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 can no longer be a machine applicable suggestion if it needs to be made mutable because of cases like these:
let (Ok(f) | Err(f)) = ...;
f.filter(...).next();
There are multiple bindings that would need to be made mutable, but cx.tcx.hir().find(id)
only returns one pattern.
iter_skip_next
is doing this as well, so I think having it be unspecified is fine.
rust-clippy/clippy_lints/src/methods/iter_skip_next.rs
Lines 23 to 35 in ce0a48a
if_chain! { | |
if let Some(id) = path_to_local(recv); | |
if let Node::Pat(pat) = cx.tcx.hir().get(id); | |
if let PatKind::Binding(ann, _, _, _) = pat.kind; | |
if ann != BindingAnnotation::MUT; | |
then { | |
application = Applicability::Unspecified; | |
diag.span_help( | |
pat.span, | |
format!("for this change `{}` has to be mutable", snippet(cx, pat.span, "..")), | |
); | |
} | |
} |
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 gues we can walk the parent to find all the bindings, but that's also not simple. This is good enough for now
Can you help to review this? @blyxyas |
Yep, will review it! 🤠 |
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 mini-nit, but it's very good.. Thanks ❤️!
use rustc_errors::Applicability; | ||
use rustc_hir as hir; | ||
use rustc_lint::LateContext; | ||
use rustc_span::sym; | ||
|
||
use super::FILTER_NEXT; | ||
|
||
fn path_to_local(expr: &hir::Expr<'_>) -> Option<hir::HirId> { |
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.
We have a path_to_local
helper in clippy_utils
. Can we use that here?
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.
If I remember correctly, the existing path_to_local
in utils doesn't "work" if the local is reached through field accesses, so for example it would return false
for a.b[0]
if given the id of the local a
, but we need it to return true
, because we use this logic to figure out if a binding needs to be made mutable, and a.b[0].find(..)
requires a
be mutable. Maybe this could be better named or have a comment explaining it.
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_utils::path_to_local
is too primitive in my opinion. It only handles the case where the expression is a Path, and its Res is a Res::Local
. Maybe we should modify it and make it handle this new cases.
&& let Some(hir::Node::Pat(pat)) = cx.tcx.hir().find(id) | ||
&& let hir::PatKind::Binding(BindingAnnotation(_, Mutability::Not), _, ident, _) = pat.kind | ||
{ | ||
(Applicability::Unspecified, Some((pat.span, ident))) |
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 gues we can walk the parent to find all the bindings, but that's also not simple. This is good enough for now
☔ The latest upstream changes (presumably #11055) made this pull request unmergeable. Please resolve the merge conflicts. |
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, Thanks! ❤️
Thank you! @bors r=blyxyas,dswij |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
Fixes #10029
changelog: [
filter_next
]: suggest making binding mutable if it needs to be and adjust applicability