diff --git a/clippy_lints/src/manual_map.rs b/clippy_lints/src/manual_map.rs index e399c8fda55e..04ca35f9ca34 100644 --- a/clippy_lints/src/manual_map.rs +++ b/clippy_lints/src/manual_map.rs @@ -1,10 +1,11 @@ use crate::utils::{ is_type_diagnostic_item, match_def_path, paths, peel_hir_expr_refs, peel_mid_ty_refs_is_mutable, - snippet_with_applicability, span_lint_and_sugg, + snippet_with_applicability, span_lint_and_sugg, match_var, is_allowed, }; +use crate::{matches::MATCH_AS_REF, map_unit_fn::OPTION_MAP_UNIT_FN}; use rustc_ast::util::parser::PREC_POSTFIX; use rustc_errors::Applicability; -use rustc_hir::{Arm, BindingAnnotation, Block, Expr, ExprKind, Mutability, Pat, PatKind, Path, QPath}; +use rustc_hir::{Arm, BindingAnnotation, Block, Expr, ExprKind, Mutability, Pat, PatKind, QPath}; use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_middle::lint::in_external_macro; use rustc_session::{declare_lint_pass, declare_tool_lint}; @@ -88,6 +89,11 @@ impl LateLintPass<'_> for ManualMap { None => return, }; + if cx.typeck_results().expr_ty(some_expr) == cx.tcx.types.unit + && !is_allowed(cx, OPTION_MAP_UNIT_FN, expr.hir_id) { + return; + } + // Determine which binding mode to use. let explicit_ref = some_pat.contains_explicit_ref_binding(); let binding_mutability = explicit_ref.or(if ty_ref_count != pat_ref_count { @@ -118,6 +124,10 @@ impl LateLintPass<'_> for ManualMap { if let Some(func) = can_pass_as_func(cx, some_binding, some_expr) { snippet_with_applicability(cx, func.span, "..", &mut app).into_owned() } else { + if match_var(some_expr, some_binding.name) && !is_allowed(cx, MATCH_AS_REF, expr.hir_id) && binding_mutability.is_some() { + return; + } + // `ref` and `ref mut` annotations were handled earlier. let annotation = if matches!(annotation, BindingAnnotation::Mutable) { "mut " @@ -161,10 +171,7 @@ impl LateLintPass<'_> for ManualMap { fn can_pass_as_func(cx: &LateContext<'tcx>, binding: Ident, expr: &'tcx Expr<'_>) -> Option<&'tcx Expr<'tcx>> { match expr.kind { ExprKind::Call(func, [arg]) - if matches!(arg.kind, - ExprKind::Path(QPath::Resolved(None, Path { segments: [path], ..})) - if path.ident == binding - ) && cx.typeck_results().expr_adjustments(arg).is_empty() => + if match_var(arg, binding.name) && cx.typeck_results().expr_adjustments(arg).is_empty() => { Some(func) }, diff --git a/tests/ui/manual_map_option.fixed b/tests/ui/manual_map_option.fixed index 6cb9a37b2300..7afeba759eab 100644 --- a/tests/ui/manual_map_option.fixed +++ b/tests/ui/manual_map_option.fixed @@ -32,9 +32,16 @@ fn main() { Some(0).map(|x| x + x); + #[warn(clippy::option_map_unit_fn)] + match &mut Some(String::new()) { + Some(x) => Some(x.push_str("")), + None => None, + }; + + #[allow(clippy::option_map_unit_fn)] Some(String::new()).as_mut().map(|x| x.push_str("")); - Some(String::new()).as_ref().map(|x| &**x); + Some(String::new()).as_ref().map(|x| x.len()); Some(String::new()).as_ref().map(|x| x.is_empty()); diff --git a/tests/ui/manual_map_option.rs b/tests/ui/manual_map_option.rs index b9753060b993..f8b31121e5b2 100644 --- a/tests/ui/manual_map_option.rs +++ b/tests/ui/manual_map_option.rs @@ -66,13 +66,20 @@ fn main() { &&_ => None, }; + #[warn(clippy::option_map_unit_fn)] match &mut Some(String::new()) { Some(x) => Some(x.push_str("")), None => None, }; + #[allow(clippy::option_map_unit_fn)] match &mut Some(String::new()) { - Some(ref x) => Some(&**x), + Some(x) => Some(x.push_str("")), + None => None, + }; + + match &mut Some(String::new()) { + Some(ref x) => Some(x.len()), None => None, }; diff --git a/tests/ui/manual_map_option.stderr b/tests/ui/manual_map_option.stderr index f8e1bda83b25..08186b6ad9d7 100644 --- a/tests/ui/manual_map_option.stderr +++ b/tests/ui/manual_map_option.stderr @@ -101,7 +101,7 @@ LL | | }; | |_____^ help: try this: `Some(0).map(|x| x + x)` error: manual implementation of `Option::map` - --> $DIR/manual_map_option.rs:69:5 + --> $DIR/manual_map_option.rs:76:5 | LL | / match &mut Some(String::new()) { LL | | Some(x) => Some(x.push_str("")), @@ -110,16 +110,16 @@ LL | | }; | |_____^ help: try this: `Some(String::new()).as_mut().map(|x| x.push_str(""))` error: manual implementation of `Option::map` - --> $DIR/manual_map_option.rs:74:5 + --> $DIR/manual_map_option.rs:81:5 | LL | / match &mut Some(String::new()) { -LL | | Some(ref x) => Some(&**x), +LL | | Some(ref x) => Some(x.len()), LL | | None => None, LL | | }; - | |_____^ help: try this: `Some(String::new()).as_ref().map(|x| &**x)` + | |_____^ help: try this: `Some(String::new()).as_ref().map(|x| x.len())` error: manual implementation of `Option::map` - --> $DIR/manual_map_option.rs:79:5 + --> $DIR/manual_map_option.rs:86:5 | LL | / match &mut &Some(String::new()) { LL | | Some(x) => Some(x.is_empty()), @@ -128,7 +128,7 @@ LL | | }; | |_____^ help: try this: `Some(String::new()).as_ref().map(|x| x.is_empty())` error: manual implementation of `Option::map` - --> $DIR/manual_map_option.rs:84:5 + --> $DIR/manual_map_option.rs:91:5 | LL | / match Some((0, 1, 2)) { LL | | Some((x, y, z)) => Some(x + y + z), @@ -137,7 +137,7 @@ LL | | }; | |_____^ help: try this: `Some((0, 1, 2)).map(|(x, y, z)| x + y + z)` error: manual implementation of `Option::map` - --> $DIR/manual_map_option.rs:89:5 + --> $DIR/manual_map_option.rs:96:5 | LL | / match Some([1, 2, 3]) { LL | | Some([first, ..]) => Some(first), @@ -146,7 +146,7 @@ LL | | }; | |_____^ help: try this: `Some([1, 2, 3]).map(|[first, ..]| first)` error: manual implementation of `Option::map` - --> $DIR/manual_map_option.rs:94:5 + --> $DIR/manual_map_option.rs:101:5 | LL | / match &Some((String::new(), "test")) { LL | | Some((x, y)) => Some((y, x)),