diff options
| author | Nick Torres <nickrtorres@icloud.com> | 2020-04-04 13:53:08 -0700 |
|---|---|---|
| committer | Nick Torres <nickrtorres@icloud.com> | 2020-04-04 14:16:23 -0700 |
| commit | d0738bd673fe8e2b42d26b6d116f197f4aecea82 (patch) | |
| tree | 14c71eee31abfccc29fee4dc06fab4662a300c38 | |
| parent | 3a29aedf8db3af19ee0f64dee6f00489812e6cb0 (diff) | |
| download | rust-d0738bd673fe8e2b42d26b6d116f197f4aecea82.tar.gz rust-d0738bd673fe8e2b42d26b6d116f197f4aecea82.zip | |
result_map_or_into_option: destructure lint tuple or return early
| -rw-r--r-- | clippy_lints/src/methods/mod.rs | 74 | ||||
| -rw-r--r-- | tests/ui/result_map_or_into_option.fixed | 4 |
2 files changed, 41 insertions, 37 deletions
diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index 7c818232a55..62fcd801bc3 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -2573,42 +2573,48 @@ fn lint_map_or_none<'a, 'tcx>( false }; - let mess = if is_option && default_arg_is_none { - let self_snippet = snippet(cx, map_or_args[0].span, ".."); - let func_snippet = snippet(cx, map_or_args[2].span, ".."); - let msg = "called `map_or(None, f)` on an `Option` value. This can be done more directly by calling \ - `and_then(f)` instead"; - Some(( - OPTION_MAP_OR_NONE, - msg, - "try using `and_then` instead", - format!("{0}.and_then({1})", self_snippet, func_snippet), - )) - } else if is_result && f_arg_is_some { - let msg = "called `map_or(None, Some)` on a `Result` value. This can be done more directly by calling \ - `ok()` instead"; - let self_snippet = snippet(cx, map_or_args[0].span, ".."); - Some(( - RESULT_MAP_OR_INTO_OPTION, - msg, - "try using `ok` instead", - format!("{0}.ok()", self_snippet), - )) - } else { - None + let (lint, msg, instead, hint) = { + if !default_arg_is_none { + // nothing to lint! + return; + } + + if is_option { + let self_snippet = snippet(cx, map_or_args[0].span, ".."); + let func_snippet = snippet(cx, map_or_args[2].span, ".."); + let msg = "called `map_or(None, f)` on an `Option` value. This can be done more directly by calling \ + `and_then(f)` instead"; + ( + OPTION_MAP_OR_NONE, + msg, + "try using `and_then` instead", + format!("{0}.and_then({1})", self_snippet, func_snippet), + ) + } else if f_arg_is_some { + let msg = "called `map_or(None, Some)` on a `Result` value. This can be done more directly by calling \ + `ok()` instead"; + let self_snippet = snippet(cx, map_or_args[0].span, ".."); + ( + RESULT_MAP_OR_INTO_OPTION, + msg, + "try using `ok` instead", + format!("{0}.ok()", self_snippet), + ) + } else { + // nothing to lint! + return; + } }; - if let Some((lint, msg, instead, hint)) = mess { - span_lint_and_sugg( - cx, - lint, - expr.span, - msg, - instead, - hint, - Applicability::MachineApplicable, - ); - } + span_lint_and_sugg( + cx, + lint, + expr.span, + msg, + instead, + hint, + Applicability::MachineApplicable, + ); } /// Lint use of `_.and_then(|x| Some(y))` for `Option`s diff --git a/tests/ui/result_map_or_into_option.fixed b/tests/ui/result_map_or_into_option.fixed index 948eb6a3aca..07daf19fa25 100644 --- a/tests/ui/result_map_or_into_option.fixed +++ b/tests/ui/result_map_or_into_option.fixed @@ -6,9 +6,7 @@ fn main() { let opt: Result<u32, &str> = Ok(1); let _ = opt.ok(); - let rewrap = |s: u32| -> Option<u32> { - Some(s) - }; + let rewrap = |s: u32| -> Option<u32> { Some(s) }; // A non-Some `f` arg should not emit the lint let opt: Result<u32, &str> = Ok(1); |
