]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/clippy_utils/src/usage.rs
New upstream version 1.74.1+dfsg1
[rustc.git] / src / tools / clippy / clippy_utils / src / usage.rs
1 use crate::visitors::{for_each_expr, for_each_expr_with_closures, Descend, Visitable};
2 use crate::{self as utils, get_enclosing_loop_or_multi_call_closure};
3 use core::ops::ControlFlow;
4 use hir::def::Res;
5 use rustc_hir::intravisit::{self, Visitor};
6 use rustc_hir::{self as hir, Expr, ExprKind, HirId, HirIdSet};
7 use rustc_hir_typeck::expr_use_visitor::{Delegate, ExprUseVisitor, Place, PlaceBase, PlaceWithHirId};
8 use rustc_infer::infer::TyCtxtInferExt;
9 use rustc_lint::LateContext;
10 use rustc_middle::hir::nested_filter;
11 use rustc_middle::mir::FakeReadCause;
12 use rustc_middle::ty;
13
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(),
18 skip: false,
19 };
20 let infcx = cx.tcx.infer_ctxt().build();
21 ExprUseVisitor::new(
22 &mut delegate,
23 &infcx,
24 expr.hir_id.owner.def_id,
25 cx.param_env,
26 cx.typeck_results(),
27 )
28 .walk_expr(expr);
29
30 if delegate.skip {
31 return None;
32 }
33 Some(delegate.used_mutably)
34 }
35
36 pub fn is_potentially_mutated<'tcx>(variable: HirId, expr: &'tcx Expr<'_>, cx: &LateContext<'tcx>) -> bool {
37 mutated_variables(expr, cx).map_or(true, |mutated| mutated.contains(&variable))
38 }
39
40 pub fn is_potentially_local_place(local_id: HirId, place: &Place<'_>) -> bool {
41 match place.base {
42 PlaceBase::Local(id) => id == local_id,
43 PlaceBase::Upvar(_) => {
44 // Conservatively assume yes.
45 true
46 },
47 _ => false,
48 }
49 }
50
51 struct MutVarsDelegate {
52 used_mutably: HirIdSet,
53 skip: bool,
54 }
55
56 impl<'tcx> MutVarsDelegate {
57 fn update(&mut self, cat: &PlaceWithHirId<'tcx>) {
58 match cat.place.base {
59 PlaceBase::Local(id) => {
60 self.used_mutably.insert(id);
61 },
62 PlaceBase::Upvar(_) => {
63 //FIXME: This causes false negatives. We can't get the `NodeId` from
64 //`Categorization::Upvar(_)`. So we search for any `Upvar`s in the
65 //`while`-body, not just the ones in the condition.
66 self.skip = true;
67 },
68 _ => {},
69 }
70 }
71 }
72
73 impl<'tcx> Delegate<'tcx> for MutVarsDelegate {
74 fn consume(&mut self, _: &PlaceWithHirId<'tcx>, _: HirId) {}
75
76 fn borrow(&mut self, cmt: &PlaceWithHirId<'tcx>, _: HirId, bk: ty::BorrowKind) {
77 if bk == ty::BorrowKind::MutBorrow {
78 self.update(cmt);
79 }
80 }
81
82 fn mutate(&mut self, cmt: &PlaceWithHirId<'tcx>, _: HirId) {
83 self.update(cmt);
84 }
85
86 fn fake_read(&mut self, _: &rustc_hir_typeck::expr_use_visitor::PlaceWithHirId<'tcx>, _: FakeReadCause, _: HirId) {}
87 }
88
89 pub struct ParamBindingIdCollector {
90 pub binding_hir_ids: Vec<hir::HirId>,
91 }
92 impl<'tcx> ParamBindingIdCollector {
93 fn collect_binding_hir_ids(body: &'tcx hir::Body<'tcx>) -> Vec<hir::HirId> {
94 let mut hir_ids: Vec<hir::HirId> = Vec::new();
95 for param in body.params {
96 let mut finder = ParamBindingIdCollector {
97 binding_hir_ids: Vec::new(),
98 };
99 finder.visit_param(param);
100 for hir_id in &finder.binding_hir_ids {
101 hir_ids.push(*hir_id);
102 }
103 }
104 hir_ids
105 }
106 }
107 impl<'tcx> intravisit::Visitor<'tcx> for ParamBindingIdCollector {
108 fn visit_pat(&mut self, pat: &'tcx hir::Pat<'tcx>) {
109 if let hir::PatKind::Binding(_, hir_id, ..) = pat.kind {
110 self.binding_hir_ids.push(hir_id);
111 }
112 intravisit::walk_pat(self, pat);
113 }
114 }
115
116 pub struct BindingUsageFinder<'a, 'tcx> {
117 cx: &'a LateContext<'tcx>,
118 binding_ids: Vec<hir::HirId>,
119 usage_found: bool,
120 }
121 impl<'a, 'tcx> BindingUsageFinder<'a, 'tcx> {
122 pub fn are_params_used(cx: &'a LateContext<'tcx>, body: &'tcx hir::Body<'tcx>) -> bool {
123 let mut finder = BindingUsageFinder {
124 cx,
125 binding_ids: ParamBindingIdCollector::collect_binding_hir_ids(body),
126 usage_found: false,
127 };
128 finder.visit_body(body);
129 finder.usage_found
130 }
131 }
132 impl<'a, 'tcx> intravisit::Visitor<'tcx> for BindingUsageFinder<'a, 'tcx> {
133 type NestedFilter = nested_filter::OnlyBodies;
134
135 fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
136 if !self.usage_found {
137 intravisit::walk_expr(self, expr);
138 }
139 }
140
141 fn visit_path(&mut self, path: &hir::Path<'tcx>, _: hir::HirId) {
142 if let Res::Local(id) = path.res {
143 if self.binding_ids.contains(&id) {
144 self.usage_found = true;
145 }
146 }
147 }
148
149 fn nested_visit_map(&mut self) -> Self::Map {
150 self.cx.tcx.hir()
151 }
152 }
153
154 pub fn contains_return_break_continue_macro(expression: &Expr<'_>) -> bool {
155 for_each_expr(expression, |e| {
156 match e.kind {
157 ExprKind::Ret(..) | ExprKind::Break(..) | ExprKind::Continue(..) => ControlFlow::Break(()),
158 // Something special could be done here to handle while or for loop
159 // desugaring, as this will detect a break if there's a while loop
160 // or a for loop inside the expression.
161 _ if e.span.from_expansion() => ControlFlow::Break(()),
162 _ => ControlFlow::Continue(()),
163 }
164 })
165 .is_some()
166 }
167
168 pub fn local_used_in<'tcx>(cx: &LateContext<'tcx>, local_id: HirId, v: impl Visitable<'tcx>) -> bool {
169 for_each_expr_with_closures(cx, v, |e| {
170 if utils::path_to_local_id(e, local_id) {
171 ControlFlow::Break(())
172 } else {
173 ControlFlow::Continue(())
174 }
175 })
176 .is_some()
177 }
178
179 pub fn local_used_after_expr(cx: &LateContext<'_>, local_id: HirId, after: &Expr<'_>) -> bool {
180 let Some(block) = utils::get_enclosing_block(cx, local_id) else {
181 return false;
182 };
183
184 // for _ in 1..3 {
185 // local
186 // }
187 //
188 // let closure = || local;
189 // closure();
190 // closure();
191 let loop_start = get_enclosing_loop_or_multi_call_closure(cx, after).map(|e| e.hir_id);
192
193 let mut past_expr = false;
194 for_each_expr_with_closures(cx, block, |e| {
195 if past_expr {
196 if utils::path_to_local_id(e, local_id) {
197 ControlFlow::Break(())
198 } else {
199 ControlFlow::Continue(Descend::Yes)
200 }
201 } else if e.hir_id == after.hir_id {
202 past_expr = true;
203 ControlFlow::Continue(Descend::No)
204 } else {
205 past_expr = Some(e.hir_id) == loop_start;
206 ControlFlow::Continue(Descend::Yes)
207 }
208 })
209 .is_some()
210 }