]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_borrowck/src/diagnostics/find_all_local_uses.rs
Rollup merge of #95740 - Amanieu:kreg0, r=nagisa
[rust.git] / compiler / rustc_borrowck / src / diagnostics / find_all_local_uses.rs
1 use std::collections::BTreeSet;
2
3 use rustc_middle::mir::visit::{PlaceContext, Visitor};
4 use rustc_middle::mir::{Body, Local, Location};
5
6 /// Find all uses of (including assignments to) a [`Local`].
7 ///
8 /// Uses `BTreeSet` so output is deterministic.
9 pub(super) fn find<'tcx>(body: &Body<'tcx>, local: Local) -> BTreeSet<Location> {
10     let mut visitor = AllLocalUsesVisitor { for_local: local, uses: BTreeSet::default() };
11     visitor.visit_body(body);
12     visitor.uses
13 }
14
15 struct AllLocalUsesVisitor {
16     for_local: Local,
17     uses: BTreeSet<Location>,
18 }
19
20 impl<'tcx> Visitor<'tcx> for AllLocalUsesVisitor {
21     fn visit_local(&mut self, local: &Local, _context: PlaceContext, location: Location) {
22         if *local == self.for_local {
23             self.uses.insert(location);
24         }
25     }
26 }