rust-lang / rust-lang/rust-clippy
question_mark suggestion changes code semantics
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Summary
If let expression that produces a result is suggested to change to ?, changing the semantics of the code
Lint Name
question_mark
Reproducer
I tried this code:
fn do_stuff() -> Result<(), ()> {
let result = if let Err(e) = op1() { Err(e) } else { Ok(()) };
do_something_with_result(&result);
result
}
fn op1() -> Result<(), ()> {
Err(())
}
fn do_something_with_result(arg: &Result<(), ()>) {
println!("doing something with the result {arg:?}");
}
fn main() {
do_stuff().unwrap();
}
}
I saw this happen:
warning: this block may be rewritten with the `?` operator
--> src/main.rs:2:18
|
2 | let result = if let Err(e) = op1() { Err(e) } else { Ok(()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `op1()?`
|
= note: `#[warn(clippy::question_mark)]` on by default
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#question_mark
I expected to see this happen:
There should be no warning. Changing the code for what clippy suggests would prevent calling do_something_with_result when the result is an error
Version
rustc 1.64.0 (a55dd71d5 2022-09-19)
binary: rustc
commit-hash: a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52
commit-date: 2022-09-19
host: x86_64-unknown-linux-gnu
release: 1.64.0
LLVM version: 14.0.6
Additional Labels
No response
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 with the question_mark lint and run the supplied Rust reproducer. Compare the suggested ? rewrite with the original control flow, especially whether do_something_with_result still runs, and verify that the corrected lint behavior preserves the intended semantics.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100