Add match-based manual try to clippy::question_mark (#13627)

Closes #10.

changelog: [`question_mark`]: Now lints for match-based manual try
This commit is contained in:
Timo 2024-11-10 16:26:56 +00:00 committed by GitHub
commit f58088b23e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 347 additions and 13 deletions

View file

@ -96,12 +96,42 @@ impl MoveStruct {
}
fn func() -> Option<i32> {
macro_rules! opt_none {
() => {
None
};
}
fn f() -> Option<String> {
Some(String::new())
}
f()?;
let _val = f()?;
let s: &str = match &Some(String::new()) {
Some(v) => v,
None => return None,
};
f()?;
opt_none!()?;
match f() {
Some(x) => x,
None => return opt_none!(),
};
match f() {
Some(val) => {
println!("{val}");
val
},
None => return None,
};
Some(0)
}
@ -114,6 +144,10 @@ fn result_func(x: Result<i32, i32>) -> Result<i32, i32> {
x?;
let _val = func_returning_result()?;
func_returning_result()?;
// No warning
let y = if let Ok(x) = x {
x
@ -157,6 +191,28 @@ fn result_func(x: Result<i32, i32>) -> Result<i32, i32> {
Ok(y)
}
fn infer_check() {
let closure = |x: Result<u8, ()>| {
// `?` would fail here, as it expands to `Err(val.into())` which is not constrained.
let _val = match x {
Ok(val) => val,
Err(val) => return Err(val),
};
Ok(())
};
let closure = |x: Result<u8, ()>| -> Result<(), _> {
// `?` would fail here, as it expands to `Err(val.into())` which is not constrained.
let _val = match x {
Ok(val) => val,
Err(val) => return Err(val),
};
Ok(())
};
}
// see issue #8019
pub enum NotOption {
None,

View file

@ -124,6 +124,12 @@ impl MoveStruct {
}
fn func() -> Option<i32> {
macro_rules! opt_none {
() => {
None
};
}
fn f() -> Option<String> {
Some(String::new())
}
@ -132,6 +138,39 @@ fn func() -> Option<i32> {
return None;
}
let _val = match f() {
Some(val) => val,
None => return None,
};
let s: &str = match &Some(String::new()) {
Some(v) => v,
None => return None,
};
match f() {
Some(val) => val,
None => return None,
};
match opt_none!() {
Some(x) => x,
None => return None,
};
match f() {
Some(x) => x,
None => return opt_none!(),
};
match f() {
Some(val) => {
println!("{val}");
val
},
None => return None,
};
Some(0)
}
@ -146,6 +185,16 @@ fn result_func(x: Result<i32, i32>) -> Result<i32, i32> {
return x;
}
let _val = match func_returning_result() {
Ok(val) => val,
Err(err) => return Err(err),
};
match func_returning_result() {
Ok(val) => val,
Err(err) => return Err(err),
};
// No warning
let y = if let Ok(x) = x {
x
@ -189,6 +238,28 @@ fn result_func(x: Result<i32, i32>) -> Result<i32, i32> {
Ok(y)
}
fn infer_check() {
let closure = |x: Result<u8, ()>| {
// `?` would fail here, as it expands to `Err(val.into())` which is not constrained.
let _val = match x {
Ok(val) => val,
Err(val) => return Err(val),
};
Ok(())
};
let closure = |x: Result<u8, ()>| -> Result<(), _> {
// `?` would fail here, as it expands to `Err(val.into())` which is not constrained.
let _val = match x {
Ok(val) => val,
Err(val) => return Err(val),
};
Ok(())
};
}
// see issue #8019
pub enum NotOption {
None,

View file

@ -94,29 +94,76 @@ LL | | };
| |_________^ help: replace it with: `self.opt?`
error: this block may be rewritten with the `?` operator
--> tests/ui/question_mark.rs:131:5
--> tests/ui/question_mark.rs:137:5
|
LL | / if f().is_none() {
LL | | return None;
LL | | }
| |_____^ help: replace it with: `f()?;`
error: this `match` expression can be replaced with `?`
--> tests/ui/question_mark.rs:141:16
|
LL | let _val = match f() {
| ________________^
LL | | Some(val) => val,
LL | | None => return None,
LL | | };
| |_____^ help: try instead: `f()?`
error: this `match` expression can be replaced with `?`
--> tests/ui/question_mark.rs:151:5
|
LL | / match f() {
LL | | Some(val) => val,
LL | | None => return None,
LL | | };
| |_____^ help: try instead: `f()?`
error: this `match` expression can be replaced with `?`
--> tests/ui/question_mark.rs:156:5
|
LL | / match opt_none!() {
LL | | Some(x) => x,
LL | | None => return None,
LL | | };
| |_____^ help: try instead: `opt_none!()?`
error: this block may be rewritten with the `?` operator
--> tests/ui/question_mark.rs:143:13
--> tests/ui/question_mark.rs:182:13
|
LL | let _ = if let Ok(x) = x { x } else { return x };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `x?`
error: this block may be rewritten with the `?` operator
--> tests/ui/question_mark.rs:145:5
--> tests/ui/question_mark.rs:184:5
|
LL | / if x.is_err() {
LL | | return x;
LL | | }
| |_____^ help: replace it with: `x?;`
error: this `match` expression can be replaced with `?`
--> tests/ui/question_mark.rs:188:16
|
LL | let _val = match func_returning_result() {
| ________________^
LL | | Ok(val) => val,
LL | | Err(err) => return Err(err),
LL | | };
| |_____^ help: try instead: `func_returning_result()?`
error: this `match` expression can be replaced with `?`
--> tests/ui/question_mark.rs:193:5
|
LL | / match func_returning_result() {
LL | | Ok(val) => val,
LL | | Err(err) => return Err(err),
LL | | };
| |_____^ help: try instead: `func_returning_result()?`
error: this block may be rewritten with the `?` operator
--> tests/ui/question_mark.rs:213:5
--> tests/ui/question_mark.rs:284:5
|
LL | / if let Err(err) = func_returning_result() {
LL | | return Err(err);
@ -124,7 +171,7 @@ LL | | }
| |_____^ help: replace it with: `func_returning_result()?;`
error: this block may be rewritten with the `?` operator
--> tests/ui/question_mark.rs:220:5
--> tests/ui/question_mark.rs:291:5
|
LL | / if let Err(err) = func_returning_result() {
LL | | return Err(err);
@ -132,7 +179,7 @@ LL | | }
| |_____^ help: replace it with: `func_returning_result()?;`
error: this block may be rewritten with the `?` operator
--> tests/ui/question_mark.rs:297:13
--> tests/ui/question_mark.rs:368:13
|
LL | / if a.is_none() {
LL | | return None;
@ -142,12 +189,12 @@ LL | | }
| |_____________^ help: replace it with: `a?;`
error: this `let...else` may be rewritten with the `?` operator
--> tests/ui/question_mark.rs:357:5
--> tests/ui/question_mark.rs:428:5
|
LL | / let Some(v) = bar.foo.owned.clone() else {
LL | | return None;
LL | | };
| |______^ help: replace it with: `let v = bar.foo.owned.clone()?;`
error: aborting due to 17 previous errors
error: aborting due to 22 previous errors