mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-28 15:41:10 +00:00
manual-unwrap-or / pr remarks, round 2
This commit is contained in:
parent
f2da0c701e
commit
6533d8becf
4 changed files with 29 additions and 7 deletions
|
@ -111,7 +111,9 @@ fn lint_manual_unwrap_or<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
|
|||
then {
|
||||
let reindented_or_body =
|
||||
utils::reindent_multiline(or_body_snippet.into(), true, Some(indent));
|
||||
let wrap_in_parens = !matches!(scrutinee, Expr { kind: ExprKind::Call(..), .. });
|
||||
let wrap_in_parens = !matches!(scrutinee, Expr {
|
||||
kind: ExprKind::Call(..) | ExprKind::Path(_), ..
|
||||
});
|
||||
let l_paren = if wrap_in_parens { "(" } else { "" };
|
||||
let r_paren = if wrap_in_parens { ")" } else { "" };
|
||||
utils::span_lint_and_sugg(
|
||||
|
|
|
@ -70,6 +70,10 @@ fn result_unwrap_or() {
|
|||
// int case
|
||||
Ok::<i32, &str>(1).unwrap_or(42);
|
||||
|
||||
// int case, scrutinee is a binding
|
||||
let a = Ok::<i32, &str>(1);
|
||||
a.unwrap_or(42);
|
||||
|
||||
// int case, suggestion must surround with parenthesis
|
||||
(Ok(1) as Result<i32, &str>).unwrap_or(42);
|
||||
|
||||
|
|
|
@ -88,6 +88,13 @@ fn result_unwrap_or() {
|
|||
Err(_) => 42,
|
||||
};
|
||||
|
||||
// int case, scrutinee is a binding
|
||||
let a = Ok::<i32, &str>(1);
|
||||
match a {
|
||||
Ok(i) => i,
|
||||
Err(_) => 42,
|
||||
};
|
||||
|
||||
// int case, suggestion must surround with parenthesis
|
||||
match Ok(1) as Result<i32, &str> {
|
||||
Ok(i) => i,
|
||||
|
|
|
@ -67,7 +67,16 @@ LL | | };
|
|||
| |_____^ help: replace with: `Ok::<i32, &str>(1).unwrap_or(42)`
|
||||
|
||||
error: this pattern reimplements `Result::unwrap_or`
|
||||
--> $DIR/manual_unwrap_or.rs:92:5
|
||||
--> $DIR/manual_unwrap_or.rs:93:5
|
||||
|
|
||||
LL | / match a {
|
||||
LL | | Ok(i) => i,
|
||||
LL | | Err(_) => 42,
|
||||
LL | | };
|
||||
| |_____^ help: replace with: `a.unwrap_or(42)`
|
||||
|
||||
error: this pattern reimplements `Result::unwrap_or`
|
||||
--> $DIR/manual_unwrap_or.rs:99:5
|
||||
|
|
||||
LL | / match Ok(1) as Result<i32, &str> {
|
||||
LL | | Ok(i) => i,
|
||||
|
@ -76,7 +85,7 @@ LL | | };
|
|||
| |_____^ help: replace with: `(Ok(1) as Result<i32, &str>).unwrap_or(42)`
|
||||
|
||||
error: this pattern reimplements `Result::unwrap_or`
|
||||
--> $DIR/manual_unwrap_or.rs:98:5
|
||||
--> $DIR/manual_unwrap_or.rs:105:5
|
||||
|
|
||||
LL | / match Ok::<i32, &str>(1) {
|
||||
LL | | Err(_) => 42,
|
||||
|
@ -85,7 +94,7 @@ LL | | };
|
|||
| |_____^ help: replace with: `Ok::<i32, &str>(1).unwrap_or(42)`
|
||||
|
||||
error: this pattern reimplements `Result::unwrap_or`
|
||||
--> $DIR/manual_unwrap_or.rs:104:5
|
||||
--> $DIR/manual_unwrap_or.rs:111:5
|
||||
|
|
||||
LL | / match Ok::<i32, &str>(1) {
|
||||
LL | | Ok(i) => i,
|
||||
|
@ -94,7 +103,7 @@ LL | | };
|
|||
| |_____^ help: replace with: `Ok::<i32, &str>(1).unwrap_or(1 + 42)`
|
||||
|
||||
error: this pattern reimplements `Result::unwrap_or`
|
||||
--> $DIR/manual_unwrap_or.rs:111:5
|
||||
--> $DIR/manual_unwrap_or.rs:118:5
|
||||
|
|
||||
LL | / match Ok::<i32, &str>(1) {
|
||||
LL | | Ok(i) => i,
|
||||
|
@ -115,7 +124,7 @@ LL | });
|
|||
|
|
||||
|
||||
error: this pattern reimplements `Result::unwrap_or`
|
||||
--> $DIR/manual_unwrap_or.rs:121:5
|
||||
--> $DIR/manual_unwrap_or.rs:128:5
|
||||
|
|
||||
LL | / match Ok::<&str, &str>("Bob") {
|
||||
LL | | Ok(i) => i,
|
||||
|
@ -123,5 +132,5 @@ LL | | Err(_) => "Alice",
|
|||
LL | | };
|
||||
| |_____^ help: replace with: `Ok::<&str, &str>("Bob").unwrap_or("Alice")`
|
||||
|
||||
error: aborting due to 11 previous errors
|
||||
error: aborting due to 12 previous errors
|
||||
|
||||
|
|
Loading…
Reference in a new issue