Skip to content

Commit b546fd3

Browse files
committed
result_map_unwrap_or_else: drop the .ok() in the suggestion
Adding the `.ok()` changes the semantics since that assumes that `g` was ignoring its argument. Instead, allow `g` to keep taking its argument and preserve the isomorphism to the original code in the suggestion. Fixes #1590
1 parent 3710ec5 commit b546fd3

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ declare_clippy_lint! {
205205
/// **What it does:** Checks for usage of `result.map(_).unwrap_or_else(_)`.
206206
///
207207
/// **Why is this bad?** Readability, this can be written more concisely as
208-
/// `result.ok().map_or_else(_, _)`.
208+
/// `result.map_or_else(_, _)`.
209209
///
210210
/// **Known problems:** None.
211211
///
@@ -215,7 +215,7 @@ declare_clippy_lint! {
215215
/// ```
216216
pub RESULT_MAP_UNWRAP_OR_ELSE,
217217
pedantic,
218-
"using `Result.map(f).unwrap_or_else(g)`, which is more succinctly expressed as `.ok().map_or_else(g, f)`"
218+
"using `Result.map(f).unwrap_or_else(g)`, which is more succinctly expressed as `map_or_else(g, f)`"
219219
}
220220

221221
declare_clippy_lint! {
@@ -1848,7 +1848,7 @@ fn lint_map_unwrap_or_else<'a, 'tcx>(
18481848
`map_or_else(g, f)` instead"
18491849
} else {
18501850
"called `map(f).unwrap_or_else(g)` on a Result value. This can be done more directly by calling \
1851-
`ok().map_or_else(g, f)` instead"
1851+
`map_or_else(g, f)` instead"
18521852
};
18531853
// get snippets for args to map() and unwrap_or_else()
18541854
let map_snippet = snippet(cx, map_args[1].span, "..");
@@ -1869,10 +1869,8 @@ fn lint_map_unwrap_or_else<'a, 'tcx>(
18691869
msg,
18701870
expr.span,
18711871
&format!(
1872-
"replace `map({0}).unwrap_or_else({1})` with `{2}map_or_else({1}, {0})`",
1873-
map_snippet,
1874-
unwrap_snippet,
1875-
if is_result { "ok()." } else { "" }
1872+
"replace `map({0}).unwrap_or_else({1})` with `map_or_else({1}, {0})`",
1873+
map_snippet, unwrap_snippet,
18761874
),
18771875
);
18781876
} else if same_span && multiline {
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
error: called `map(f).unwrap_or_else(g)` on a Result value. This can be done more directly by calling `ok().map_or_else(g, f)` instead
1+
error: called `map(f).unwrap_or_else(g)` on a Result value. This can be done more directly by calling `map_or_else(g, f)` instead
22
--> $DIR/result_map_unwrap_or_else.rs:15:13
33
|
44
LL | let _ = res.map(|x| x + 1).unwrap_or_else(|e| 0); // should lint even though this call is on a separate line
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::result-map-unwrap-or-else` implied by `-D warnings`
8-
= note: replace `map(|x| x + 1).unwrap_or_else(|e| 0)` with `ok().map_or_else(|e| 0, |x| x + 1)`
8+
= note: replace `map(|x| x + 1).unwrap_or_else(|e| 0)` with `map_or_else(|e| 0, |x| x + 1)`
99

10-
error: called `map(f).unwrap_or_else(g)` on a Result value. This can be done more directly by calling `ok().map_or_else(g, f)` instead
10+
error: called `map(f).unwrap_or_else(g)` on a Result value. This can be done more directly by calling `map_or_else(g, f)` instead
1111
--> $DIR/result_map_unwrap_or_else.rs:17:13
1212
|
1313
LL | let _ = res.map(|x| x + 1).unwrap_or_else(|e| 0);
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1515
|
16-
= note: replace `map(|x| x + 1).unwrap_or_else(|e| 0)` with `ok().map_or_else(|e| 0, |x| x + 1)`
16+
= note: replace `map(|x| x + 1).unwrap_or_else(|e| 0)` with `map_or_else(|e| 0, |x| x + 1)`
1717

18-
error: called `map(f).unwrap_or_else(g)` on a Result value. This can be done more directly by calling `ok().map_or_else(g, f)` instead
18+
error: called `map(f).unwrap_or_else(g)` on a Result value. This can be done more directly by calling `map_or_else(g, f)` instead
1919
--> $DIR/result_map_unwrap_or_else.rs:18:13
2020
|
2121
LL | let _ = res.map(|x| x + 1).unwrap_or_else(|e| 0);
2222
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2323
|
24-
= note: replace `map(|x| x + 1).unwrap_or_else(|e| 0)` with `ok().map_or_else(|e| 0, |x| x + 1)`
24+
= note: replace `map(|x| x + 1).unwrap_or_else(|e| 0)` with `map_or_else(|e| 0, |x| x + 1)`
2525

2626
error: aborting due to 3 previous errors
2727

0 commit comments

Comments
 (0)