Skip to content

Commit e0a62ac

Browse files
committed
Add clippy_utils::is_mutable()
1 parent a8b1782 commit e0a62ac

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

clippy_utils/src/lib.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3675,3 +3675,24 @@ pub fn expr_requires_coercion<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) -
36753675
_ => false,
36763676
}
36773677
}
3678+
3679+
/// Returns `true` if `expr` designates a mutable static, a mutable local binding, or an expression
3680+
/// that can be owned.
3681+
pub fn is_mutable(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
3682+
if let Some(hir_id) = path_to_local(expr)
3683+
&& let Node::Pat(pat) = cx.tcx.hir_node(hir_id)
3684+
{
3685+
matches!(pat.kind, PatKind::Binding(BindingMode::MUT, ..))
3686+
} else if let ExprKind::Path(p) = &expr.kind
3687+
&& let Some(mutability) = cx
3688+
.qpath_res(p, expr.hir_id)
3689+
.opt_def_id()
3690+
.and_then(|id| cx.tcx.static_mutability(id))
3691+
{
3692+
mutability == Mutability::Mut
3693+
} else if let ExprKind::Field(parent, _) = expr.kind {
3694+
is_mutable(cx, parent)
3695+
} else {
3696+
true
3697+
}
3698+
}

0 commit comments

Comments
 (0)