Skip to content

manual-assert mishandles if macro! { panic!() } #15227

@matthiaskrgr

Description

@matthiaskrgr

Using the following flags

--force-warn clippy::manual-assert

this code:

pub fn add(left: u64, right: u64) -> u64 {
    if !std::arch::is_x86_feature_detected!("ssse3") {
        panic!("SSSE3 is not supported");
    }
    unsafe { add_with_ssse3(left, right) }
}

#[target_feature(enable = "ssse3")]
pub unsafe fn add_with_ssse3(left: u64, right: u64) -> u64 {
    left + right
}

caused the following diagnostics:

    Checking _a v0.1.0 (/tmp/icemaker_global_tempdir.aNI5ROlsd82n/icemaker_clippyfix_tempdir.JIvgUU4RPjA0/_a)
warning: only a `panic!` in `if`-then statement
 --> src/lib.rs:2:5
  |
2 | /     if !std::arch::is_x86_feature_detected!("ssse3") {
3 | |         panic!("SSSE3 is not supported");
4 | |     }
  | |_____^ help: try instead: `assert!((cfg!(target_feature = $target_feature_lit) || $feature), "SSSE3 is not supported");`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_assert
  = note: requested on the command line with `--force-warn clippy::manual-assert`

warning: `_a` (lib) generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.21s

However after applying these diagnostics, the resulting code:

pub fn add(left: u64, right: u64) -> u64 {
    assert!((cfg!(target_feature = $target_feature_lit) || $feature), "SSSE3 is not supported");
    unsafe { add_with_ssse3(left, right) }
}

#[target_feature(enable = "ssse3")]
pub unsafe fn add_with_ssse3(left: u64, right: u64) -> u64 {
    left + right
}

no longer compiled:

    Checking _a v0.1.0 (/tmp/icemaker_global_tempdir.aNI5ROlsd82n/icemaker_clippyfix_tempdir.JIvgUU4RPjA0/_a)
error: expected expression, found `$`
 --> src/lib.rs:2:60
  |
2 |     assert!((cfg!(target_feature = $target_feature_lit) || $feature), "SSSE3 is not supported");
  |                                                            ^ expected expression

error: could not compile `_a` (lib test) due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `_a` (lib) due to 1 previous error

Version:

rustc 1.90.0-nightly (2f8eeb2bb 2025-07-07)
binary: rustc
commit-hash: 2f8eeb2bba86b8f457ec602c578473c711f85628
commit-date: 2025-07-07
host: x86_64-unknown-linux-gnu
release: 1.90.0-nightly
LLVM version: 20.1.7

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedT-macrosType: Issues with macros and macro expansion

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions