Skip to content

Commit d0e0ffa

Browse files
committed
make use of Result::map_or
1 parent 7b8e829 commit d0e0ffa

File tree

6 files changed

+16
-16
lines changed

6 files changed

+16
-16
lines changed

clippy_lints/src/escape.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ impl<'a, 'tcx> EscapeDelegate<'a, 'tcx> {
152152
fn is_large_box(&self, ty: Ty<'tcx>) -> bool {
153153
// Large types need to be boxed to avoid stack overflows.
154154
if ty.is_box() {
155-
self.cx.layout_of(ty.boxed_ty()).ok().map_or(0, |l| l.size.bytes()) > self.too_large_for_stack
155+
self.cx.layout_of(ty.boxed_ty()).map_or(0, |l| l.size.bytes()) > self.too_large_for_stack
156156
} else {
157157
false
158158
}

clippy_lints/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#![cfg_attr(feature = "deny-warnings", deny(warnings))]
1313
#![feature(crate_visibility_modifier)]
1414
#![feature(concat_idents)]
15+
#![feature(result_map_or)]
1516

1617
// FIXME: switch to something more ergonomic here, once available.
1718
// (Currently there is no way to opt into sysroot crates without `extern crate`.)

clippy_lints/src/methods/mod.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ declare_clippy_lint! {
291291
/// **What it does:** Checks for usage of `result.map(_).unwrap_or_else(_)`.
292292
///
293293
/// **Why is this bad?** Readability, this can be written more concisely as
294-
/// `result.ok().map_or_else(_, _)`.
294+
/// `result.map_or_else(_, _)`.
295295
///
296296
/// **Known problems:** None.
297297
///
@@ -303,7 +303,7 @@ declare_clippy_lint! {
303303
/// ```
304304
pub RESULT_MAP_UNWRAP_OR_ELSE,
305305
pedantic,
306-
"using `Result.map(f).unwrap_or_else(g)`, which is more succinctly expressed as `.ok().map_or_else(g, f)`"
306+
"using `Result.map(f).unwrap_or_else(g)`, which is more succinctly expressed as `.map_or_else(g, f)`"
307307
}
308308

309309
declare_clippy_lint! {
@@ -2217,7 +2217,7 @@ fn lint_map_unwrap_or_else<'a, 'tcx>(
22172217
`map_or_else(g, f)` instead"
22182218
} else {
22192219
"called `map(f).unwrap_or_else(g)` on a Result value. This can be done more directly by calling \
2220-
`ok().map_or_else(g, f)` instead"
2220+
`.map_or_else(g, f)` instead"
22212221
};
22222222
// get snippets for args to map() and unwrap_or_else()
22232223
let map_snippet = snippet(cx, map_args[1].span, "..");
@@ -2238,10 +2238,8 @@ fn lint_map_unwrap_or_else<'a, 'tcx>(
22382238
msg,
22392239
expr.span,
22402240
&format!(
2241-
"replace `map({0}).unwrap_or_else({1})` with `{2}map_or_else({1}, {0})`",
2242-
map_snippet,
2243-
unwrap_snippet,
2244-
if is_result { "ok()." } else { "" }
2241+
"replace `map({0}).unwrap_or_else({1})` with `map_or_else({1}, {0})`",
2242+
map_snippet, unwrap_snippet,
22452243
),
22462244
);
22472245
} else if same_span && multiline {

src/driver.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![cfg_attr(feature = "deny-warnings", deny(warnings))]
2+
#![feature(result_map_or)]
23
#![feature(rustc_private)]
34

45
// FIXME: switch to something more ergonomic here, once available.
@@ -319,7 +320,7 @@ pub fn main() {
319320
// this check ensures that dependencies are built but not linted and the final
320321
// crate is
321322
// linted but not built
322-
let clippy_enabled = env::var("CLIPPY_TESTS").ok().map_or(false, |val| val == "true")
323+
let clippy_enabled = env::var("CLIPPY_TESTS").map_or(false, |val| val == "true")
323324
|| arg_value(&orig_args, "--emit", |val| val.split(',').any(|e| e == "metadata")).is_some();
324325

325326
if clippy_enabled {

src/lintlist/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1697,7 +1697,7 @@ pub const ALL_LINTS: [Lint; 337] = [
16971697
Lint {
16981698
name: "result_map_unwrap_or_else",
16991699
group: "pedantic",
1700-
desc: "using `Result.map(f).unwrap_or_else(g)`, which is more succinctly expressed as `.ok().map_or_else(g, f)`",
1700+
desc: "using `Result.map(f).unwrap_or_else(g)`, which is more succinctly expressed as `.map_or_else(g, f)`",
17011701
deprecation: None,
17021702
module: "methods",
17031703
},
+6-6
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)