Skip to content

Commit e0e204f

Browse files
committed
Auto merge of #149096 - chenyukang:yukang-fix-unused-vars-148373, r=oli-obk
Skip unused variables warning for unreachable code Fixes #148373 These warnings are not reported on stable branch, but are now reported on the beta. I tried another solution to record whether a `local` is reachable in `find_dead_assignments`, but the code in this PR seems simpler. r? `@cjgillot`
2 parents 94b49fd + 2c6b1d3 commit e0e204f

File tree

5 files changed

+91
-11
lines changed

5 files changed

+91
-11
lines changed

compiler/rustc_mir_transform/src/liveness.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,33 @@ impl<'a, 'tcx> AssignmentResult<'a, 'tcx> {
841841
dead_captures
842842
}
843843

844+
/// Check if a local is referenced in any reachable basic block.
845+
/// Variables in unreachable code (e.g., after `todo!()`) should not trigger unused warnings.
846+
fn is_local_in_reachable_code(&self, local: Local) -> bool {
847+
struct LocalVisitor {
848+
target_local: Local,
849+
found: bool,
850+
}
851+
852+
impl<'tcx> Visitor<'tcx> for LocalVisitor {
853+
fn visit_local(&mut self, local: Local, _context: PlaceContext, _location: Location) {
854+
if local == self.target_local {
855+
self.found = true;
856+
}
857+
}
858+
}
859+
860+
let mut visitor = LocalVisitor { target_local: local, found: false };
861+
for (bb, bb_data) in traversal::postorder(self.body) {
862+
visitor.visit_basic_block_data(bb, bb_data);
863+
if visitor.found {
864+
return true;
865+
}
866+
}
867+
868+
false
869+
}
870+
844871
/// Report fully unused locals, and forget the corresponding assignments.
845872
fn report_fully_unused(&mut self) {
846873
let tcx = self.tcx;
@@ -932,6 +959,10 @@ impl<'a, 'tcx> AssignmentResult<'a, 'tcx> {
932959

933960
let statements = &mut self.assignments[index];
934961
if statements.is_empty() {
962+
if !self.is_local_in_reachable_code(local) {
963+
continue;
964+
}
965+
935966
let sugg = if from_macro {
936967
errors::UnusedVariableSugg::NoSugg { span: def_span, name }
937968
} else {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//@ check-pass
2+
3+
#![allow(unreachable_code)]
4+
#![allow(dead_code)]
5+
#![warn(unused_variables)]
6+
7+
fn after_todo() {
8+
todo!("not implemented");
9+
10+
// This should not warn - the code is unreachable
11+
let a = 1;
12+
if a < 2 {
13+
eprintln!("a: {}", a);
14+
}
15+
}
16+
17+
fn after_panic() {
18+
panic!("oops");
19+
20+
// This should not warn - the code is unreachable
21+
let b = 2;
22+
println!("{}", b);
23+
}
24+
25+
fn after_unimplemented() {
26+
unimplemented!();
27+
28+
// This should not warn - the code is unreachable
29+
let c = 3;
30+
println!("{}", c);
31+
}
32+
33+
fn after_unreachable() {
34+
unsafe { std::hint::unreachable_unchecked() }
35+
36+
// This should not warn - the code is unreachable
37+
let d = 4;
38+
println!("{}", d);
39+
}
40+
41+
fn reachable_unused() {
42+
// This SHOULD warn - the code is reachable
43+
let e = 5; //~ WARN unused variable: `e`
44+
}
45+
46+
fn main() {}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
warning: unused variable: `e`
2+
--> $DIR/unused-var-in-unreachable-code.rs:43:9
3+
|
4+
LL | let e = 5;
5+
| ^ help: if this is intentional, prefix it with an underscore: `_e`
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/unused-var-in-unreachable-code.rs:5:9
9+
|
10+
LL | #![warn(unused_variables)]
11+
| ^^^^^^^^^^^^^^^^
12+
13+
warning: 1 warning emitted
14+

tests/ui/return/early-return-with-unreachable-code-24353.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ fn main() {
44
return ();
55

66
let x = ();
7-
//~^ WARN unused variable: `x`
87
x
98
}
109

tests/ui/return/early-return-with-unreachable-code-24353.stderr

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)