-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,7 +91,7 @@ use std::sync::{Mutex, MutexGuard, OnceLock}; | |
|
||
use clippy_config::types::DisallowedPath; | ||
use itertools::Itertools; | ||
use rustc_ast::ast::{self, LitKind, RangeLimits}; | ||
use rustc_ast::ast::{self, BinOpKind, LitKind, RangeLimits}; | ||
use rustc_data_structures::fx::FxHashMap; | ||
use rustc_data_structures::packed::Pu128; | ||
use rustc_data_structures::unhash::UnhashMap; | ||
|
@@ -2791,16 +2791,46 @@ impl<'tcx> ExprUseCtxt<'tcx> { | |
.position(|arg| arg.hir_id == self.child_id) | ||
.map_or(0, |i| i + 1), | ||
), | ||
ExprKind::Field(_, name) => ExprUseNode::FieldAccess(name), | ||
ExprKind::Field(_, name) => ExprUseNode::FieldAccess(use_expr.hir_id, name), | ||
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)] | ||
let idx = if lhs.hir_id == self.child_id { 0 } else { 1 }; | ||
ExprUseNode::Assign(idx) | ||
}, | ||
ExprKind::AssignOp(op, lhs, rhs) => { | ||
debug_assert!(lhs.hir_id == self.child_id || rhs.hir_id == self.child_id); | ||
#[allow(clippy::bool_to_int_with_if)] | ||
let idx = if lhs.hir_id == self.child_id { 0 } else { 1 }; | ||
ExprUseNode::AssignOp(op.node, idx) | ||
}, | ||
_ => ExprUseNode::Other, | ||
}, | ||
_ => ExprUseNode::Other, | ||
} | ||
} | ||
|
||
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, | ||
} | ||
} | ||
Comment on lines
+2814
to
+2829
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using 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 commentThe reason will be displayed to describe this comment to others. Learn more. Oh, thanks for pointing those out, I'll check it out.
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 commentThe reason will be displayed to describe this comment to others. Learn more. Field accesses through a pointer don't. e.g. |
||
} | ||
|
||
/// The node which consumes a value. | ||
#[derive(Debug)] | ||
pub enum ExprUseNode<'tcx> { | ||
/// Assignment to, or initializer for, a local | ||
LetStmt(&'tcx LetStmt<'tcx>), | ||
|
@@ -2817,9 +2847,13 @@ pub enum ExprUseNode<'tcx> { | |
/// The callee of a function call. | ||
Callee, | ||
/// Access of a field. | ||
FieldAccess(Ident), | ||
FieldAccess(HirId, Ident), | ||
/// Borrow expression. | ||
AddrOf(ast::BorrowKind, Mutability), | ||
/// Assignment. | ||
Assign(usize), | ||
/// Assignment with an operator. | ||
AssignOp(BinOpKind, usize), | ||
Comment on lines
+2853
to
+2856
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
Other, | ||
} | ||
impl<'tcx> ExprUseNode<'tcx> { | ||
|
@@ -2896,7 +2930,13 @@ impl<'tcx> ExprUseNode<'tcx> { | |
let sig = cx.tcx.fn_sig(id).skip_binder(); | ||
Some(DefinedTy::Mir(cx.tcx.param_env(id).and(sig.input(i)))) | ||
}, | ||
Self::LetStmt(_) | Self::FieldAccess(..) | Self::Callee | Self::Other | Self::AddrOf(..) => None, | ||
Self::LetStmt(_) | ||
| Self::FieldAccess(..) | ||
| Self::Callee | ||
| Self::Other | ||
| Self::AddrOf(..) | ||
| Self::Assign(_) | ||
| Self::AssignOp(..) => None, | ||
} | ||
} | ||
} | ||
|
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:
Is there a reason why you rather expect this lint over taking the suggestion?