Skip to content

Commit 0d21ae0

Browse files
committed
manual-unwrap-or / pr remarks, round 3
1 parent 6533d8b commit 0d21ae0

File tree

4 files changed

+42
-17
lines changed

4 files changed

+42
-17
lines changed

clippy_lints/src/manual_unwrap_or.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::consts::constant_simple;
22
use crate::utils;
3+
use crate::utils::sugg;
34
use if_chain::if_chain;
45
use rustc_errors::Applicability;
56
use rustc_hir::{def, Arm, Expr, ExprKind, Pat, PatKind, QPath};
@@ -104,28 +105,20 @@ fn lint_manual_unwrap_or<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
104105
None
105106
};
106107
if let Some(or_arm) = applicable_or_arm(match_arms);
107-
if let Some(scrutinee_snippet) = utils::snippet_opt(cx, scrutinee.span);
108108
if let Some(or_body_snippet) = utils::snippet_opt(cx, or_arm.body.span);
109109
if let Some(indent) = utils::indent_of(cx, expr.span);
110110
if constant_simple(cx, cx.typeck_results(), or_arm.body).is_some();
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 {
115-
kind: ExprKind::Call(..) | ExprKind::Path(_), ..
116-
});
117-
let l_paren = if wrap_in_parens { "(" } else { "" };
118-
let r_paren = if wrap_in_parens { ")" } else { "" };
119114
utils::span_lint_and_sugg(
120115
cx,
121116
MANUAL_UNWRAP_OR, expr.span,
122117
&format!("this pattern reimplements `{}`", case.unwrap_fn_path()),
123118
"replace with",
124119
format!(
125-
"{}{}{}.unwrap_or({})",
126-
l_paren,
127-
scrutinee_snippet,
128-
r_paren,
120+
"{}.unwrap_or({})",
121+
sugg::Sugg::hir(cx, scrutinee, "..").maybe_par(),
129122
reindented_or_body,
130123
),
131124
Applicability::MachineApplicable,

tests/ui/manual_unwrap_or.fixed

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,19 @@ fn result_unwrap_or() {
7474
let a = Ok::<i32, &str>(1);
7575
a.unwrap_or(42);
7676

77-
// int case, suggestion must surround with parenthesis
77+
// int case, suggestion must surround Result expr with parenthesis
7878
(Ok(1) as Result<i32, &str>).unwrap_or(42);
7979

80+
// method call case, suggestion must not surround Result expr `s.method()` with parenthesis
81+
struct S {}
82+
impl S {
83+
fn method(self) -> Option<i32> {
84+
Some(42)
85+
}
86+
}
87+
let s = S {};
88+
s.method().unwrap_or(42);
89+
8090
// int case reversed
8191
Ok::<i32, &str>(1).unwrap_or(42);
8292

tests/ui/manual_unwrap_or.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,25 @@ fn result_unwrap_or() {
9595
Err(_) => 42,
9696
};
9797

98-
// int case, suggestion must surround with parenthesis
98+
// int case, suggestion must surround Result expr with parenthesis
9999
match Ok(1) as Result<i32, &str> {
100100
Ok(i) => i,
101101
Err(_) => 42,
102102
};
103103

104+
// method call case, suggestion must not surround Result expr `s.method()` with parenthesis
105+
struct S {}
106+
impl S {
107+
fn method(self) -> Option<i32> {
108+
Some(42)
109+
}
110+
}
111+
let s = S {};
112+
match s.method() {
113+
Some(i) => i,
114+
None => 42,
115+
};
116+
104117
// int case reversed
105118
match Ok::<i32, &str>(1) {
106119
Err(_) => 42,

tests/ui/manual_unwrap_or.stderr

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,17 @@ LL | | Err(_) => 42,
8484
LL | | };
8585
| |_____^ help: replace with: `(Ok(1) as Result<i32, &str>).unwrap_or(42)`
8686

87+
error: this pattern reimplements `Option::unwrap_or`
88+
--> $DIR/manual_unwrap_or.rs:112:5
89+
|
90+
LL | / match s.method() {
91+
LL | | Some(i) => i,
92+
LL | | None => 42,
93+
LL | | };
94+
| |_____^ help: replace with: `s.method().unwrap_or(42)`
95+
8796
error: this pattern reimplements `Result::unwrap_or`
88-
--> $DIR/manual_unwrap_or.rs:105:5
97+
--> $DIR/manual_unwrap_or.rs:118:5
8998
|
9099
LL | / match Ok::<i32, &str>(1) {
91100
LL | | Err(_) => 42,
@@ -94,7 +103,7 @@ LL | | };
94103
| |_____^ help: replace with: `Ok::<i32, &str>(1).unwrap_or(42)`
95104

96105
error: this pattern reimplements `Result::unwrap_or`
97-
--> $DIR/manual_unwrap_or.rs:111:5
106+
--> $DIR/manual_unwrap_or.rs:124:5
98107
|
99108
LL | / match Ok::<i32, &str>(1) {
100109
LL | | Ok(i) => i,
@@ -103,7 +112,7 @@ LL | | };
103112
| |_____^ help: replace with: `Ok::<i32, &str>(1).unwrap_or(1 + 42)`
104113

105114
error: this pattern reimplements `Result::unwrap_or`
106-
--> $DIR/manual_unwrap_or.rs:118:5
115+
--> $DIR/manual_unwrap_or.rs:131:5
107116
|
108117
LL | / match Ok::<i32, &str>(1) {
109118
LL | | Ok(i) => i,
@@ -124,13 +133,13 @@ LL | });
124133
|
125134

126135
error: this pattern reimplements `Result::unwrap_or`
127-
--> $DIR/manual_unwrap_or.rs:128:5
136+
--> $DIR/manual_unwrap_or.rs:141:5
128137
|
129138
LL | / match Ok::<&str, &str>("Bob") {
130139
LL | | Ok(i) => i,
131140
LL | | Err(_) => "Alice",
132141
LL | | };
133142
| |_____^ help: replace with: `Ok::<&str, &str>("Bob").unwrap_or("Alice")`
134143

135-
error: aborting due to 12 previous errors
144+
error: aborting due to 13 previous errors
136145

0 commit comments

Comments
 (0)