2 use crate::visitors::{expr_visitor, expr_visitor_no_bodies};
4 use rustc_hir::intravisit::{self, Visitor};
5 use rustc_hir::HirIdSet;
6 use rustc_hir::{Expr, ExprKind, HirId};
7 use rustc_infer::infer::TyCtxtInferExt;
8 use rustc_lint::LateContext;
9 use rustc_middle::hir::nested_filter;
10 use rustc_middle::mir::FakeReadCause;
12 use rustc_typeck::expr_use_visitor::{Delegate, ExprUseVisitor, PlaceBase, PlaceWithHirId};
14 /// Returns a set of mutated local variable IDs, or `None` if mutations could not be determined.
15 pub fn mutated_variables<'tcx>(expr: &'tcx Expr<'_>, cx: &LateContext<'tcx>) -> Option<HirIdSet> {
16 let mut delegate = MutVarsDelegate {
17 used_mutably: HirIdSet::default(),
20 cx.tcx.infer_ctxt().enter(|infcx| {
34 Some(delegate.used_mutably)
37 pub fn is_potentially_mutated<'tcx>(variable: HirId, expr: &'tcx Expr<'_>, cx: &LateContext<'tcx>) -> bool {
38 mutated_variables(expr, cx).map_or(true, |mutated| mutated.contains(&variable))
41 struct MutVarsDelegate {
42 used_mutably: HirIdSet,
46 impl<'tcx> MutVarsDelegate {
47 #[allow(clippy::similar_names)]
48 fn update(&mut self, cat: &PlaceWithHirId<'tcx>) {
49 match cat.place.base {
50 PlaceBase::Local(id) => {
51 self.used_mutably.insert(id);
53 PlaceBase::Upvar(_) => {
54 //FIXME: This causes false negatives. We can't get the `NodeId` from
55 //`Categorization::Upvar(_)`. So we search for any `Upvar`s in the
56 //`while`-body, not just the ones in the condition.
64 impl<'tcx> Delegate<'tcx> for MutVarsDelegate {
65 fn consume(&mut self, _: &PlaceWithHirId<'tcx>, _: HirId) {}
67 fn borrow(&mut self, cmt: &PlaceWithHirId<'tcx>, _: HirId, bk: ty::BorrowKind) {
68 if bk == ty::BorrowKind::MutBorrow {
73 fn mutate(&mut self, cmt: &PlaceWithHirId<'tcx>, _: HirId) {
77 fn fake_read(&mut self, _: rustc_typeck::expr_use_visitor::Place<'tcx>, _: FakeReadCause, _: HirId) {}
80 pub struct ParamBindingIdCollector {
81 pub binding_hir_ids: Vec<hir::HirId>,
83 impl<'tcx> ParamBindingIdCollector {
84 fn collect_binding_hir_ids(body: &'tcx hir::Body<'tcx>) -> Vec<hir::HirId> {
85 let mut hir_ids: Vec<hir::HirId> = Vec::new();
86 for param in body.params.iter() {
87 let mut finder = ParamBindingIdCollector {
88 binding_hir_ids: Vec::new(),
90 finder.visit_param(param);
91 for hir_id in &finder.binding_hir_ids {
92 hir_ids.push(*hir_id);
98 impl<'tcx> intravisit::Visitor<'tcx> for ParamBindingIdCollector {
99 fn visit_pat(&mut self, pat: &'tcx hir::Pat<'tcx>) {
100 if let hir::PatKind::Binding(_, hir_id, ..) = pat.kind {
101 self.binding_hir_ids.push(hir_id);
103 intravisit::walk_pat(self, pat);
107 pub struct BindingUsageFinder<'a, 'tcx> {
108 cx: &'a LateContext<'tcx>,
109 binding_ids: Vec<hir::HirId>,
112 impl<'a, 'tcx> BindingUsageFinder<'a, 'tcx> {
113 pub fn are_params_used(cx: &'a LateContext<'tcx>, body: &'tcx hir::Body<'tcx>) -> bool {
114 let mut finder = BindingUsageFinder {
116 binding_ids: ParamBindingIdCollector::collect_binding_hir_ids(body),
119 finder.visit_body(body);
123 impl<'a, 'tcx> intravisit::Visitor<'tcx> for BindingUsageFinder<'a, 'tcx> {
124 type NestedFilter = nested_filter::OnlyBodies;
126 fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
127 if !self.usage_found {
128 intravisit::walk_expr(self, expr);
132 fn visit_path(&mut self, path: &'tcx hir::Path<'tcx>, _: hir::HirId) {
133 if let hir::def::Res::Local(id) = path.res {
134 if self.binding_ids.contains(&id) {
135 self.usage_found = true;
140 fn nested_visit_map(&mut self) -> Self::Map {
145 pub fn contains_return_break_continue_macro(expression: &Expr<'_>) -> bool {
146 let mut seen_return_break_continue = false;
147 expr_visitor_no_bodies(|ex| {
148 if seen_return_break_continue {
152 ExprKind::Ret(..) | ExprKind::Break(..) | ExprKind::Continue(..) => {
153 seen_return_break_continue = true;
155 // Something special could be done here to handle while or for loop
156 // desugaring, as this will detect a break if there's a while loop
157 // or a for loop inside the expression.
159 if ex.span.from_expansion() {
160 seen_return_break_continue = true;
164 !seen_return_break_continue
166 .visit_expr(expression);
167 seen_return_break_continue
170 pub fn local_used_after_expr(cx: &LateContext<'_>, local_id: HirId, after: &Expr<'_>) -> bool {
171 let Some(block) = utils::get_enclosing_block(cx, local_id) else { return false };
172 let mut used_after_expr = false;
173 let mut past_expr = false;
174 expr_visitor(cx, |expr| {
179 if expr.hir_id == after.hir_id {
181 } else if past_expr && utils::path_to_local_id(expr, local_id) {
182 used_after_expr = true;