-
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 manual_inspect
to consider mutability
#13609
base: master
Are you sure you want to change the base?
Conversation
/// Assignment. | ||
Assign(usize), | ||
/// Assignment with an operator. | ||
AssignOp(BinOpKind, usize), |
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.
No need to capture this. The lint already checks if the binding in the closure is mutable so x = something
would fail to compile.
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 lint already checks if the binding in the closure is mutable so
x = something
would fail to compile.
Do you mean this line?
&& let PatKind::Binding(BindingMode(ByRef::No, Mutability::Not), arg_id, _, None) = param.pat.kind
This checks the mut pattern, right. So Assign
is indeed not needed for this lint, but given that it's a function in clippy_utils
, I'd like to make it take all cases into account as much as possible. And AssignOp
is still needed for something like x += y
.
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.
x += y
also wouldn't compile. Once you fix use_mutability
there will be no uses of these variants left (use_node
and expr_use_ctxt
shouldn't be used there).
pub fn use_mutability(&self, cx: &LateContext<'tcx>) -> Mutability { | ||
match self.use_node(cx) { | ||
ExprUseNode::FieldAccess(parent, _) => { | ||
let parent = cx.tcx.hir().expect_expr(parent); | ||
expr_use_ctxt(cx, parent).use_mutability(cx) | ||
}, | ||
ExprUseNode::AssignOp(_, 0) | ExprUseNode::Assign(0) => Mutability::Mut, | ||
ExprUseNode::AddrOf(_, mutbl) => mutbl, | ||
ExprUseNode::FnArg(_, _) | ExprUseNode::MethodArg(_, _, _) => { | ||
let child_expr = cx.tcx.hir().expect_expr(self.child_id); | ||
let ty = cx.typeck_results().expr_ty_adjusted(child_expr); | ||
ty.ref_mutability().unwrap_or(Mutability::Not) | ||
}, | ||
_ => Mutability::Not, | ||
} | ||
} |
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.
Using expr_use_ctxt
here is wrong since it allows moves to occur. Just use parent_iter
.
You'll need to handle mutable borrow or deref adjustments, mutable borrows, mutable derefs, mutable indexing, and assignments. For indexing and derefs you can't tell at the expression so you have to keep walking up the tree. For adjustments, don't go by the resulting type, but walk the adjustments themselves. Mutable auto-deref doesn't always result in a reference after all adjustments take place.
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, thanks for pointing those out, I'll check it out.
Mutable auto-deref doesn't always result in a reference after all adjustments take place.
Would you mind explaining it further? Under what circumstances would adjustments not work? So that I can write a test case to make sure of that.
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.
Field accesses through a pointer don't. e.g. x.y
where x
is a reference will have have a deref adjustment, but not a borrow adjustment.
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.
Tiny NIT, besides the comments from @Jarcho. Otherwise, it looks good to me :D
ExprKind::AddrOf(kind, mutbl, _) => ExprUseNode::AddrOf(kind, mutbl), | ||
ExprKind::Assign(lhs, rhs, _) => { | ||
debug_assert!(lhs.hir_id == self.child_id || rhs.hir_id == self.child_id); | ||
#[allow(clippy::bool_to_int_with_if)] |
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.
NIT:
#[allow(clippy::bool_to_int_with_if)] | |
#[expect(clippy::bool_to_int_with_if)] |
Is there a reason why you rather expect this lint over taking the suggestion?
☔ The latest upstream changes (presumably #13639) made this pull request unmergeable. Please resolve the merge conflicts. |
Fixes #13185.
In this PR, we fix the false positives. I left some FIXME cases, it would be nice if they could be supported in the future.
changelog: [
manual_inspect
]: Fix to consider mutability