2 use crate::visitors::{for_each_expr, for_each_expr_with_closures, Descend};
3 use core::ops::ControlFlow;
5 use rustc_hir::intravisit::{self, Visitor};
6 use rustc_hir::HirIdSet;
7 use rustc_hir::{Expr, ExprKind, HirId, Node};
8 use rustc_hir_analysis::expr_use_visitor::{Delegate, ExprUseVisitor, PlaceBase, PlaceWithHirId};
9 use rustc_infer::infer::TyCtxtInferExt;
10 use rustc_lint::LateContext;
11 use rustc_middle::hir::nested_filter;
12 use rustc_middle::mir::FakeReadCause;
15 /// Returns a set of mutated local variable IDs, or `None` if mutations could not be determined.
16 pub fn mutated_variables<'tcx>(expr: &'tcx Expr<'_>, cx: &LateContext<'tcx>) -> Option<HirIdSet> {
17 let mut delegate = MutVarsDelegate {
18 used_mutably: HirIdSet::default(),
21 let infcx = cx.tcx.infer_ctxt().build();
25 expr.hir_id.owner.def_id,
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 fn update(&mut self, cat: &PlaceWithHirId<'tcx>) {
48 match cat.place.base {
49 PlaceBase::Local(id) => {
50 self.used_mutably.insert(id);
52 PlaceBase::Upvar(_) => {
53 //FIXME: This causes false negatives. We can't get the `NodeId` from
54 //`Categorization::Upvar(_)`. So we search for any `Upvar`s in the
55 //`while`-body, not just the ones in the condition.
63 impl<'tcx> Delegate<'tcx> for MutVarsDelegate {
64 fn consume(&mut self, _: &PlaceWithHirId<'tcx>, _: HirId) {}
66 fn borrow(&mut self, cmt: &PlaceWithHirId<'tcx>, _: HirId, bk: ty::BorrowKind) {
67 if bk == ty::BorrowKind::MutBorrow {
72 fn mutate(&mut self, cmt: &PlaceWithHirId<'tcx>, _: HirId) {
78 _: &rustc_hir_analysis::expr_use_visitor::PlaceWithHirId<'tcx>,
85 pub struct ParamBindingIdCollector {
86 pub binding_hir_ids: Vec<hir::HirId>,
88 impl<'tcx> ParamBindingIdCollector {
89 fn collect_binding_hir_ids(body: &'tcx hir::Body<'tcx>) -> Vec<hir::HirId> {
90 let mut hir_ids: Vec<hir::HirId> = Vec::new();
91 for param in body.params.iter() {
92 let mut finder = ParamBindingIdCollector {
93 binding_hir_ids: Vec::new(),
95 finder.visit_param(param);
96 for hir_id in &finder.binding_hir_ids {
97 hir_ids.push(*hir_id);
103 impl<'tcx> intravisit::Visitor<'tcx> for ParamBindingIdCollector {
104 fn visit_pat(&mut self, pat: &'tcx hir::Pat<'tcx>) {
105 if let hir::PatKind::Binding(_, hir_id, ..) = pat.kind {
106 self.binding_hir_ids.push(hir_id);
108 intravisit::walk_pat(self, pat);
112 pub struct BindingUsageFinder<'a, 'tcx> {
113 cx: &'a LateContext<'tcx>,
114 binding_ids: Vec<hir::HirId>,
117 impl<'a, 'tcx> BindingUsageFinder<'a, 'tcx> {
118 pub fn are_params_used(cx: &'a LateContext<'tcx>, body: &'tcx hir::Body<'tcx>) -> bool {
119 let mut finder = BindingUsageFinder {
121 binding_ids: ParamBindingIdCollector::collect_binding_hir_ids(body),
124 finder.visit_body(body);
128 impl<'a, 'tcx> intravisit::Visitor<'tcx> for BindingUsageFinder<'a, 'tcx> {
129 type NestedFilter = nested_filter::OnlyBodies;
131 fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
132 if !self.usage_found {
133 intravisit::walk_expr(self, expr);
137 fn visit_path(&mut self, path: &'tcx hir::Path<'tcx>, _: hir::HirId) {
138 if let hir::def::Res::Local(id) = path.res {
139 if self.binding_ids.contains(&id) {
140 self.usage_found = true;
145 fn nested_visit_map(&mut self) -> Self::Map {
150 pub fn contains_return_break_continue_macro(expression: &Expr<'_>) -> bool {
151 for_each_expr(expression, |e| {
153 ExprKind::Ret(..) | ExprKind::Break(..) | ExprKind::Continue(..) => ControlFlow::Break(()),
154 // Something special could be done here to handle while or for loop
155 // desugaring, as this will detect a break if there's a while loop
156 // or a for loop inside the expression.
157 _ if e.span.from_expansion() => ControlFlow::Break(()),
158 _ => ControlFlow::Continue(()),
164 pub fn local_used_after_expr(cx: &LateContext<'_>, local_id: HirId, after: &Expr<'_>) -> bool {
165 let Some(block) = utils::get_enclosing_block(cx, local_id) else { return false };
171 // let closure = || local;
174 let in_loop_or_closure = cx
177 .parent_iter(after.hir_id)
178 .take_while(|&(id, _)| id != block.hir_id)
183 kind: ExprKind::Loop(..) | ExprKind::Closure { .. },
188 if in_loop_or_closure {
192 let mut past_expr = false;
193 for_each_expr_with_closures(cx, block, |e| {
194 if e.hir_id == after.hir_id {
196 ControlFlow::Continue(Descend::No)
197 } else if past_expr && utils::path_to_local_id(e, local_id) {
198 ControlFlow::Break(())
200 ControlFlow::Continue(Descend::Yes)