Skip to content

Commit 492d1cb

Browse files
committed
qualify_min_const_fn: ignore cleanup bbs
1 parent 0129b93 commit 492d1cb

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

clippy_utils/src/qualify_min_const_fn.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,13 @@ pub fn is_min_const_fn<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, msrv: &Msrv)
4040
)?;
4141

4242
for bb in &*body.basic_blocks {
43-
check_terminator(tcx, body, bb.terminator(), msrv)?;
44-
for stmt in &bb.statements {
45-
check_statement(tcx, body, def_id, stmt)?;
43+
// Cleanup blocks are ignored entirely by const eval, so we can too:
44+
// https://github.com/rust-lang/rust/blob/1dea922ea6e74f99a0e97de5cdb8174e4dea0444/compiler/rustc_const_eval/src/transform/check_consts/check.rs#L382
45+
if !bb.is_cleanup {
46+
check_terminator(tcx, body, bb.terminator(), msrv)?;
47+
for stmt in &bb.statements {
48+
check_statement(tcx, body, def_id, stmt)?;
49+
}
4650
}
4751
}
4852
Ok(())

tests/ui/missing_const_for_fn/could_be_const.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,16 @@ mod issue12677 {
130130
Self { strings: Vec::new() }
131131
}
132132
}
133+
134+
pub struct Other {
135+
pub text: String,
136+
pub vec: Vec<String>,
137+
}
138+
139+
impl Other {
140+
pub fn new(text: String) -> Self {
141+
let vec = Vec::new();
142+
Self { text, vec }
143+
}
144+
}
133145
}

tests/ui/missing_const_for_fn/could_be_const.stderr

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,5 +118,14 @@ LL | | Self { strings: Vec::new() }
118118
LL | | }
119119
| |_________^
120120

121-
error: aborting due to 13 previous errors
121+
error: this could be a `const fn`
122+
--> tests/ui/missing_const_for_fn/could_be_const.rs:140:9
123+
|
124+
LL | / pub fn new(text: String) -> Self {
125+
LL | | let vec = Vec::new();
126+
LL | | Self { text, vec }
127+
LL | | }
128+
| |_________^
129+
130+
error: aborting due to 14 previous errors
122131

0 commit comments

Comments
 (0)