Skip to content

Commit 74db4b7

Browse files
Add new ui tests for map_clone lint on types implementing Copy
1 parent 82841aa commit 74db4b7

File tree

3 files changed

+97
-7
lines changed

3 files changed

+97
-7
lines changed

tests/ui/map_clone.fixed

+33
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,48 @@ fn main() {
6969
//~^ ERROR: you are explicitly cloning with `.map()`
7070
let y = x.cloned();
7171
//~^ ERROR: you are explicitly cloning with `.map()`
72+
//~| HELP: consider calling the dedicated `cloned` method
7273
let y = x.cloned();
7374
//~^ ERROR: you are explicitly cloning with `.map()`
75+
//~| HELP: consider calling the dedicated `cloned` method
76+
77+
let x: Option<u32> = Some(0);
78+
let x = x.as_ref(); // We do this to prevent triggering the `useless_asref` lint.
79+
let y = x.copied();
80+
//~^ ERROR: you are explicitly cloning with `.map()`
81+
//~| HELP: consider calling the dedicated `copied` method
82+
let y = x.copied();
83+
//~^ ERROR: you are explicitly cloning with `.map()`
84+
//~| HELP: consider calling the dedicated `copied` method
85+
86+
// Should not suggest `copied` or `cloned` here since `T` is not a reference.
87+
let x: Option<u32> = Some(0);
88+
let y = x.map(|x| u32::clone(&x));
89+
let y = x.map(|x| Clone::clone(&x));
7490

7591
// Testing with `Result` now.
7692
let x: Result<String, ()> = Ok(String::new());
7793
let x = x.as_ref(); // We do this to prevent triggering the `useless_asref` lint.
7894
let y = x.cloned();
7995
//~^ ERROR: you are explicitly cloning with `.map()`
96+
//~| HELP: consider calling the dedicated `cloned` method
8097
let y = x.cloned();
98+
//~^ ERROR: you are explicitly cloning with `.map()`
99+
//~| HELP: consider calling the dedicated `cloned` method
100+
101+
let x: Result<u32, ()> = Ok(0);
102+
let x = x.as_ref(); // We do this to prevent triggering the `useless_asref` lint.
103+
let y = x.copied();
104+
//~^ ERROR: you are explicitly cloning with `.map()`
105+
//~| HELP: consider calling the dedicated `copied` method
106+
let y = x.copied();
107+
//~^ ERROR: you are explicitly cloning with `.map()`
108+
//~| HELP: consider calling the dedicated `copied` method
109+
110+
// Should not suggest `copied` or `cloned` here since `T` is not a reference.
111+
let x: Result<u32, ()> = Ok(0);
112+
let y = x.map(|x| u32::clone(&x));
113+
let y = x.map(|x| Clone::clone(&x));
81114

82115
// We ensure that no warning is emitted here because `useless_asref` is taking over.
83116
let x = Some(String::new());

tests/ui/map_clone.rs

+34-1
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,48 @@ fn main() {
6969
//~^ ERROR: you are explicitly cloning with `.map()`
7070
let y = x.map(Clone::clone);
7171
//~^ ERROR: you are explicitly cloning with `.map()`
72+
//~| HELP: consider calling the dedicated `cloned` method
7273
let y = x.map(String::clone);
7374
//~^ ERROR: you are explicitly cloning with `.map()`
75+
//~| HELP: consider calling the dedicated `cloned` method
76+
77+
let x: Option<u32> = Some(0);
78+
let x = x.as_ref(); // We do this to prevent triggering the `useless_asref` lint.
79+
let y = x.map(|x| u32::clone(x));
80+
//~^ ERROR: you are explicitly cloning with `.map()`
81+
//~| HELP: consider calling the dedicated `copied` method
82+
let y = x.map(|x| Clone::clone(x));
83+
//~^ ERROR: you are explicitly cloning with `.map()`
84+
//~| HELP: consider calling the dedicated `copied` method
85+
86+
// Should not suggest `copied` or `cloned` here since `T` is not a reference.
87+
let x: Option<u32> = Some(0);
88+
let y = x.map(|x| u32::clone(&x));
89+
let y = x.map(|x| Clone::clone(&x));
7490

7591
// Testing with `Result` now.
7692
let x: Result<String, ()> = Ok(String::new());
7793
let x = x.as_ref(); // We do this to prevent triggering the `useless_asref` lint.
7894
let y = x.map(|x| String::clone(x));
7995
//~^ ERROR: you are explicitly cloning with `.map()`
80-
let y = x.map(|x| String::clone(x));
96+
//~| HELP: consider calling the dedicated `cloned` method
97+
let y = x.map(|x| Clone::clone(x));
98+
//~^ ERROR: you are explicitly cloning with `.map()`
99+
//~| HELP: consider calling the dedicated `cloned` method
100+
101+
let x: Result<u32, ()> = Ok(0);
102+
let x = x.as_ref(); // We do this to prevent triggering the `useless_asref` lint.
103+
let y = x.map(|x| u32::clone(x));
104+
//~^ ERROR: you are explicitly cloning with `.map()`
105+
//~| HELP: consider calling the dedicated `copied` method
106+
let y = x.map(|x| Clone::clone(x));
107+
//~^ ERROR: you are explicitly cloning with `.map()`
108+
//~| HELP: consider calling the dedicated `copied` method
109+
110+
// Should not suggest `copied` or `cloned` here since `T` is not a reference.
111+
let x: Result<u32, ()> = Ok(0);
112+
let y = x.map(|x| u32::clone(&x));
113+
let y = x.map(|x| Clone::clone(&x));
81114

82115
// We ensure that no warning is emitted here because `useless_asref` is taking over.
83116
let x = Some(String::new());

tests/ui/map_clone.stderr

+30-6
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,46 @@ LL | let y = x.map(Clone::clone);
5050
| ^^^^^^^^^^^^^^^^^^^ help: consider calling the dedicated `cloned` method: `x.cloned()`
5151

5252
error: you are explicitly cloning with `.map()`
53-
--> $DIR/map_clone.rs:72:13
53+
--> $DIR/map_clone.rs:73:13
5454
|
5555
LL | let y = x.map(String::clone);
5656
| ^^^^^^^^^^^^^^^^^^^^ help: consider calling the dedicated `cloned` method: `x.cloned()`
5757

5858
error: you are explicitly cloning with `.map()`
59-
--> $DIR/map_clone.rs:78:13
59+
--> $DIR/map_clone.rs:79:13
6060
|
61-
LL | let y = x.map(|x| String::clone(x));
62-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling the dedicated `cloned` method: `x.cloned()`
61+
LL | let y = x.map(|x| u32::clone(x));
62+
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling the dedicated `copied` method: `x.copied()`
6363

6464
error: you are explicitly cloning with `.map()`
65-
--> $DIR/map_clone.rs:80:13
65+
--> $DIR/map_clone.rs:82:13
66+
|
67+
LL | let y = x.map(|x| Clone::clone(x));
68+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling the dedicated `copied` method: `x.copied()`
69+
70+
error: you are explicitly cloning with `.map()`
71+
--> $DIR/map_clone.rs:94:13
6672
|
6773
LL | let y = x.map(|x| String::clone(x));
6874
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling the dedicated `cloned` method: `x.cloned()`
6975

70-
error: aborting due to 11 previous errors
76+
error: you are explicitly cloning with `.map()`
77+
--> $DIR/map_clone.rs:97:13
78+
|
79+
LL | let y = x.map(|x| Clone::clone(x));
80+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling the dedicated `cloned` method: `x.cloned()`
81+
82+
error: you are explicitly cloning with `.map()`
83+
--> $DIR/map_clone.rs:103:13
84+
|
85+
LL | let y = x.map(|x| u32::clone(x));
86+
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling the dedicated `copied` method: `x.copied()`
87+
88+
error: you are explicitly cloning with `.map()`
89+
--> $DIR/map_clone.rs:106:13
90+
|
91+
LL | let y = x.map(|x| Clone::clone(x));
92+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling the dedicated `copied` method: `x.copied()`
93+
94+
error: aborting due to 15 previous errors
7195

0 commit comments

Comments
 (0)