Skip to content

Commit 6533d8b

Browse files
committed
manual-unwrap-or / pr remarks, round 2
1 parent f2da0c7 commit 6533d8b

File tree

4 files changed

+29
-7
lines changed

4 files changed

+29
-7
lines changed

clippy_lints/src/manual_unwrap_or.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ fn lint_manual_unwrap_or<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
111111
then {
112112
let reindented_or_body =
113113
utils::reindent_multiline(or_body_snippet.into(), true, Some(indent));
114-
let wrap_in_parens = !matches!(scrutinee, Expr { kind: ExprKind::Call(..), .. });
114+
let wrap_in_parens = !matches!(scrutinee, Expr {
115+
kind: ExprKind::Call(..) | ExprKind::Path(_), ..
116+
});
115117
let l_paren = if wrap_in_parens { "(" } else { "" };
116118
let r_paren = if wrap_in_parens { ")" } else { "" };
117119
utils::span_lint_and_sugg(

tests/ui/manual_unwrap_or.fixed

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ fn result_unwrap_or() {
7070
// int case
7171
Ok::<i32, &str>(1).unwrap_or(42);
7272

73+
// int case, scrutinee is a binding
74+
let a = Ok::<i32, &str>(1);
75+
a.unwrap_or(42);
76+
7377
// int case, suggestion must surround with parenthesis
7478
(Ok(1) as Result<i32, &str>).unwrap_or(42);
7579

tests/ui/manual_unwrap_or.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ fn result_unwrap_or() {
8888
Err(_) => 42,
8989
};
9090

91+
// int case, scrutinee is a binding
92+
let a = Ok::<i32, &str>(1);
93+
match a {
94+
Ok(i) => i,
95+
Err(_) => 42,
96+
};
97+
9198
// int case, suggestion must surround with parenthesis
9299
match Ok(1) as Result<i32, &str> {
93100
Ok(i) => i,

tests/ui/manual_unwrap_or.stderr

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,16 @@ LL | | };
6767
| |_____^ help: replace with: `Ok::<i32, &str>(1).unwrap_or(42)`
6868

6969
error: this pattern reimplements `Result::unwrap_or`
70-
--> $DIR/manual_unwrap_or.rs:92:5
70+
--> $DIR/manual_unwrap_or.rs:93:5
71+
|
72+
LL | / match a {
73+
LL | | Ok(i) => i,
74+
LL | | Err(_) => 42,
75+
LL | | };
76+
| |_____^ help: replace with: `a.unwrap_or(42)`
77+
78+
error: this pattern reimplements `Result::unwrap_or`
79+
--> $DIR/manual_unwrap_or.rs:99:5
7180
|
7281
LL | / match Ok(1) as Result<i32, &str> {
7382
LL | | Ok(i) => i,
@@ -76,7 +85,7 @@ LL | | };
7685
| |_____^ help: replace with: `(Ok(1) as Result<i32, &str>).unwrap_or(42)`
7786

7887
error: this pattern reimplements `Result::unwrap_or`
79-
--> $DIR/manual_unwrap_or.rs:98:5
88+
--> $DIR/manual_unwrap_or.rs:105:5
8089
|
8190
LL | / match Ok::<i32, &str>(1) {
8291
LL | | Err(_) => 42,
@@ -85,7 +94,7 @@ LL | | };
8594
| |_____^ help: replace with: `Ok::<i32, &str>(1).unwrap_or(42)`
8695

8796
error: this pattern reimplements `Result::unwrap_or`
88-
--> $DIR/manual_unwrap_or.rs:104:5
97+
--> $DIR/manual_unwrap_or.rs:111:5
8998
|
9099
LL | / match Ok::<i32, &str>(1) {
91100
LL | | Ok(i) => i,
@@ -94,7 +103,7 @@ LL | | };
94103
| |_____^ help: replace with: `Ok::<i32, &str>(1).unwrap_or(1 + 42)`
95104

96105
error: this pattern reimplements `Result::unwrap_or`
97-
--> $DIR/manual_unwrap_or.rs:111:5
106+
--> $DIR/manual_unwrap_or.rs:118:5
98107
|
99108
LL | / match Ok::<i32, &str>(1) {
100109
LL | | Ok(i) => i,
@@ -115,13 +124,13 @@ LL | });
115124
|
116125

117126
error: this pattern reimplements `Result::unwrap_or`
118-
--> $DIR/manual_unwrap_or.rs:121:5
127+
--> $DIR/manual_unwrap_or.rs:128:5
119128
|
120129
LL | / match Ok::<&str, &str>("Bob") {
121130
LL | | Ok(i) => i,
122131
LL | | Err(_) => "Alice",
123132
LL | | };
124133
| |_____^ help: replace with: `Ok::<&str, &str>("Bob").unwrap_or("Alice")`
125134

126-
error: aborting due to 11 previous errors
135+
error: aborting due to 12 previous errors
127136

0 commit comments

Comments
 (0)