1use rustc_abi::FieldIdx;
2use rustc_data_structures::fx::{FxHashSet, FxIndexMap, IndexEntry};
3use rustc_hir::def::{CtorKind, DefKind};
4use rustc_hir::def_id::{DefId, LocalDefId};
5use rustc_hir::find_attr;
6use rustc_index::IndexVec;
7use rustc_index::bit_set::DenseBitSet;
8use rustc_middle::bug;
9use rustc_middle::mir::visit::{
10 MutatingUseContext, NonMutatingUseContext, NonUseContext, PlaceContext, Visitor,
11};
12use rustc_middle::mir::*;
13use rustc_middle::ty::print::with_no_trimmed_paths;
14use rustc_middle::ty::{self, Ty, TyCtxt};
15use rustc_mir_dataflow::fmt::DebugWithContext;
16use rustc_mir_dataflow::{Analysis, Backward, ResultsCursor};
17use rustc_session::lint;
18use rustc_span::Span;
19use rustc_span::edit_distance::find_best_match_for_name;
20use rustc_span::symbol::{Symbol, kw, sym};
21
22use crate::errors;
23
24#[derive(#[automatically_derived]
impl ::core::marker::Copy for AccessKind { }Copy, #[automatically_derived]
impl ::core::clone::Clone for AccessKind {
#[inline]
fn clone(&self) -> AccessKind { *self }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for AccessKind {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
AccessKind::Param => "Param",
AccessKind::Assign => "Assign",
AccessKind::Capture => "Capture",
})
}
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for AccessKind {
#[inline]
fn eq(&self, other: &AccessKind) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for AccessKind {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {}
}Eq)]
25enum AccessKind {
26 Param,
27 Assign,
28 Capture,
29}
30
31#[derive(#[automatically_derived]
impl ::core::marker::Copy for CaptureKind { }Copy, #[automatically_derived]
impl ::core::clone::Clone for CaptureKind {
#[inline]
fn clone(&self) -> CaptureKind {
let _: ::core::clone::AssertParamIsClone<ty::ClosureKind>;
*self
}
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for CaptureKind {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
CaptureKind::Closure(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"Closure", &__self_0),
CaptureKind::Coroutine =>
::core::fmt::Formatter::write_str(f, "Coroutine"),
CaptureKind::CoroutineClosure =>
::core::fmt::Formatter::write_str(f, "CoroutineClosure"),
CaptureKind::None => ::core::fmt::Formatter::write_str(f, "None"),
}
}
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for CaptureKind {
#[inline]
fn eq(&self, other: &CaptureKind) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr &&
match (self, other) {
(CaptureKind::Closure(__self_0),
CaptureKind::Closure(__arg1_0)) => __self_0 == __arg1_0,
_ => true,
}
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for CaptureKind {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<ty::ClosureKind>;
}
}Eq)]
32enum CaptureKind {
33 Closure(ty::ClosureKind),
34 Coroutine,
35 CoroutineClosure,
36 None,
37}
38
39#[derive(#[automatically_derived]
impl ::core::marker::Copy for Access { }Copy, #[automatically_derived]
impl ::core::clone::Clone for Access {
#[inline]
fn clone(&self) -> Access {
let _: ::core::clone::AssertParamIsClone<AccessKind>;
let _: ::core::clone::AssertParamIsClone<Location>;
let _: ::core::clone::AssertParamIsClone<bool>;
*self
}
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for Access {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field4_finish(f, "Access",
"kind", &self.kind, "location", &self.location, "live",
&self.live, "is_direct", &&self.is_direct)
}
}Debug)]
40struct Access {
41 kind: AccessKind,
43 location: Location,
45 live: bool,
49 is_direct: bool,
52}
53
54x;#[tracing::instrument(level = "debug", skip(tcx), ret)]
55pub(crate) fn check_liveness<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> DenseBitSet<FieldIdx> {
56 if tcx.is_synthetic_mir(def_id) {
58 return DenseBitSet::new_empty(0);
59 }
60
61 if tcx.intrinsic(def_id.to_def_id()).is_some() {
63 return DenseBitSet::new_empty(0);
64 }
65
66 if find_attr!(tcx, def_id.to_def_id(), Naked(..)) {
68 return DenseBitSet::new_empty(0);
69 }
70
71 let parent = tcx.local_parent(tcx.typeck_root_def_id_local(def_id));
73 if let DefKind::Impl { of_trait: true } = tcx.def_kind(parent)
74 && find_attr!(tcx, parent, AutomaticallyDerived)
75 {
76 return DenseBitSet::new_empty(0);
77 }
78
79 let mut body = &*tcx.mir_promoted(def_id).0.borrow();
80 let mut body_mem;
81
82 if body.tainted_by_errors.is_some() {
84 return DenseBitSet::new_empty(0);
85 }
86
87 let mut checked_places = PlaceSet::default();
88 checked_places.insert_locals(&body.local_decls);
89
90 let (capture_kind, num_captures) = if tcx.is_closure_like(def_id.to_def_id()) {
92 let mut self_ty = body.local_decls[ty::CAPTURE_STRUCT_LOCAL].ty;
93 let mut self_is_ref = false;
94 if let ty::Ref(_, ty, _) = self_ty.kind() {
95 self_ty = *ty;
96 self_is_ref = true;
97 }
98
99 let (capture_kind, args) = match self_ty.kind() {
100 ty::Closure(_, args) => {
101 (CaptureKind::Closure(args.as_closure().kind()), ty::UpvarArgs::Closure(args))
102 }
103 &ty::Coroutine(_, args) => (CaptureKind::Coroutine, ty::UpvarArgs::Coroutine(args)),
104 &ty::CoroutineClosure(_, args) => {
105 (CaptureKind::CoroutineClosure, ty::UpvarArgs::CoroutineClosure(args))
106 }
107 _ => bug!("expected closure or generator, found {:?}", self_ty),
108 };
109
110 let captures = tcx.closure_captures(def_id);
111 checked_places.insert_captures(tcx, self_is_ref, captures, args.upvar_tys());
112
113 if let CaptureKind::Closure(ty::ClosureKind::FnMut) = capture_kind {
117 body_mem = body.clone();
119 for bbdata in body_mem.basic_blocks_mut() {
120 if let TerminatorKind::Return | TerminatorKind::UnwindResume =
122 bbdata.terminator().kind
123 {
124 bbdata.terminator_mut().kind = TerminatorKind::Goto { target: START_BLOCK };
125 }
126 }
127 body = &body_mem;
128 }
129
130 (capture_kind, args.upvar_tys().len())
131 } else {
132 (CaptureKind::None, 0)
133 };
134
135 checked_places.record_debuginfo(&body.var_debug_info);
137
138 let self_assignment = find_self_assignments(&checked_places, body);
139
140 let mut live =
141 MaybeLivePlaces { tcx, capture_kind, checked_places: &checked_places, self_assignment }
142 .iterate_to_fixpoint(tcx, body, None)
143 .into_results_cursor(body);
144
145 let typing_env = ty::TypingEnv::post_analysis(tcx, body.source.def_id());
146
147 let mut assignments =
148 AssignmentResult::find_dead_assignments(tcx, typing_env, &checked_places, &mut live, body);
149
150 assignments.merge_guards();
151
152 let dead_captures = assignments.compute_dead_captures(num_captures);
153
154 assignments.report_fully_unused();
155 assignments.report_unused_assignments();
156
157 dead_captures
158}
159
160#[inline]
162fn is_capture(place: PlaceRef<'_>) -> bool {
163 if !place.projection.is_empty() {
164 if true {
match (&place.local, &ty::CAPTURE_STRUCT_LOCAL) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
};
};debug_assert_eq!(place.local, ty::CAPTURE_STRUCT_LOCAL);
165 true
166 } else {
167 false
168 }
169}
170
171fn maybe_suggest_unit_pattern_typo<'tcx>(
173 tcx: TyCtxt<'tcx>,
174 body_def_id: DefId,
175 name: Symbol,
176 span: Span,
177 ty: Ty<'tcx>,
178) -> Option<errors::PatternTypo> {
179 if let ty::Adt(adt_def, _) = ty.peel_refs().kind() {
180 let variant_names: Vec<_> = adt_def
181 .variants()
182 .iter()
183 .filter(|v| #[allow(non_exhaustive_omitted_patterns)] match v.ctor {
Some((CtorKind::Const, _)) => true,
_ => false,
}matches!(v.ctor, Some((CtorKind::Const, _))))
184 .map(|v| v.name)
185 .collect();
186 if let Some(name) = find_best_match_for_name(&variant_names, name, None)
187 && let Some(variant) = adt_def
188 .variants()
189 .iter()
190 .find(|v| v.name == name && #[allow(non_exhaustive_omitted_patterns)] match v.ctor {
Some((CtorKind::Const, _)) => true,
_ => false,
}matches!(v.ctor, Some((CtorKind::Const, _))))
191 {
192 return Some(errors::PatternTypo {
193 span,
194 code: { let _guard = NoTrimmedGuard::new(); tcx.def_path_str(variant.def_id) }with_no_trimmed_paths!(tcx.def_path_str(variant.def_id)),
195 kind: tcx.def_descr(variant.def_id),
196 item_name: variant.name,
197 });
198 }
199 }
200
201 let constants = tcx
204 .hir_body_owners()
205 .filter(|&def_id| {
206 #[allow(non_exhaustive_omitted_patterns)] match tcx.def_kind(def_id) {
DefKind::Const { .. } => true,
_ => false,
}matches!(tcx.def_kind(def_id), DefKind::Const { .. })
207 && tcx.type_of(def_id).instantiate_identity().skip_norm_wip() == ty
208 && tcx.visibility(def_id).is_accessible_from(body_def_id, tcx)
209 })
210 .collect::<Vec<_>>();
211 let names = constants.iter().map(|&def_id| tcx.item_name(def_id)).collect::<Vec<_>>();
212 if let Some(item_name) = find_best_match_for_name(&names, name, None)
213 && let Some(position) = names.iter().position(|&n| n == item_name)
214 && let Some(&def_id) = constants.get(position)
215 {
216 return Some(errors::PatternTypo {
217 span,
218 code: { let _guard = NoTrimmedGuard::new(); tcx.def_path_str(def_id) }with_no_trimmed_paths!(tcx.def_path_str(def_id)),
219 kind: "constant",
220 item_name,
221 });
222 }
223
224 None
225}
226
227fn maybe_drop_guard<'tcx>(
229 tcx: TyCtxt<'tcx>,
230 typing_env: ty::TypingEnv<'tcx>,
231 index: PlaceIndex,
232 ever_dropped: &DenseBitSet<PlaceIndex>,
233 checked_places: &PlaceSet<'tcx>,
234 body: &Body<'tcx>,
235) -> bool {
236 if ever_dropped.contains(index) {
237 let ty = checked_places.places[index].ty(&body.local_decls, tcx).ty;
238 #[allow(non_exhaustive_omitted_patterns)] match ty.kind() {
ty::Closure(..) | ty::Coroutine(..) | ty::Tuple(..) | ty::Adt(..) |
ty::Dynamic(..) | ty::Array(..) | ty::Slice(..) |
ty::Alias(ty::AliasTy { kind: ty::Opaque { .. }, .. }) => true,
_ => false,
}matches!(
239 ty.kind(),
240 ty::Closure(..)
241 | ty::Coroutine(..)
242 | ty::Tuple(..)
243 | ty::Adt(..)
244 | ty::Dynamic(..)
245 | ty::Array(..)
246 | ty::Slice(..)
247 | ty::Alias(ty::AliasTy { kind: ty::Opaque { .. }, .. })
248 ) && ty.needs_drop(tcx, typing_env)
249 } else {
250 false
251 }
252}
253
254fn annotate_mut_binding_to_immutable_binding<'tcx>(
273 tcx: TyCtxt<'tcx>,
274 place: PlaceRef<'tcx>,
275 body_def_id: LocalDefId,
276 assignment_span: Span,
277 body: &Body<'tcx>,
278) -> Option<errors::UnusedAssignSuggestion> {
279 use rustc_hir as hir;
280 use rustc_hir::intravisit::{self, Visitor};
281
282 let local = place.as_local()?;
284 let LocalKind::Arg = body.local_kind(local) else { return None };
285 let Mutability::Mut = body.local_decls[local].mutability else { return None };
286
287 let hir_param_index =
289 local.as_usize() - if tcx.is_closure_like(body_def_id.to_def_id()) { 2 } else { 1 };
290 let fn_decl = tcx.hir_node_by_def_id(body_def_id).fn_decl()?;
291 let ty = fn_decl.inputs[hir_param_index];
292 let hir::TyKind::Ref(lt, mut_ty) = ty.kind else { return None };
293
294 let hir_body = tcx.hir_maybe_body_owned_by(body_def_id)?;
296 let param = hir_body.params[hir_param_index];
297 let hir::PatKind::Binding(hir::BindingMode::MUT, _hir_id, ident, _) = param.pat.kind else {
298 return None;
299 };
300
301 let mut finder = ExprFinder { assignment_span, lhs: None, rhs: None };
303 finder.visit_body(hir_body);
304 let lhs = finder.lhs?;
305 let rhs = finder.rhs?;
306
307 let hir::ExprKind::AddrOf(hir::BorrowKind::Ref, _mut, inner) = rhs.kind else { return None };
308
309 let pre = if lt.ident.span.is_empty() { "" } else { " " };
311 let ty_span = if mut_ty.mutbl.is_mut() {
312 None
314 } else {
315 Some(mut_ty.ty.span.shrink_to_lo())
317 };
318
319 return Some(errors::UnusedAssignSuggestion {
320 ty_span,
321 pre,
322 ty_ref_span: param.pat.span.until(ident.span),
324 pre_lhs_span: lhs.span.shrink_to_lo(),
326 rhs_borrow_span: rhs.span.until(inner.span),
328 });
329
330 #[derive(#[automatically_derived]
impl<'hir> ::core::fmt::Debug for ExprFinder<'hir> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field3_finish(f, "ExprFinder",
"assignment_span", &self.assignment_span, "lhs", &self.lhs, "rhs",
&&self.rhs)
}
}Debug)]
331 struct ExprFinder<'hir> {
332 assignment_span: Span,
333 lhs: Option<&'hir hir::Expr<'hir>>,
334 rhs: Option<&'hir hir::Expr<'hir>>,
335 }
336 impl<'hir> Visitor<'hir> for ExprFinder<'hir> {
337 fn visit_expr(&mut self, expr: &'hir hir::Expr<'hir>) {
338 if expr.span == self.assignment_span
339 && let hir::ExprKind::Assign(lhs, rhs, _) = expr.kind
340 {
341 self.lhs = Some(lhs);
342 self.rhs = Some(rhs);
343 } else {
344 intravisit::walk_expr(self, expr)
345 }
346 }
347 }
348}
349
350fn find_self_assignments<'tcx>(
362 checked_places: &PlaceSet<'tcx>,
363 body: &Body<'tcx>,
364) -> FxHashSet<Location> {
365 let mut self_assign = FxHashSet::default();
366
367 const FIELD_0: FieldIdx = FieldIdx::from_u32(0);
368 const FIELD_1: FieldIdx = FieldIdx::from_u32(1);
369
370 for (bb, bb_data) in body.basic_blocks.iter_enumerated() {
371 for (statement_index, stmt) in bb_data.statements.iter().enumerate() {
372 let StatementKind::Assign((first_place, rvalue)) = &stmt.kind else { continue };
373 match rvalue {
374 Rvalue::BinaryOp(
376 BinOp::AddWithOverflow | BinOp::SubWithOverflow | BinOp::MulWithOverflow,
377 (Operand::Copy(lhs), _),
378 ) => {
379 if statement_index + 1 != bb_data.statements.len() {
381 continue;
382 }
383
384 let TerminatorKind::Assert {
385 cond, target, msg: AssertKind::Overflow(..), ..
386 } = &bb_data.terminator().kind
387 else {
388 continue;
389 };
390 let Some(assign) = body.basic_blocks[*target].statements.first() else {
391 continue;
392 };
393 let StatementKind::Assign((dest, Rvalue::Use(Operand::Move(temp), _))) =
394 assign.kind
395 else {
396 continue;
397 };
398
399 if dest != *lhs {
400 continue;
401 }
402
403 let Operand::Move(cond) = cond else { continue };
404 let [PlaceElem::Field(FIELD_0, _)] = &temp.projection.as_slice() else {
405 continue;
406 };
407 let [PlaceElem::Field(FIELD_1, _)] = &cond.projection.as_slice() else {
408 continue;
409 };
410
411 let is_indirect = checked_places
413 .get(dest.as_ref())
414 .map_or(false, |(_, projections)| is_indirect(projections));
415 if is_indirect {
416 continue;
417 }
418
419 if first_place.local == temp.local
420 && first_place.local == cond.local
421 && first_place.projection.is_empty()
422 {
423 self_assign.insert(Location {
425 block: bb,
426 statement_index: bb_data.statements.len() - 1,
427 });
428 self_assign.insert(Location {
429 block: bb,
430 statement_index: bb_data.statements.len(),
431 });
432 self_assign.insert(Location { block: *target, statement_index: 0 });
434 }
435 }
436 Rvalue::BinaryOp(op, (Operand::Copy(lhs), _)) => {
438 if lhs != first_place {
439 continue;
440 }
441
442 let is_indirect = checked_places
444 .get(first_place.as_ref())
445 .map_or(false, |(_, projections)| is_indirect(projections));
446 if is_indirect {
447 continue;
448 }
449
450 self_assign.insert(Location { block: bb, statement_index });
451
452 if let BinOp::Div | BinOp::Rem = op
455 && statement_index == 0
456 && let &[pred] = body.basic_blocks.predecessors()[bb].as_slice()
457 && let TerminatorKind::Assert { msg, .. } =
458 &body.basic_blocks[pred].terminator().kind
459 && let AssertKind::Overflow(..) = **msg
460 && let len = body.basic_blocks[pred].statements.len()
461 && len >= 2
462 {
463 self_assign.insert(Location { block: pred, statement_index: len - 1 });
465 self_assign.insert(Location { block: pred, statement_index: len - 2 });
467 }
468 }
469 _ => {}
470 }
471 }
472 }
473
474 self_assign
475}
476
477#[derive(#[automatically_derived]
impl<'tcx> ::core::default::Default for PlaceSet<'tcx> {
#[inline]
fn default() -> PlaceSet<'tcx> {
PlaceSet {
places: ::core::default::Default::default(),
names: ::core::default::Default::default(),
locals: ::core::default::Default::default(),
capture_field_pos: ::core::default::Default::default(),
captures: ::core::default::Default::default(),
}
}
}Default, #[automatically_derived]
impl<'tcx> ::core::fmt::Debug for PlaceSet<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field5_finish(f, "PlaceSet",
"places", &self.places, "names", &self.names, "locals",
&self.locals, "capture_field_pos", &self.capture_field_pos,
"captures", &&self.captures)
}
}Debug)]
478struct PlaceSet<'tcx> {
479 places: IndexVec<PlaceIndex, PlaceRef<'tcx>>,
480 names: IndexVec<PlaceIndex, Option<(Symbol, Span)>>,
481
482 locals: IndexVec<Local, Option<PlaceIndex>>,
484
485 capture_field_pos: usize,
488 captures: IndexVec<FieldIdx, (PlaceIndex, bool)>,
490}
491
492impl<'tcx> PlaceSet<'tcx> {
493 fn insert_locals(&mut self, decls: &IndexVec<Local, LocalDecl<'tcx>>) {
494 self.locals = IndexVec::from_elem(None, &decls);
495 for (local, decl) in decls.iter_enumerated() {
496 if let LocalInfo::User(BindingForm::Var(_) | BindingForm::RefForGuard(_)) =
499 decl.local_info()
500 {
501 let index = self.places.push(local.into());
502 self.locals[local] = Some(index);
503 let _index = self.names.push(None);
504 if true {
match (&index, &_index) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
};
};debug_assert_eq!(index, _index);
505 }
506 }
507 }
508
509 fn insert_captures(
510 &mut self,
511 tcx: TyCtxt<'tcx>,
512 self_is_ref: bool,
513 captures: &[&'tcx ty::CapturedPlace<'tcx>],
514 upvars: &ty::List<Ty<'tcx>>,
515 ) {
516 if true {
match (&self.locals[ty::CAPTURE_STRUCT_LOCAL], &None) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
};
};debug_assert_eq!(self.locals[ty::CAPTURE_STRUCT_LOCAL], None);
518
519 let self_place = Place {
520 local: ty::CAPTURE_STRUCT_LOCAL,
521 projection: tcx.mk_place_elems(if self_is_ref { &[PlaceElem::Deref] } else { &[] }),
522 };
523 if self_is_ref {
524 self.capture_field_pos = 1;
525 }
526
527 for (f, (capture, ty)) in std::iter::zip(captures, upvars).enumerate() {
528 let f = FieldIdx::from_usize(f);
529 let elem = PlaceElem::Field(f, ty);
530 let by_ref = #[allow(non_exhaustive_omitted_patterns)] match capture.info.capture_kind {
ty::UpvarCapture::ByRef(..) => true,
_ => false,
}matches!(capture.info.capture_kind, ty::UpvarCapture::ByRef(..));
531 let place = if by_ref {
532 self_place.project_deeper(&[elem, PlaceElem::Deref], tcx)
533 } else {
534 self_place.project_deeper(&[elem], tcx)
535 };
536 let index = self.places.push(place.as_ref());
537 let _f = self.captures.push((index, by_ref));
538 if true {
match (&_f, &f) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
};
};debug_assert_eq!(_f, f);
539
540 self.names.insert(
543 index,
544 (Symbol::intern(&capture.to_string(tcx)), capture.get_path_span(tcx)),
545 );
546 }
547 }
548
549 fn record_debuginfo(&mut self, var_debug_info: &Vec<VarDebugInfo<'tcx>>) {
550 let ignore_name = |name: Symbol| {
551 name == sym::empty || name == kw::SelfLower || name.as_str().starts_with('_')
552 };
553 for var_debug_info in var_debug_info {
554 if let VarDebugInfoContents::Place(place) = var_debug_info.value
555 && let Some(index) = self.locals[place.local]
556 && !ignore_name(var_debug_info.name)
557 {
558 self.names.get_or_insert_with(index, || {
559 (var_debug_info.name, var_debug_info.source_info.span)
560 });
561 }
562 }
563
564 for index_opt in self.locals.iter_mut() {
566 if let Some(index) = *index_opt {
567 let remove = match self.names[index] {
568 None => true,
569 Some((name, _)) => ignore_name(name),
570 };
571 if remove {
572 *index_opt = None;
573 }
574 }
575 }
576 }
577
578 #[inline]
579 fn get(&self, place: PlaceRef<'tcx>) -> Option<(PlaceIndex, &'tcx [PlaceElem<'tcx>])> {
580 if let Some(index) = self.locals[place.local] {
581 return Some((index, place.projection));
582 }
583 if place.local == ty::CAPTURE_STRUCT_LOCAL
584 && !self.captures.is_empty()
585 && self.capture_field_pos < place.projection.len()
586 && let PlaceElem::Field(f, _) = place.projection[self.capture_field_pos]
587 && let Some((index, by_ref)) = self.captures.get(f)
588 {
589 let mut start = self.capture_field_pos + 1;
590 if *by_ref {
591 start += 1;
593 }
594 if start <= place.projection.len() {
596 let projection = &place.projection[start..];
597 return Some((*index, projection));
598 }
599 }
600 None
601 }
602
603 fn iter(&self) -> impl Iterator<Item = (PlaceIndex, &PlaceRef<'tcx>)> {
604 self.places.iter_enumerated()
605 }
606
607 fn len(&self) -> usize {
608 self.places.len()
609 }
610}
611
612struct AssignmentResult<'a, 'tcx> {
613 tcx: TyCtxt<'tcx>,
614 typing_env: ty::TypingEnv<'tcx>,
615 checked_places: &'a PlaceSet<'tcx>,
616 body: &'a Body<'tcx>,
617 ever_live: DenseBitSet<PlaceIndex>,
619 ever_dropped: DenseBitSet<PlaceIndex>,
622 assignments: IndexVec<PlaceIndex, FxIndexMap<SourceInfo, Access>>,
629}
630
631impl<'a, 'tcx> AssignmentResult<'a, 'tcx> {
632 fn find_dead_assignments(
637 tcx: TyCtxt<'tcx>,
638 typing_env: ty::TypingEnv<'tcx>,
639 checked_places: &'a PlaceSet<'tcx>,
640 cursor: &mut ResultsCursor<'_, 'tcx, MaybeLivePlaces<'_, 'tcx>>,
641 body: &'a Body<'tcx>,
642 ) -> AssignmentResult<'a, 'tcx> {
643 let mut ever_live = DenseBitSet::new_empty(checked_places.len());
644 let mut ever_dropped = DenseBitSet::new_empty(checked_places.len());
645 let mut assignments = IndexVec::<PlaceIndex, FxIndexMap<_, _>>::from_elem(
646 Default::default(),
647 &checked_places.places,
648 );
649
650 let mut check_place = |place: Place<'tcx>,
651 kind,
652 source_info: SourceInfo,
653 location: Location,
654 live: &DenseBitSet<PlaceIndex>| {
655 if let Some((index, extra_projections)) = checked_places.get(place.as_ref()) {
656 if !is_indirect(extra_projections) {
657 let is_direct = extra_projections.is_empty();
658 match assignments[index].entry(source_info) {
659 IndexEntry::Vacant(v) => {
660 let access =
661 Access { kind, location, live: live.contains(index), is_direct };
662 v.insert(access);
663 }
664 IndexEntry::Occupied(mut o) => {
665 o.get_mut().live |= live.contains(index);
668 o.get_mut().is_direct &= is_direct;
669 }
670 }
671 }
672 }
673 };
674
675 let mut record_drop = |place: Place<'tcx>| {
676 if let Some((index, &[])) = checked_places.get(place.as_ref()) {
677 ever_dropped.insert(index);
678 }
679 };
680
681 for (bb, bb_data) in traversal::postorder(body) {
682 cursor.seek_to_block_end(bb);
683 let live = cursor.get();
684 ever_live.union(live);
685
686 let terminator = bb_data.terminator();
687 match &terminator.kind {
688 TerminatorKind::Call { destination: place, .. }
689 | TerminatorKind::Yield { resume_arg: place, .. } => {
690 check_place(
691 *place,
692 AccessKind::Assign,
693 terminator.source_info,
694 body.terminator_loc(bb),
695 live,
696 );
697 record_drop(*place)
698 }
699 TerminatorKind::Drop { place, .. } => record_drop(*place),
700 TerminatorKind::InlineAsm { operands, .. } => {
701 for operand in operands {
702 if let InlineAsmOperand::Out { place: Some(place), .. }
703 | InlineAsmOperand::InOut { out_place: Some(place), .. } = operand
704 {
705 check_place(
706 *place,
707 AccessKind::Assign,
708 terminator.source_info,
709 body.terminator_loc(bb),
710 live,
711 );
712 }
713 }
714 }
715 _ => {}
716 }
717
718 for (statement_index, statement) in bb_data.statements.iter().enumerate().rev() {
719 let location = Location { block: bb, statement_index };
720 cursor.seek_before_primary_effect(location);
721 let live = cursor.get();
722 ever_live.union(live);
723 match &statement.kind {
724 StatementKind::Assign((place, _)) => {
725 check_place(
726 *place,
727 AccessKind::Assign,
728 statement.source_info,
729 location,
730 live,
731 );
732 }
733 StatementKind::SetDiscriminant { place, .. } => {
734 check_place(
735 **place,
736 AccessKind::Assign,
737 statement.source_info,
738 location,
739 live,
740 );
741 }
742 StatementKind::StorageLive(_)
743 | StatementKind::StorageDead(_)
744 | StatementKind::Coverage(_)
745 | StatementKind::Intrinsic(_)
746 | StatementKind::Nop
747 | StatementKind::FakeRead(_)
748 | StatementKind::PlaceMention(_)
749 | StatementKind::ConstEvalCounter
750 | StatementKind::BackwardIncompatibleDropHint { .. }
751 | StatementKind::AscribeUserType(_, _) => (),
752 }
753 }
754 }
755
756 {
758 cursor.seek_to_block_start(START_BLOCK);
759 let live = cursor.get();
760 ever_live.union(live);
761
762 for (index, place) in checked_places.iter() {
764 let kind = if is_capture(*place) {
765 if place.projection.last() == Some(&PlaceElem::Deref) {
768 continue;
769 }
770
771 AccessKind::Capture
772 } else if body.local_kind(place.local) == LocalKind::Arg {
773 AccessKind::Param
774 } else {
775 continue;
776 };
777 let source_info = body.local_decls[place.local].source_info;
778 let access = Access {
779 kind,
780 location: Location::START,
781 live: live.contains(index),
782 is_direct: true,
783 };
784 assignments[index].insert(source_info, access);
785 }
786 }
787
788 AssignmentResult {
789 tcx,
790 typing_env,
791 checked_places,
792 ever_live,
793 ever_dropped,
794 assignments,
795 body,
796 }
797 }
798
799 fn merge_guards(&mut self) {
811 for (index, place) in self.checked_places.iter() {
812 let local = place.local;
813 if let &LocalInfo::User(BindingForm::RefForGuard(arm_local)) =
814 self.body.local_decls[local].local_info()
815 {
816 if true {
if !place.projection.is_empty() {
::core::panicking::panic("assertion failed: place.projection.is_empty()")
};
};debug_assert!(place.projection.is_empty());
817
818 let Some((arm_index, _proj)) = self.checked_places.get(arm_local.into()) else {
820 continue;
821 };
822 if true {
match (&index, &arm_index) {
(left_val, right_val) => {
if *left_val == *right_val {
let kind = ::core::panicking::AssertKind::Ne;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
};
};debug_assert_ne!(index, arm_index);
823 if true {
match (&_proj, &&[]) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
};
};debug_assert_eq!(_proj, &[]);
824
825 if self.ever_live.contains(index) {
827 self.ever_live.insert(arm_index);
828 }
829
830 let guard_assignments = std::mem::take(&mut self.assignments[index]);
838 let arm_assignments = &mut self.assignments[arm_index];
839 for (source_info, access) in guard_assignments {
840 match arm_assignments.entry(source_info) {
841 IndexEntry::Vacant(v) => {
842 v.insert(access);
843 }
844 IndexEntry::Occupied(mut o) => {
845 o.get_mut().live |= access.live;
846 }
847 }
848 }
849 }
850 }
851 }
852
853 fn compute_dead_captures(&self, num_captures: usize) -> DenseBitSet<FieldIdx> {
855 let mut dead_captures = DenseBitSet::new_empty(num_captures);
857 for (index, place) in self.checked_places.iter() {
858 if self.ever_live.contains(index) {
859 continue;
860 }
861
862 if is_capture(*place) {
864 for p in place.projection {
865 if let PlaceElem::Field(f, _) = p {
866 dead_captures.insert(*f);
867 break;
868 }
869 }
870 continue;
871 }
872 }
873
874 dead_captures
875 }
876
877 fn is_local_in_reachable_code(&self, local: Local) -> bool {
880 struct LocalVisitor {
881 target_local: Local,
882 found: bool,
883 }
884
885 impl<'tcx> Visitor<'tcx> for LocalVisitor {
886 fn visit_local(&mut self, local: Local, _context: PlaceContext, _location: Location) {
887 if local == self.target_local {
888 self.found = true;
889 }
890 }
891 }
892
893 let mut visitor = LocalVisitor { target_local: local, found: false };
894 for (bb, bb_data) in traversal::postorder(self.body) {
895 visitor.visit_basic_block_data(bb, bb_data);
896 if visitor.found {
897 return true;
898 }
899 }
900
901 false
902 }
903
904 fn report_fully_unused(&mut self) {
906 let tcx = self.tcx;
907
908 let mut string_constants_in_body = None;
911 let mut maybe_suggest_literal_matching_name = |name: Symbol| {
912 let string_constants_in_body = string_constants_in_body.get_or_insert_with(|| {
914 struct LiteralFinder {
915 found: Vec<(Span, String)>,
916 }
917
918 impl<'tcx> Visitor<'tcx> for LiteralFinder {
919 fn visit_const_operand(&mut self, constant: &ConstOperand<'tcx>, _: Location) {
920 if let ty::Ref(_, ref_ty, _) = constant.ty().kind()
921 && ref_ty.kind() == &ty::Str
922 {
923 let rendered_constant = constant.const_.to_string();
924 self.found.push((constant.span, rendered_constant));
925 }
926 }
927 }
928
929 let mut finder = LiteralFinder { found: ::alloc::vec::Vec::new()vec![] };
930 finder.visit_body(self.body);
931 finder.found
932 });
933
934 let brace_name = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{{{0}", name))
})format!("{{{name}");
935 string_constants_in_body
936 .iter()
937 .filter(|(_, rendered_constant)| {
938 rendered_constant
939 .split(&brace_name)
940 .any(|c| #[allow(non_exhaustive_omitted_patterns)] match c.chars().next() {
Some('}' | ':') => true,
_ => false,
}matches!(c.chars().next(), Some('}' | ':')))
941 })
942 .map(|&(lit, _)| errors::UnusedVariableStringInterp { lit })
943 .collect::<Vec<_>>()
944 };
945
946 for (index, place) in self.checked_places.iter() {
948 if self.ever_live.contains(index) {
949 continue;
950 }
951
952 if is_capture(*place) {
954 continue;
955 }
956
957 let local = place.local;
958 let decl = &self.body.local_decls[local];
959
960 if decl.from_compiler_desugaring() {
961 continue;
962 }
963
964 let LocalInfo::User(BindingForm::Var(binding)) = decl.local_info() else { continue };
966 let Some(hir_id) = decl.source_info.scope.lint_root(&self.body.source_scopes) else {
967 continue;
968 };
969
970 let introductions = &binding.introductions;
971
972 let Some((name, def_span)) = self.checked_places.names[index] else { continue };
973
974 let from_macro = def_span.from_expansion()
977 && introductions.iter().any(|intro| intro.span.eq_ctxt(def_span));
978
979 let maybe_suggest_typo = || {
980 if let LocalKind::Arg = self.body.local_kind(local) {
981 None
982 } else {
983 maybe_suggest_unit_pattern_typo(
984 tcx,
985 self.body.source.def_id(),
986 name,
987 def_span,
988 decl.ty,
989 )
990 }
991 };
992
993 let statements = &mut self.assignments[index];
994 if statements.is_empty() {
995 if !self.is_local_in_reachable_code(local) {
996 continue;
997 }
998
999 let sugg = if from_macro {
1000 errors::UnusedVariableSugg::NoSugg { span: def_span, name }
1001 } else {
1002 let typo = maybe_suggest_typo();
1003 errors::UnusedVariableSugg::TryPrefix { spans: ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[def_span]))vec![def_span], name, typo }
1004 };
1005 tcx.emit_node_span_lint(
1006 lint::builtin::UNUSED_VARIABLES,
1007 hir_id,
1008 def_span,
1009 errors::UnusedVariable {
1010 name,
1011 string_interp: maybe_suggest_literal_matching_name(name),
1012 sugg,
1013 },
1014 );
1015 continue;
1016 }
1017
1018 statements.retain(|source_info, _| {
1022 !binding.introductions.iter().any(|intro| intro.span == source_info.span)
1023 });
1024
1025 if let Some((_, initializer_span)) = binding.opt_match_place {
1028 statements.retain(|source_info, _| {
1029 let within = source_info.span.find_ancestor_inside(initializer_span);
1030 let outer_initializer_span =
1031 initializer_span.find_ancestor_in_same_ctxt(source_info.span);
1032 within.is_none()
1033 && outer_initializer_span.map_or(true, |s| !s.contains(source_info.span))
1034 });
1035 }
1036
1037 if !statements.is_empty() {
1038 if maybe_drop_guard(
1041 tcx,
1042 self.typing_env,
1043 index,
1044 &self.ever_dropped,
1045 self.checked_places,
1046 self.body,
1047 ) {
1048 statements.retain(|_, access| access.is_direct);
1049 if statements.is_empty() {
1050 continue;
1051 }
1052 }
1053
1054 let typo = maybe_suggest_typo();
1055 tcx.emit_node_span_lint(
1056 lint::builtin::UNUSED_VARIABLES,
1057 hir_id,
1058 def_span,
1059 errors::UnusedVarAssignedOnly { name, typo },
1060 );
1061 continue;
1062 }
1063
1064 let spans = introductions.iter().map(|intro| intro.span).collect::<Vec<_>>();
1066
1067 let any_shorthand = introductions.iter().any(|intro| intro.is_shorthand);
1068
1069 let sugg = if any_shorthand {
1070 errors::UnusedVariableSugg::TryIgnore {
1071 name: name.to_ident_string(),
1072 shorthands: introductions
1073 .iter()
1074 .filter_map(
1075 |intro| if intro.is_shorthand { Some(intro.span) } else { None },
1076 )
1077 .collect(),
1078 non_shorthands: introductions
1079 .iter()
1080 .filter_map(
1081 |intro| {
1082 if !intro.is_shorthand { Some(intro.span) } else { None }
1083 },
1084 )
1085 .collect(),
1086 }
1087 } else if from_macro {
1088 errors::UnusedVariableSugg::NoSugg { span: def_span, name }
1089 } else if !introductions.is_empty() {
1090 let typo = maybe_suggest_typo();
1091 errors::UnusedVariableSugg::TryPrefix { name, typo, spans: spans.clone() }
1092 } else {
1093 let typo = maybe_suggest_typo();
1094 errors::UnusedVariableSugg::TryPrefix { name, typo, spans: ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[def_span]))vec![def_span] }
1095 };
1096
1097 tcx.emit_node_span_lint(
1098 lint::builtin::UNUSED_VARIABLES,
1099 hir_id,
1100 spans,
1101 errors::UnusedVariable {
1102 name,
1103 string_interp: maybe_suggest_literal_matching_name(name),
1104 sugg,
1105 },
1106 );
1107 }
1108 }
1109
1110 fn report_unused_assignments(self) {
1113 let tcx = self.tcx;
1114
1115 for (index, statements) in self.assignments.into_iter_enumerated() {
1116 if statements.is_empty() {
1117 continue;
1118 }
1119
1120 let Some((name, decl_span)) = self.checked_places.names[index] else { continue };
1121
1122 let is_maybe_drop_guard = maybe_drop_guard(
1123 tcx,
1124 self.typing_env,
1125 index,
1126 &self.ever_dropped,
1127 self.checked_places,
1128 self.body,
1129 );
1130
1131 if name.as_str().starts_with('_') {
1133 continue;
1134 }
1135
1136 let mut next_direct_assignments: Vec<(Span, Location)> = Vec::new();
1137 let mut dead_statements = Vec::with_capacity(statements.len());
1138
1139 for (source_info, Access { live, kind, is_direct, location }) in statements.into_iter()
1140 {
1141 let direct_assignment = kind == AccessKind::Assign && is_direct;
1142 let should_report = !live && (is_direct || !is_maybe_drop_guard);
1143
1144 let overwrite = if should_report && direct_assignment {
1145 next_direct_assignments
1146 .iter()
1147 .rfind(|(_, overwrite_location)| {
1148 location.is_predecessor_of(*overwrite_location, self.body)
1149 })
1150 .map(|&(overwrite_span, _)| errors::UnusedAssignOverwrite {
1151 assigned_span: source_info.span,
1152 overwrite_span,
1153 name,
1154 })
1155 } else {
1156 None
1157 };
1158
1159 if direct_assignment {
1160 next_direct_assignments.push((source_info.span, location));
1161 }
1162
1163 if !should_report {
1164 continue;
1165 }
1166 dead_statements.push((source_info, kind, is_direct, overwrite));
1167 }
1168
1169 for (source_info, kind, is_direct, overwrite) in dead_statements.into_iter().rev() {
1172 let Some(hir_id) = source_info.scope.lint_root(&self.body.source_scopes) else {
1174 continue;
1175 };
1176
1177 match kind {
1178 AccessKind::Assign => {
1179 let suggestion = annotate_mut_binding_to_immutable_binding(
1180 tcx,
1181 self.checked_places.places[index],
1182 self.body.source.def_id().expect_local(),
1183 source_info.span,
1184 self.body,
1185 );
1186 let overwrite =
1187 if suggestion.is_none() && is_direct { overwrite } else { None };
1188 let help = suggestion.is_none() && overwrite.is_none();
1189 tcx.emit_node_span_lint(
1190 lint::builtin::UNUSED_ASSIGNMENTS,
1191 hir_id,
1192 source_info.span,
1193 errors::UnusedAssign { name, overwrite, help, suggestion },
1194 )
1195 }
1196 AccessKind::Param => tcx.emit_node_span_lint(
1197 lint::builtin::UNUSED_ASSIGNMENTS,
1198 hir_id,
1199 source_info.span,
1200 errors::UnusedAssignPassed { name },
1201 ),
1202 AccessKind::Capture => tcx.emit_node_span_lint(
1203 lint::builtin::UNUSED_ASSIGNMENTS,
1204 hir_id,
1205 decl_span,
1206 errors::UnusedCaptureMaybeCaptureRef { name },
1207 ),
1208 }
1209 }
1210 }
1211 }
1212}
1213
1214impl ::std::fmt::Debug for PlaceIndex {
fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
fmt.write_fmt(format_args!("{0}", self.as_u32()))
}
}rustc_index::newtype_index! {
1215 pub struct PlaceIndex {}
1216}
1217
1218impl DebugWithContext<MaybeLivePlaces<'_, '_>> for PlaceIndex {
1219 fn fmt_with(
1220 &self,
1221 ctxt: &MaybeLivePlaces<'_, '_>,
1222 f: &mut std::fmt::Formatter<'_>,
1223 ) -> std::fmt::Result {
1224 std::fmt::Debug::fmt(&ctxt.checked_places.places[*self], f)
1225 }
1226}
1227
1228pub struct MaybeLivePlaces<'a, 'tcx> {
1229 tcx: TyCtxt<'tcx>,
1230 checked_places: &'a PlaceSet<'tcx>,
1231 capture_kind: CaptureKind,
1232 self_assignment: FxHashSet<Location>,
1233}
1234
1235impl<'tcx> MaybeLivePlaces<'_, 'tcx> {
1236 fn transfer_function<'a>(
1237 &'a self,
1238 trans: &'a mut DenseBitSet<PlaceIndex>,
1239 ) -> TransferFunction<'a, 'tcx> {
1240 TransferFunction {
1241 tcx: self.tcx,
1242 checked_places: &self.checked_places,
1243 capture_kind: self.capture_kind,
1244 trans,
1245 self_assignment: &self.self_assignment,
1246 }
1247 }
1248}
1249
1250impl<'tcx> Analysis<'tcx> for MaybeLivePlaces<'_, 'tcx> {
1251 type Domain = DenseBitSet<PlaceIndex>;
1252 type Direction = Backward;
1253
1254 const NAME: &'static str = "liveness-lint";
1255
1256 fn bottom_value(&self, _: &Body<'tcx>) -> Self::Domain {
1257 DenseBitSet::new_empty(self.checked_places.len())
1259 }
1260
1261 fn initialize_start_block(&self, _: &Body<'tcx>, _: &mut Self::Domain) {
1262 }
1264
1265 fn apply_primary_statement_effect(
1266 &self,
1267 trans: &mut Self::Domain,
1268 statement: &Statement<'tcx>,
1269 location: Location,
1270 ) {
1271 self.transfer_function(trans).visit_statement(statement, location);
1272 }
1273
1274 fn apply_primary_terminator_effect<'mir>(
1275 &self,
1276 trans: &mut Self::Domain,
1277 terminator: &'mir Terminator<'tcx>,
1278 location: Location,
1279 ) -> TerminatorEdges<'mir, 'tcx> {
1280 self.transfer_function(trans).visit_terminator(terminator, location);
1281 terminator.edges()
1282 }
1283
1284 fn apply_call_return_effect(
1285 &self,
1286 _trans: &mut Self::Domain,
1287 _block: BasicBlock,
1288 _return_places: CallReturnPlaces<'_, 'tcx>,
1289 ) {
1290 }
1292}
1293
1294struct TransferFunction<'a, 'tcx> {
1295 tcx: TyCtxt<'tcx>,
1296 checked_places: &'a PlaceSet<'tcx>,
1297 trans: &'a mut DenseBitSet<PlaceIndex>,
1298 capture_kind: CaptureKind,
1299 self_assignment: &'a FxHashSet<Location>,
1300}
1301
1302impl<'tcx> Visitor<'tcx> for TransferFunction<'_, 'tcx> {
1303 fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
1304 match statement.kind {
1305 StatementKind::FakeRead((
1308 FakeReadCause::ForLet(None) | FakeReadCause::ForGuardBinding,
1309 _,
1310 )) => return,
1311 StatementKind::Assign((ref dest, ref rvalue))
1313 if self.self_assignment.contains(&location) =>
1314 {
1315 if let Rvalue::BinaryOp(
1316 BinOp::AddWithOverflow | BinOp::SubWithOverflow | BinOp::MulWithOverflow,
1317 (_, rhs),
1318 ) = rvalue
1319 {
1320 self.visit_operand(rhs, location);
1324 self.visit_place(
1325 dest,
1326 PlaceContext::MutatingUse(MutatingUseContext::Store),
1327 location,
1328 );
1329 } else if let Rvalue::BinaryOp(_, (_, rhs)) = rvalue {
1330 self.visit_operand(rhs, location);
1334 } else {
1335 self.visit_rvalue(rvalue, location);
1340 }
1341 }
1342 _ => self.super_statement(statement, location),
1343 }
1344 }
1345
1346 fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
1347 match terminator.kind {
1350 TerminatorKind::Return
1351 | TerminatorKind::Yield { .. }
1352 | TerminatorKind::Goto { target: START_BLOCK } | TerminatorKind::Call { target: None, .. } if self.capture_kind != CaptureKind::None =>
1355 {
1356 for (index, place) in self.checked_places.iter() {
1358 if place.local == ty::CAPTURE_STRUCT_LOCAL
1359 && place.projection.last() == Some(&PlaceElem::Deref)
1360 {
1361 self.trans.insert(index);
1362 }
1363 }
1364 }
1365 TerminatorKind::Drop { .. } => {}
1367 TerminatorKind::Assert { .. } => {}
1369 _ => self.super_terminator(terminator, location),
1370 }
1371 }
1372
1373 fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
1374 match rvalue {
1375 Rvalue::Aggregate(
1379 AggregateKind::Closure(def_id, _) | AggregateKind::Coroutine(def_id, _),
1380 operands,
1381 ) => {
1382 if let Some(def_id) = def_id.as_local() {
1383 let dead_captures = self.tcx.check_liveness(def_id);
1384 for (field, operand) in
1385 operands.iter_enumerated().take(dead_captures.domain_size())
1386 {
1387 if !dead_captures.contains(field) {
1388 self.visit_operand(operand, location);
1389 }
1390 }
1391 }
1392 }
1393 _ => self.super_rvalue(rvalue, location),
1394 }
1395 }
1396
1397 fn visit_place(&mut self, place: &Place<'tcx>, context: PlaceContext, location: Location) {
1398 if let Some((index, extra_projections)) = self.checked_places.get(place.as_ref()) {
1399 for i in (extra_projections.len()..=place.projection.len()).rev() {
1400 let place_part =
1401 PlaceRef { local: place.local, projection: &place.projection[..i] };
1402 let extra_projections = &place.projection[i..];
1403
1404 if let Some(&elem) = extra_projections.get(0) {
1405 self.visit_projection_elem(place_part, elem, context, location);
1406 }
1407 }
1408
1409 match DefUse::for_place(extra_projections, context) {
1410 Some(DefUse::Def) => {
1411 self.trans.remove(index);
1412 }
1413 Some(DefUse::Use) => {
1414 self.trans.insert(index);
1415 }
1416 None => {}
1417 }
1418 } else {
1419 self.super_place(place, context, location)
1420 }
1421 }
1422
1423 fn visit_local(&mut self, local: Local, context: PlaceContext, _: Location) {
1424 if let Some((index, _proj)) = self.checked_places.get(local.into()) {
1425 if true {
match (&_proj, &&[]) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
};
};debug_assert_eq!(_proj, &[]);
1426 match DefUse::for_place(&[], context) {
1427 Some(DefUse::Def) => {
1428 self.trans.remove(index);
1429 }
1430 Some(DefUse::Use) => {
1431 self.trans.insert(index);
1432 }
1433 _ => {}
1434 }
1435 }
1436 }
1437}
1438
1439#[derive(#[automatically_derived]
impl ::core::cmp::Eq for DefUse {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {}
}Eq, #[automatically_derived]
impl ::core::cmp::PartialEq for DefUse {
#[inline]
fn eq(&self, other: &DefUse) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq, #[automatically_derived]
impl ::core::fmt::Debug for DefUse {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self { DefUse::Def => "Def", DefUse::Use => "Use", })
}
}Debug, #[automatically_derived]
impl ::core::clone::Clone for DefUse {
#[inline]
fn clone(&self) -> DefUse {
match self { DefUse::Def => DefUse::Def, DefUse::Use => DefUse::Use, }
}
}Clone)]
1440enum DefUse {
1441 Def,
1442 Use,
1443}
1444
1445fn is_indirect(proj: &[PlaceElem<'_>]) -> bool {
1446 proj.iter().any(|p| p.is_indirect())
1447}
1448
1449impl DefUse {
1450 fn for_place<'tcx>(projection: &[PlaceElem<'tcx>], context: PlaceContext) -> Option<DefUse> {
1451 let is_indirect = is_indirect(projection);
1452 match context {
1453 PlaceContext::MutatingUse(
1454 MutatingUseContext::Store | MutatingUseContext::SetDiscriminant,
1455 ) => {
1456 if is_indirect {
1457 Some(DefUse::Use)
1460 } else if projection.is_empty() {
1461 Some(DefUse::Def)
1462 } else {
1463 None
1464 }
1465 }
1466
1467 PlaceContext::MutatingUse(
1472 MutatingUseContext::Call
1473 | MutatingUseContext::Yield
1474 | MutatingUseContext::AsmOutput,
1475 ) => is_indirect.then_some(DefUse::Use),
1476
1477 PlaceContext::MutatingUse(
1479 MutatingUseContext::RawBorrow
1480 | MutatingUseContext::Borrow
1481 | MutatingUseContext::Drop
1482 | MutatingUseContext::Retag,
1483 )
1484 | PlaceContext::NonMutatingUse(
1485 NonMutatingUseContext::RawBorrow
1486 | NonMutatingUseContext::Copy
1487 | NonMutatingUseContext::Inspect
1488 | NonMutatingUseContext::Move
1489 | NonMutatingUseContext::FakeBorrow
1490 | NonMutatingUseContext::SharedBorrow
1491 | NonMutatingUseContext::PlaceMention,
1492 ) => Some(DefUse::Use),
1493
1494 PlaceContext::NonUse(
1495 NonUseContext::StorageLive
1496 | NonUseContext::StorageDead
1497 | NonUseContext::AscribeUserTy(_)
1498 | NonUseContext::BackwardIncompatibleDropHint
1499 | NonUseContext::VarDebugInfo,
1500 ) => None,
1501
1502 PlaceContext::MutatingUse(MutatingUseContext::Projection)
1503 | PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection) => {
1504 {
::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
format_args!("A projection could be a def or a use and must be handled separately")));
}unreachable!("A projection could be a def or a use and must be handled separately")
1505 }
1506 }
1507 }
1508}