Auto merge of #3518 - sinkuu:redundant_clone_tw, r=phansch

Lint redundant clone of fields

Makes `redundant_clone` warn on unnecessary `foo.field.clone()` sometimes (it can detect an unnecessary clone only if the base of projection, `foo` in this case, is not used at all after that). This is enough for cases like `returns_tuple().0.clone()`.
This commit is contained in:
bors 2018-12-10 18:55:49 +00:00
commit ada0b2b095
6 changed files with 132 additions and 39 deletions

View file

@ -34,14 +34,35 @@ fn main() {
// Check that lint level works
#[allow(clippy::redundant_clone)] let _ = String::new().to_string();
let tup = (String::from("foo"),);
let _ = tup.0.clone();
let tup_ref = &(String::from("foo"),);
let _s = tup_ref.0.clone(); // this `.clone()` cannot be removed
}
#[derive(Clone)]
struct Alpha;
fn double(a: Alpha) -> (Alpha, Alpha) {
if true {
fn with_branch(a: Alpha, b: bool) -> (Alpha, Alpha) {
if b {
(a.clone(), a.clone())
} else {
(Alpha, a)
}
}
struct TypeWithDrop {
x: String,
}
impl Drop for TypeWithDrop {
fn drop(&mut self) {}
}
fn cannot_move_from_type_with_drop() -> String {
let s = TypeWithDrop {
x: String::new()
};
s.x.clone() // removing this `clone()` summons E0509
}

View file

@ -96,16 +96,28 @@ note: this value is dropped without further use
| ^^^^^^^^^^^^^^^
error: redundant clone
--> $DIR/redundant_clone.rs:43:22
--> $DIR/redundant_clone.rs:39:18
|
43 | (a.clone(), a.clone())
39 | let _ = tup.0.clone();
| ^^^^^^^^ help: remove this
|
note: this value is dropped without further use
--> $DIR/redundant_clone.rs:39:13
|
39 | let _ = tup.0.clone();
| ^^^^^
error: redundant clone
--> $DIR/redundant_clone.rs:49:22
|
49 | (a.clone(), a.clone())
| ^^^^^^^^ help: remove this
|
note: this value is dropped without further use
--> $DIR/redundant_clone.rs:43:21
--> $DIR/redundant_clone.rs:49:21
|
43 | (a.clone(), a.clone())
49 | (a.clone(), a.clone())
| ^
error: aborting due to 9 previous errors
error: aborting due to 10 previous errors