1 use rustc::mir::visit::{PlaceContext, Visitor};
2 use rustc::mir::{BasicBlock, Local, Location, Place, Statement, StatementKind, TerminatorKind};
4 use rustc_data_structures::fx::FxHashSet;
6 use borrow_check::MirBorrowckCtxt;
8 impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
9 /// Walks the MIR adding to the set of `used_mut` locals that will be ignored for the purposes
10 /// of the `unused_mut` lint.
12 /// `temporary_used_locals` should contain locals that were found to be temporary, mutable and
13 /// used from borrow checking. This function looks for assignments into these locals from
14 /// user-declared locals and adds those user-defined locals to the `used_mut` set. This can
15 /// occur due to a rare case involving upvars in closures.
17 /// `never_initialized_mut_locals` should contain the set of user-declared mutable locals
18 /// (not arguments) that have not already been marked as being used.
19 /// This function then looks for assignments from statements or the terminator into the locals
20 /// from this set and removes them from the set. This leaves only those locals that have not
21 /// been assigned to - this set is used as a proxy for locals that were not initialized due to
22 /// unreachable code. These locals are then considered "used" to silence the lint for them.
23 /// See #55344 for context.
24 crate fn gather_used_muts(
26 temporary_used_locals: FxHashSet<Local>,
27 mut never_initialized_mut_locals: FxHashSet<Local>,
30 let mut visitor = GatherUsedMutsVisitor {
31 temporary_used_locals,
32 never_initialized_mut_locals: &mut never_initialized_mut_locals,
35 visitor.visit_mir(visitor.mbcx.mir);
38 // Take the union of the existed `used_mut` set with those variables we've found were
40 debug!("gather_used_muts: never_initialized_mut_locals={:?}", never_initialized_mut_locals);
41 self.used_mut = self.used_mut.union(&never_initialized_mut_locals).cloned().collect();
45 /// MIR visitor for collecting used mutable variables.
46 /// The 'visit lifetime represents the duration of the MIR walk.
47 struct GatherUsedMutsVisitor<'visit, 'cx: 'visit, 'gcx: 'tcx, 'tcx: 'cx> {
48 temporary_used_locals: FxHashSet<Local>,
49 never_initialized_mut_locals: &'visit mut FxHashSet<Local>,
50 mbcx: &'visit mut MirBorrowckCtxt<'cx, 'gcx, 'tcx>,
53 impl<'visit, 'cx, 'gcx, 'tcx> Visitor<'tcx> for GatherUsedMutsVisitor<'visit, 'cx, 'gcx, 'tcx> {
54 fn visit_terminator_kind(
57 kind: &TerminatorKind<'tcx>,
60 debug!("visit_terminator_kind: kind={:?}", kind);
62 TerminatorKind::Call { destination: Some((into, _)), .. } => {
63 if let Some(local) = into.base_local() {
65 "visit_terminator_kind: kind={:?} local={:?} \
66 never_initialized_mut_locals={:?}",
67 kind, local, self.never_initialized_mut_locals
69 let _ = self.never_initialized_mut_locals.remove(&local);
79 statement: &Statement<'tcx>,
82 match &statement.kind {
83 StatementKind::Assign(into, _) => {
84 // Remove any locals that we found were initialized from the
85 // `never_initialized_mut_locals` set. At the end, the only remaining locals will
86 // be those that were never initialized - we will consider those as being used as
87 // they will either have been removed by unreachable code optimizations; or linted
88 // as unused variables.
89 if let Some(local) = into.base_local() {
91 "visit_statement: statement={:?} local={:?} \
92 never_initialized_mut_locals={:?}",
93 statement, local, self.never_initialized_mut_locals
95 let _ = self.never_initialized_mut_locals.remove(&local);
105 place_context: PlaceContext<'tcx>,
108 if place_context.is_place_assignment() && self.temporary_used_locals.contains(local) {
109 // Propagate the Local assigned at this Location as a used mutable local variable
110 for moi in &self.mbcx.move_data.loc_map[location] {
111 let mpi = &self.mbcx.move_data.moves[*moi].path;
112 let path = &self.mbcx.move_data.move_paths[*mpi];
114 "assignment of {:?} to {:?}, adding {:?} to used mutable set",
115 path.place, local, path.place
117 if let Place::Local(user_local) = path.place {
118 self.mbcx.used_mut.insert(user_local);