Skip to content

Commit

Permalink
qualify_min_const_fn: ignore cleanup bbs
Browse files Browse the repository at this point in the history
  • Loading branch information
y21 committed May 15, 2024
1 parent 763e334 commit bd47189
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
10 changes: 7 additions & 3 deletions clippy_utils/src/qualify_min_const_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@ pub fn is_min_const_fn<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, msrv: &Msrv)
)?;

for bb in &*body.basic_blocks {
check_terminator(tcx, body, bb.terminator(), msrv)?;
for stmt in &bb.statements {
check_statement(tcx, body, def_id, stmt, msrv)?;
// Cleanup blocks are ignored entirely by const eval, so we can too:
// https://github.com/rust-lang/rust/blob/1dea922ea6e74f99a0e97de5cdb8174e4dea0444/compiler/rustc_const_eval/src/transform/check_consts/check.rs#L382
if !bb.is_cleanup {
check_terminator(tcx, body, bb.terminator(), msrv)?;
for stmt in &bb.statements {
check_statement(tcx, body, def_id, stmt, msrv)?;
}
}
}
Ok(())
Expand Down
12 changes: 12 additions & 0 deletions tests/ui/missing_const_for_fn/could_be_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,16 @@ mod issue12677 {
Self { strings: Vec::new() }
}
}

pub struct Other {
pub text: String,
pub vec: Vec<String>,
}

impl Other {
pub fn new(text: String) -> Self {
let vec = Vec::new();
Self { text, vec }
}
}
}
11 changes: 10 additions & 1 deletion tests/ui/missing_const_for_fn/could_be_const.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,14 @@ LL | | Self { strings: Vec::new() }
LL | | }
| |_________^

error: aborting due to 16 previous errors
error: this could be a `const fn`
--> tests/ui/missing_const_for_fn/could_be_const.rs:168:9
|
LL | / pub fn new(text: String) -> Self {
LL | | let vec = Vec::new();
LL | | Self { text, vec }
LL | | }
| |_________^

error: aborting due to 17 previous errors

0 comments on commit bd47189

Please sign in to comment.