From d0738bd673fe8e2b42d26b6d116f197f4aecea82 Mon Sep 17 00:00:00 2001 From: Nick Torres Date: Sat, 4 Apr 2020 13:53:08 -0700 Subject: [PATCH] result_map_or_into_option: destructure lint tuple or return early --- clippy_lints/src/methods/mod.rs | 74 +++++++++++++----------- 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 7c818232a..62fcd801b 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 948eb6a3a..07daf19fa 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 = Ok(1); let _ = opt.ok(); - let rewrap = |s: u32| -> Option { - Some(s) - }; + let rewrap = |s: u32| -> Option { Some(s) }; // A non-Some `f` arg should not emit the lint let opt: Result = Ok(1);