Skip to content

Commit 103e888

Browse files
Add tests to ensure that map_clone is not emitted if as_ref().clone() is present
1 parent 83ae5ed commit 103e888

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

tests/ui/map_clone.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
clippy::many_single_char_names,
66
clippy::redundant_clone,
77
clippy::redundant_closure,
8+
clippy::useless_asref,
89
clippy::useless_vec
910
)]
1011

@@ -76,4 +77,11 @@ fn main() {
7677
let x = x.as_ref(); // We do this to prevent triggering the `useless_asref` lint.
7778
let y = x.cloned();
7879
//~^ ERROR: you are explicitly cloning with `.map()`
80+
let y = x.cloned();
81+
82+
// We ensure that no warning is emitted here because `useless_asref` is taking over.
83+
let x = Some(String::new());
84+
let y = x.as_ref().map(|x| String::clone(x));
85+
let x: Result<String, ()> = Ok(String::new());
86+
let y = x.as_ref().map(|x| String::clone(x));
7987
}

tests/ui/map_clone.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
clippy::many_single_char_names,
66
clippy::redundant_clone,
77
clippy::redundant_closure,
8+
clippy::useless_asref,
89
clippy::useless_vec
910
)]
1011

@@ -76,4 +77,11 @@ fn main() {
7677
let x = x.as_ref(); // We do this to prevent triggering the `useless_asref` lint.
7778
let y = x.map(|x| String::clone(x));
7879
//~^ ERROR: you are explicitly cloning with `.map()`
80+
let y = x.map(|x| String::clone(x));
81+
82+
// We ensure that no warning is emitted here because `useless_asref` is taking over.
83+
let x = Some(String::new());
84+
let y = x.as_ref().map(|x| String::clone(x));
85+
let x: Result<String, ()> = Ok(String::new());
86+
let y = x.as_ref().map(|x| String::clone(x));
7987
}

tests/ui/map_clone.stderr

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: you are using an explicit closure for copying elements
2-
--> $DIR/map_clone.rs:12:22
2+
--> $DIR/map_clone.rs:13:22
33
|
44
LL | let _: Vec<i8> = vec![5_i8; 6].iter().map(|x| *x).collect();
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling the dedicated `copied` method: `vec![5_i8; 6].iter().copied()`
@@ -8,58 +8,64 @@ LL | let _: Vec<i8> = vec![5_i8; 6].iter().map(|x| *x).collect();
88
= help: to override `-D warnings` add `#[allow(clippy::map_clone)]`
99

1010
error: you are using an explicit closure for cloning elements
11-
--> $DIR/map_clone.rs:13:26
11+
--> $DIR/map_clone.rs:14:26
1212
|
1313
LL | let _: Vec<String> = vec![String::new()].iter().map(|x| x.clone()).collect();
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling the dedicated `cloned` method: `vec![String::new()].iter().cloned()`
1515

1616
error: you are using an explicit closure for copying elements
17-
--> $DIR/map_clone.rs:14:23
17+
--> $DIR/map_clone.rs:15:23
1818
|
1919
LL | let _: Vec<u32> = vec![42, 43].iter().map(|&x| x).collect();
2020
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling the dedicated `copied` method: `vec![42, 43].iter().copied()`
2121

2222
error: you are using an explicit closure for copying elements
23-
--> $DIR/map_clone.rs:16:26
23+
--> $DIR/map_clone.rs:17:26
2424
|
2525
LL | let _: Option<u64> = Some(&16).map(|b| *b);
2626
| ^^^^^^^^^^^^^^^^^^^^^ help: consider calling the dedicated `copied` method: `Some(&16).copied()`
2727

2828
error: you are using an explicit closure for copying elements
29-
--> $DIR/map_clone.rs:17:25
29+
--> $DIR/map_clone.rs:18:25
3030
|
3131
LL | let _: Option<u8> = Some(&1).map(|x| x.clone());
3232
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling the dedicated `copied` method: `Some(&1).copied()`
3333

3434
error: you are needlessly cloning iterator elements
35-
--> $DIR/map_clone.rs:28:29
35+
--> $DIR/map_clone.rs:29:29
3636
|
3737
LL | let _ = std::env::args().map(|v| v.clone());
3838
| ^^^^^^^^^^^^^^^^^^^ help: remove the `map` call
3939

4040
error: you are explicitly cloning with `.map()`
41-
--> $DIR/map_clone.rs:67:13
41+
--> $DIR/map_clone.rs:68:13
4242
|
4343
LL | let y = x.map(|x| String::clone(x));
4444
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling the dedicated `cloned` method: `x.cloned()`
4545

4646
error: you are explicitly cloning with `.map()`
47-
--> $DIR/map_clone.rs:69:13
47+
--> $DIR/map_clone.rs:70:13
4848
|
4949
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:71:13
53+
--> $DIR/map_clone.rs:72: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:77:13
59+
--> $DIR/map_clone.rs:78:13
6060
|
6161
LL | let y = x.map(|x| String::clone(x));
6262
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling the dedicated `cloned` method: `x.cloned()`
6363

64-
error: aborting due to 10 previous errors
64+
error: you are explicitly cloning with `.map()`
65+
--> $DIR/map_clone.rs:80:13
66+
|
67+
LL | let y = x.map(|x| String::clone(x));
68+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling the dedicated `cloned` method: `x.cloned()`
69+
70+
error: aborting due to 11 previous errors
6571

0 commit comments

Comments
 (0)