Skip to content

Commit 8791a28

Browse files
Also handle Result type for map_clone lint
1 parent cdd96bc commit 8791a28

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

clippy_lints/src/methods/map_clone.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub(super) fn check(cx: &LateContext<'_>, e: &hir::Expr<'_>, recv: &hir::Expr<'_
1818
if let Some(method_id) = cx.typeck_results().type_dependent_def_id(e.hir_id)
1919
&& (cx.tcx.impl_of_method(method_id).map_or(false, |id| {
2020
is_type_diagnostic_item(cx, cx.tcx.type_of(id).instantiate_identity(), sym::Option)
21+
|| is_type_diagnostic_item(cx, cx.tcx.type_of(id).instantiate_identity(), sym::Result)
2122
}) || is_diag_trait_item(cx, method_id, sym::Iterator))
2223
{
2324
match arg.kind {

tests/ui/map_clone.fixed

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,10 @@ fn main() {
7070
//~^ ERROR: you are explicitly cloning with `.map()`
7171
let y = x.cloned();
7272
//~^ ERROR: you are explicitly cloning with `.map()`
73+
74+
// Testing with `Result` now.
75+
let x: Result<String, ()> = Ok(String::new());
76+
let x = x.as_ref(); // We do this to prevent triggering the `useless_asref` lint.
77+
let y = x.cloned();
78+
//~^ ERROR: you are explicitly cloning with `.map()`
7379
}

tests/ui/map_clone.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,10 @@ fn main() {
7070
//~^ ERROR: you are explicitly cloning with `.map()`
7171
let y = x.map(String::clone);
7272
//~^ ERROR: you are explicitly cloning with `.map()`
73+
74+
// Testing with `Result` now.
75+
let x: Result<String, ()> = Ok(String::new());
76+
let x = x.as_ref(); // We do this to prevent triggering the `useless_asref` lint.
77+
let y = x.map(|x| String::clone(x));
78+
//~^ ERROR: you are explicitly cloning with `.map()`
7379
}

tests/ui/map_clone.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,11 @@ error: you are explicitly cloning with `.map()`
5555
LL | let y = x.map(String::clone);
5656
| ^^^^^^^^^^^^^^^^^^^^ help: consider calling the dedicated `cloned` method: `x.cloned()`
5757

58-
error: aborting due to 9 previous errors
58+
error: you are explicitly cloning with `.map()`
59+
--> $DIR/map_clone.rs:77:13
60+
|
61+
LL | let y = x.map(|x| String::clone(x));
62+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling the dedicated `cloned` method: `x.cloned()`
63+
64+
error: aborting due to 10 previous errors
5965

0 commit comments

Comments
 (0)