rust-lang / rust-lang/rust-clippy
single_match: recongnize "Ok(_) => {}" or "Err(_bla) => {}" as "_ => {}"
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
If we match a Result<x,y> , and we see that one of the results is ignored via
Ok(_) => {} or Err(_bla) => {}, can we somehow simplify this to _ => {} ?
The idea is that since we know that Result will either return Ok or Error, no other values are possible, which is why I think
match x {
Ok(_) => {},
Err(a) => { do_something(a) },
}
is equivalent to
match x {
Err(a) => { do_something(a) },
_ => {},
}
however, the second example will trigger single_match lint while the first one will not.
code sample:
fn main() {
test1();
test2();
test3();
test4();
}
fn test1() {
// false negative
match is_ok(3) {
Ok(_a) => {}
Err(bad) => {
println!("very bad: {}", bad);
}
}
}
fn test2() {
// false negative
match is_ok(3) {
Ok(_) => {}
Err(bad) => {
println!("very bad: {}", bad);
}
}
}
fn test3() {
// true positive
match is_ok(3) {
Err(bad) => {
println!("very bad: {}", bad);
}
_ => {}
}
}
fn test4() {
// true positive
match is_ok(3) {
Ok(good) => {
println!("very good: {}", good);
}
_ => {}
}
}
fn is_ok(x: i32) -> Result<bool, String> {
if x > 1 {
Ok(true)
} else {
Err("Oh no".to_string())
}
}
Does this make sense?
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 single_match lint and compare the four match examples in the issue, especially the Ok(_) and Err(_bla) arms versus the wildcard arm. Trace how the lint decides whether a match has a single meaningful arm, then add coverage for the requested Result patterns; done means the false negatives are recognized without changing the true-positive cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100