Skip to content

Commit c562caf

Browse files
Add FakeRead::ForLetUnderscore
We use it for the RHS of statements like `let _ = x`.
1 parent acfc823 commit c562caf

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

src/librustc_middle/mir/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,6 +1717,14 @@ pub enum FakeReadCause {
17171717
/// appropriate errors.
17181718
ForLet,
17191719

1720+
/// This denotes the existence of a statement like
1721+
///
1722+
/// `let _ = <expr>;`
1723+
///
1724+
/// which would not otherwise be recorded in the MIR. This is required for to
1725+
/// implement the unused variable lint on the MIR. It should be ignored elsewhere.
1726+
ForLetUnderscore,
1727+
17201728
/// If we have an index expression like
17211729
///
17221730
/// (*x)[1][{ x = y; 4}]

src/librustc_mir/borrow_check/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_index::bit_set::BitSet;
1212
use rustc_index::vec::IndexVec;
1313
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
1414
use rustc_middle::mir::{
15-
traversal, Body, ClearCrossCrate, Local, Location, Mutability, Operand, Place, PlaceElem,
15+
self, traversal, Body, ClearCrossCrate, Local, Location, Mutability, Operand, Place, PlaceElem,
1616
PlaceRef,
1717
};
1818
use rustc_middle::mir::{AggregateKind, BasicBlock, BorrowCheckResult, BorrowKind};
@@ -571,6 +571,10 @@ impl<'cx, 'tcx> dataflow::ResultsVisitor<'cx, 'tcx> for MirBorrowckCtxt<'cx, 'tc
571571

572572
self.mutate_place(location, (*lhs, span), Shallow(None), JustWrite, flow_state);
573573
}
574+
575+
// `ForLetUnderscore` is only intended for the unused variables lint.
576+
StatementKind::FakeRead(mir::FakeReadCause::ForLetUnderscore, _) => {}
577+
574578
StatementKind::FakeRead(_, box ref place) => {
575579
// Read for match doesn't access any memory and is used to
576580
// assert that a place is safe and live. So we don't have to

src/librustc_mir/transform/check_consts/validation.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,10 @@ impl Visitor<'tcx> for Validator<'mir, 'tcx> {
514514
self.check_op(ops::InlineAsm);
515515
}
516516

517-
StatementKind::FakeRead(FakeReadCause::ForLet | FakeReadCause::ForIndex, _)
517+
StatementKind::FakeRead(
518+
FakeReadCause::ForLet | FakeReadCause::ForIndex | FakeReadCause::ForLetUnderscore,
519+
_,
520+
)
518521
| StatementKind::StorageLive(_)
519522
| StatementKind::StorageDead(_)
520523
| StatementKind::Retag { .. }

src/librustc_mir_build/build/matches/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,20 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
439439
block.unit()
440440
}
441441

442+
// Handle bindings like `let _ = var;` by lowering them to a fake read of the RHS. This
443+
// is needed by the unused variables lint pass.
444+
PatKind::Wild => {
445+
let place = unpack!(block = self.as_place(block, initializer));
446+
debug!("expr_into_pattern(place = {:?}, PatKind::Wild)", place);
447+
448+
if place.as_local().is_some() {
449+
let source_info = self.source_info(irrefutable_pat.span);
450+
self.cfg.push_fake_read(block, source_info, FakeReadCause::ForLetUnderscore, place);
451+
}
452+
453+
block.unit()
454+
}
455+
442456
_ => {
443457
let place = unpack!(block = self.as_place(block, initializer));
444458
self.place_into_pattern(block, irrefutable_pat, place, true)

0 commit comments

Comments
 (0)