Skip to content

Commit 52b9e70

Browse files
committed
Auto merge of #5017 - sinkuu:mir_no_opt_fallout, r=flip1995
Fix redundant_clone lint not working with PathBuf and OsString #4825 diabled MIR optimization in clippy, including `rustc_mir::transform::InstCombine` which reduces `&(*x)` to `x`. This PR tries to unwrap `&*` when looking into `mir::Rvalue`s. Fixes #5014. --- changelog: fixed `redundant_clone` lint not working with `PathBuf` and `OsString`
2 parents c789caa + 99eec3f commit 52b9e70

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

clippy_lints/src/redundant_clone.rs

+7
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,13 @@ fn find_stmt_assigns_to<'tcx>(
307307
(true, mir::Rvalue::Ref(_, _, place)) | (false, mir::Rvalue::Use(mir::Operand::Copy(place))) => {
308308
base_local_and_movability(cx, mir, place)
309309
},
310+
(false, mir::Rvalue::Ref(_, _, place)) => {
311+
if let [mir::ProjectionElem::Deref] = place.as_ref().projection {
312+
base_local_and_movability(cx, mir, place)
313+
} else {
314+
None
315+
}
316+
},
310317
_ => None,
311318
}
312319
}

tests/ui/redundant_clone.fixed

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ fn main() {
1818

1919
let _s = Path::new("/a/b/").join("c");
2020

21-
let _s = Path::new("/a/b/").join("c").to_path_buf();
21+
let _s = Path::new("/a/b/").join("c");
2222

2323
let _s = OsString::new();
2424

25-
let _s = OsString::new().to_os_string();
25+
let _s = OsString::new();
2626

2727
// Check that lint level works
2828
#[allow(clippy::redundant_clone)]

tests/ui/redundant_clone.stderr

+25-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ note: this value is dropped without further use
5959
LL | let _s = Path::new("/a/b/").join("c").to_owned();
6060
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6161

62+
error: redundant clone
63+
--> $DIR/redundant_clone.rs:21:42
64+
|
65+
LL | let _s = Path::new("/a/b/").join("c").to_path_buf();
66+
| ^^^^^^^^^^^^^^ help: remove this
67+
|
68+
note: this value is dropped without further use
69+
--> $DIR/redundant_clone.rs:21:14
70+
|
71+
LL | let _s = Path::new("/a/b/").join("c").to_path_buf();
72+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
73+
6274
error: redundant clone
6375
--> $DIR/redundant_clone.rs:23:29
6476
|
@@ -71,6 +83,18 @@ note: this value is dropped without further use
7183
LL | let _s = OsString::new().to_owned();
7284
| ^^^^^^^^^^^^^^^
7385

86+
error: redundant clone
87+
--> $DIR/redundant_clone.rs:25:29
88+
|
89+
LL | let _s = OsString::new().to_os_string();
90+
| ^^^^^^^^^^^^^^^ help: remove this
91+
|
92+
note: this value is dropped without further use
93+
--> $DIR/redundant_clone.rs:25:14
94+
|
95+
LL | let _s = OsString::new().to_os_string();
96+
| ^^^^^^^^^^^^^^^
97+
7498
error: redundant clone
7599
--> $DIR/redundant_clone.rs:32:19
76100
|
@@ -131,5 +155,5 @@ note: this value is dropped without further use
131155
LL | let _f = f.clone();
132156
| ^
133157

134-
error: aborting due to 11 previous errors
158+
error: aborting due to 13 previous errors
135159

0 commit comments

Comments
 (0)