Skip to content

Commit 877a55d

Browse files
committed
Add run-rustfix
1 parent ef9f54c commit 877a55d

File tree

4 files changed

+240
-96
lines changed

4 files changed

+240
-96
lines changed

clippy_lints/src/redundant_clone.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,21 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantClone {
208208
let sugg_span = span.with_lo(
209209
span.lo() + BytePos(u32::try_from(dot).unwrap())
210210
);
211+
let mut app = Applicability::MaybeIncorrect;
212+
let mut call_snip = &snip[dot + 1..];
213+
if call_snip.ends_with("()") {
214+
call_snip = &call_snip[..call_snip.len()-2];
215+
if call_snip.as_bytes().iter().all(u8::is_ascii_alphabetic) {
216+
app = Applicability::MachineApplicable;
217+
}
218+
}
211219

212220
span_lint_hir_and_then(cx, REDUNDANT_CLONE, node, sugg_span, "redundant clone", |db| {
213221
db.span_suggestion(
214222
sugg_span,
215223
"remove this",
216224
String::new(),
217-
Applicability::MaybeIncorrect,
225+
app,
218226
);
219227
db.span_note(
220228
span.with_hi(span.lo() + BytePos(u32::try_from(dot).unwrap())),

tests/ui/redundant_clone.fixed

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
// run-rustfix
2+
3+
use std::ffi::OsString;
4+
use std::path::Path;
5+
6+
fn main() {
7+
let _s = ["lorem", "ipsum"].join(" ");
8+
9+
let s = String::from("foo");
10+
let _s = s;
11+
12+
let s = String::from("foo");
13+
let _s = s;
14+
15+
let s = String::from("foo");
16+
let _s = s;
17+
18+
let _s = Path::new("/a/b/").join("c");
19+
20+
let _s = Path::new("/a/b/").join("c");
21+
22+
let _s = OsString::new();
23+
24+
let _s = OsString::new();
25+
26+
// Check that lint level works
27+
#[allow(clippy::redundant_clone)]
28+
let _s = String::new().to_string();
29+
30+
let tup = (String::from("foo"),);
31+
let _t = tup.0;
32+
33+
let tup_ref = &(String::from("foo"),);
34+
let _s = tup_ref.0.clone(); // this `.clone()` cannot be removed
35+
36+
{
37+
let x = String::new();
38+
let y = &x;
39+
40+
let _x = x.clone(); // ok; `x` is borrowed by `y`
41+
42+
let _ = y.len();
43+
}
44+
45+
let x = (String::new(),);
46+
let _ = Some(String::new()).unwrap_or_else(|| x.0.clone()); // ok; closure borrows `x`
47+
48+
with_branch(Alpha, true);
49+
cannot_move_from_type_with_drop();
50+
borrower_propagation();
51+
}
52+
53+
#[derive(Clone)]
54+
struct Alpha;
55+
fn with_branch(a: Alpha, b: bool) -> (Alpha, Alpha) {
56+
if b {
57+
(a.clone(), a)
58+
} else {
59+
(Alpha, a)
60+
}
61+
}
62+
63+
struct TypeWithDrop {
64+
x: String,
65+
}
66+
67+
impl Drop for TypeWithDrop {
68+
fn drop(&mut self) {}
69+
}
70+
71+
fn cannot_move_from_type_with_drop() -> String {
72+
let s = TypeWithDrop { x: String::new() };
73+
s.x.clone() // removing this `clone()` summons E0509
74+
}
75+
76+
fn borrower_propagation() {
77+
let s = String::new();
78+
let t = String::new();
79+
80+
{
81+
fn b() -> bool {
82+
unimplemented!()
83+
}
84+
let _u = if b() { &s } else { &t };
85+
86+
// ok; `s` and `t` are possibly borrowed
87+
let _s = s.clone();
88+
let _t = t.clone();
89+
}
90+
91+
{
92+
let _u = || s.len();
93+
let _v = [&t; 32];
94+
let _s = s.clone(); // ok
95+
let _t = t.clone(); // ok
96+
}
97+
98+
{
99+
let _u = {
100+
let u = Some(&s);
101+
let _ = s.clone(); // ok
102+
u
103+
};
104+
let _s = s.clone(); // ok
105+
}
106+
107+
{
108+
use std::convert::identity as id;
109+
let _u = id(id(&s));
110+
let _s = s.clone(); // ok, `u` borrows `s`
111+
}
112+
113+
let _s = s;
114+
let _t = t;
115+
116+
#[derive(Clone)]
117+
struct Foo {
118+
x: usize,
119+
}
120+
121+
{
122+
let f = Foo { x: 123 };
123+
let _x = Some(f.x);
124+
let _f = f;
125+
}
126+
127+
{
128+
let f = Foo { x: 123 };
129+
let _x = &f.x;
130+
let _f = f.clone(); // ok
131+
}
132+
}

tests/ui/redundant_clone.rs

+29-25
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
1-
#![warn(clippy::redundant_clone)]
1+
// run-rustfix
22

33
use std::ffi::OsString;
44
use std::path::Path;
55

66
fn main() {
7-
let _ = ["lorem", "ipsum"].join(" ").to_string();
7+
let _s = ["lorem", "ipsum"].join(" ").to_string();
88

99
let s = String::from("foo");
10-
let _ = s.clone();
10+
let _s = s.clone();
1111

1212
let s = String::from("foo");
13-
let _ = s.to_string();
13+
let _s = s.to_string();
1414

1515
let s = String::from("foo");
16-
let _ = s.to_owned();
16+
let _s = s.to_owned();
1717

18-
let _ = Path::new("/a/b/").join("c").to_owned();
18+
let _s = Path::new("/a/b/").join("c").to_owned();
1919

20-
let _ = Path::new("/a/b/").join("c").to_path_buf();
20+
let _s = Path::new("/a/b/").join("c").to_path_buf();
2121

22-
let _ = OsString::new().to_owned();
22+
let _s = OsString::new().to_owned();
2323

24-
let _ = OsString::new().to_os_string();
24+
let _s = OsString::new().to_os_string();
2525

2626
// Check that lint level works
2727
#[allow(clippy::redundant_clone)]
28-
let _ = String::new().to_string();
28+
let _s = String::new().to_string();
2929

3030
let tup = (String::from("foo"),);
31-
let _ = tup.0.clone();
31+
let _t = tup.0.clone();
3232

3333
let tup_ref = &(String::from("foo"),);
3434
let _s = tup_ref.0.clone(); // this `.clone()` cannot be removed
@@ -37,13 +37,17 @@ fn main() {
3737
let x = String::new();
3838
let y = &x;
3939

40-
let _ = x.clone(); // ok; `x` is borrowed by `y`
40+
let _x = x.clone(); // ok; `x` is borrowed by `y`
4141

4242
let _ = y.len();
4343
}
4444

4545
let x = (String::new(),);
4646
let _ = Some(String::new()).unwrap_or_else(|| x.0.clone()); // ok; closure borrows `x`
47+
48+
with_branch(Alpha, true);
49+
cannot_move_from_type_with_drop();
50+
borrower_propagation();
4751
}
4852

4953
#[derive(Clone)]
@@ -77,37 +81,37 @@ fn borrower_propagation() {
7781
fn b() -> bool {
7882
unimplemented!()
7983
}
80-
let u = if b() { &s } else { &t };
84+
let _u = if b() { &s } else { &t };
8185

8286
// ok; `s` and `t` are possibly borrowed
83-
let _ = s.clone();
84-
let _ = t.clone();
87+
let _s = s.clone();
88+
let _t = t.clone();
8589
}
8690

8791
{
88-
let u = || s.len();
89-
let v = [&t; 32];
90-
let _ = s.clone(); // ok
91-
let _ = t.clone(); // ok
92+
let _u = || s.len();
93+
let _v = [&t; 32];
94+
let _s = s.clone(); // ok
95+
let _t = t.clone(); // ok
9296
}
9397

9498
{
95-
let u = {
99+
let _u = {
96100
let u = Some(&s);
97101
let _ = s.clone(); // ok
98102
u
99103
};
100-
let _ = s.clone(); // ok
104+
let _s = s.clone(); // ok
101105
}
102106

103107
{
104108
use std::convert::identity as id;
105-
let u = id(id(&s));
106-
let _ = s.clone(); // ok, `u` borrows `s`
109+
let _u = id(id(&s));
110+
let _s = s.clone(); // ok, `u` borrows `s`
107111
}
108112

109-
let _ = s.clone();
110-
let _ = t.clone();
113+
let _s = s.clone();
114+
let _t = t.clone();
111115

112116
#[derive(Clone)]
113117
struct Foo {

0 commit comments

Comments
 (0)