Skip to content

Commit 26b1f4f

Browse files
Add new ui tests for map_clone lint on types implementing Copy
1 parent 50b2731 commit 26b1f4f

File tree

3 files changed

+77
-7
lines changed

3 files changed

+77
-7
lines changed

tests/ui/map_clone.fixed

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,38 @@ 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
7485

7586
// Testing with `Result` now.
7687
let x: Result<String, ()> = Ok(String::new());
7788
let x = x.as_ref(); // We do this to prevent triggering the `useless_asref` lint.
7889
let y = x.cloned();
7990
//~^ ERROR: you are explicitly cloning with `.map()`
91+
//~| HELP: consider calling the dedicated `cloned` method
8092
let y = x.cloned();
93+
//~^ ERROR: you are explicitly cloning with `.map()`
94+
//~| HELP: consider calling the dedicated `cloned` method
95+
96+
let x: Result<u32, ()> = Ok(0);
97+
let x = x.as_ref(); // We do this to prevent triggering the `useless_asref` lint.
98+
let y = x.copied();
99+
//~^ ERROR: you are explicitly cloning with `.map()`
100+
//~| HELP: consider calling the dedicated `copied` method
101+
let y = x.copied();
102+
//~^ ERROR: you are explicitly cloning with `.map()`
103+
//~| HELP: consider calling the dedicated `copied` method
81104

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

tests/ui/map_clone.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,38 @@ 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
7485

7586
// Testing with `Result` now.
7687
let x: Result<String, ()> = Ok(String::new());
7788
let x = x.as_ref(); // We do this to prevent triggering the `useless_asref` lint.
7889
let y = x.map(|x| String::clone(x));
7990
//~^ ERROR: you are explicitly cloning with `.map()`
80-
let y = x.map(|x| String::clone(x));
91+
//~| HELP: consider calling the dedicated `cloned` method
92+
let y = x.map(|x| Clone::clone(x));
93+
//~^ ERROR: you are explicitly cloning with `.map()`
94+
//~| HELP: consider calling the dedicated `cloned` method
95+
96+
let x: Result<u32, ()> = Ok(0);
97+
let x = x.as_ref(); // We do this to prevent triggering the `useless_asref` lint.
98+
let y = x.map(|x| u32::clone(x));
99+
//~^ ERROR: you are explicitly cloning with `.map()`
100+
//~| HELP: consider calling the dedicated `copied` method
101+
let y = x.map(|x| Clone::clone(x));
102+
//~^ ERROR: you are explicitly cloning with `.map()`
103+
//~| HELP: consider calling the dedicated `copied` method
81104

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

tests/ui/map_clone.stderr

Lines changed: 30 additions & 6 deletions
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:89: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:92: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:98: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:101: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)