rust-lang / rust-lang/rust-clippy
Replacement suggested by manual_assert discards attributes
Open
Nobody has claimed this yet.
C-bug
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
fn main() {
let x = 0;
if x == 0 {
#[cfg(not(feature = "less_crashy"))]
panic!("huh");
}
}
$ cargo clippy -- -Wclippy::pedantic
warning: only a `panic!` in `if`-then statement
--> src/main.rs:3:5
|
3 | / if x == 0 {
4 | | #[cfg(not(feature = "less_crashy"))]
5 | | panic!("huh");
6 | | }
| |_____^ help: try: `assert!(!(x == 0), "huh");`
|
= note: `-W clippy::manual-assert` implied by `-W clippy::pedantic`
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_assert
Would it be possible to suggest the following?
+ #[cfg(not(feature = "less_crashy"))]
+ assert!(..., "huh");
In fact this may not be equivalent if the branch condition has side effects, so maybe we should even consider this a false positive and not lint at all.
Meta
Rust version (rustc -Vv):
rustc 1.58.0-nightly (4961b107f 2021-11-04)
binary: rustc
commit-hash: 4961b107f204e15b26961eab0685df6be3ab03c6
commit-date: 2021-11-04
host: x86_64-unknown-linux-gnu
release: 1.58.0-nightly
LLVM version: 13.0.0
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the manual_assert lint with the example and cargo clippy -- -Wclippy::pedantic. Read the manual_assert lint implementation and its existing tests, then determine whether the completed behavior should preserve the cfg attribute in the suggestion or avoid linting cases where condition side effects could change semantics.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100