about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2024-01-09 16:35:10 +0100
committerGuillaume Gomez <guillaume1.gomez@gmail.com>2024-01-09 16:35:40 +0100
commit8791a28c4ab4d6b5d4f20c6785e8d65ff02c30cc (patch)
tree2b0a921f5f03fc6cf9e72d20a3a456974bf070de
parentcdd96bc6626c342528b6f6578de176fd6c09f381 (diff)
downloadrust-8791a28c4ab4d6b5d4f20c6785e8d65ff02c30cc.tar.gz
rust-8791a28c4ab4d6b5d4f20c6785e8d65ff02c30cc.zip
Also handle `Result` type for `map_clone` lint
-rw-r--r--clippy_lints/src/methods/map_clone.rs1
-rw-r--r--tests/ui/map_clone.fixed6
-rw-r--r--tests/ui/map_clone.rs6
-rw-r--r--tests/ui/map_clone.stderr8
4 files changed, 20 insertions, 1 deletions
diff --git a/clippy_lints/src/methods/map_clone.rs b/clippy_lints/src/methods/map_clone.rs
index 652d2b80081..33b77cdc33d 100644
--- a/clippy_lints/src/methods/map_clone.rs
+++ b/clippy_lints/src/methods/map_clone.rs
@@ -18,6 +18,7 @@ pub(super) fn check(cx: &LateContext<'_>, e: &hir::Expr<'_>, recv: &hir::Expr<'_
     if let Some(method_id) = cx.typeck_results().type_dependent_def_id(e.hir_id)
         && (cx.tcx.impl_of_method(method_id).map_or(false, |id| {
             is_type_diagnostic_item(cx, cx.tcx.type_of(id).instantiate_identity(), sym::Option)
+                || is_type_diagnostic_item(cx, cx.tcx.type_of(id).instantiate_identity(), sym::Result)
         }) || is_diag_trait_item(cx, method_id, sym::Iterator))
     {
         match arg.kind {
diff --git a/tests/ui/map_clone.fixed b/tests/ui/map_clone.fixed
index 74e4ea5ce01..144c42a1cb6 100644
--- a/tests/ui/map_clone.fixed
+++ b/tests/ui/map_clone.fixed
@@ -70,4 +70,10 @@ fn main() {
     //~^ ERROR: you are explicitly cloning with `.map()`
     let y = x.cloned();
     //~^ ERROR: you are explicitly cloning with `.map()`
+
+    // Testing with `Result` now.
+    let x: Result<String, ()> = Ok(String::new());
+    let x = x.as_ref(); // We do this to prevent triggering the `useless_asref` lint.
+    let y = x.cloned();
+    //~^ ERROR: you are explicitly cloning with `.map()`
 }
diff --git a/tests/ui/map_clone.rs b/tests/ui/map_clone.rs
index 0aac8024f67..e264d8ffc53 100644
--- a/tests/ui/map_clone.rs
+++ b/tests/ui/map_clone.rs
@@ -70,4 +70,10 @@ fn main() {
     //~^ ERROR: you are explicitly cloning with `.map()`
     let y = x.map(String::clone);
     //~^ ERROR: you are explicitly cloning with `.map()`
+
+    // Testing with `Result` now.
+    let x: Result<String, ()> = Ok(String::new());
+    let x = x.as_ref(); // We do this to prevent triggering the `useless_asref` lint.
+    let y = x.map(|x| String::clone(x));
+    //~^ ERROR: you are explicitly cloning with `.map()`
 }
diff --git a/tests/ui/map_clone.stderr b/tests/ui/map_clone.stderr
index aae4dae634a..7c6dc08f604 100644
--- a/tests/ui/map_clone.stderr
+++ b/tests/ui/map_clone.stderr
@@ -55,5 +55,11 @@ error: you are explicitly cloning with `.map()`
 LL |     let y = x.map(String::clone);
    |             ^^^^^^^^^^^^^^^^^^^^ help: consider calling the dedicated `cloned` method: `x.cloned()`
 
-error: aborting due to 9 previous errors
+error: you are explicitly cloning with `.map()`
+  --> $DIR/map_clone.rs:77:13
+   |
+LL |     let y = x.map(|x| String::clone(x));
+   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling the dedicated `cloned` method: `x.cloned()`
+
+error: aborting due to 10 previous errors