Add run-rustfix
This commit is contained in:
parent
301ef6bb2a
commit
667223c35d
@ -208,13 +208,21 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantClone {
|
||||
let sugg_span = span.with_lo(
|
||||
span.lo() + BytePos(u32::try_from(dot).unwrap())
|
||||
);
|
||||
let mut app = Applicability::MaybeIncorrect;
|
||||
let mut call_snip = &snip[dot + 1..];
|
||||
if call_snip.ends_with("()") {
|
||||
call_snip = call_snip[..call_snip.len()-2].trim();
|
||||
if call_snip.as_bytes().iter().all(|b| b.is_ascii_alphabetic() || *b == b'_') {
|
||||
app = Applicability::MachineApplicable;
|
||||
}
|
||||
}
|
||||
|
||||
span_lint_hir_and_then(cx, REDUNDANT_CLONE, node, sugg_span, "redundant clone", |db| {
|
||||
db.span_suggestion(
|
||||
sugg_span,
|
||||
"remove this",
|
||||
String::new(),
|
||||
Applicability::MaybeIncorrect,
|
||||
app,
|
||||
);
|
||||
db.span_note(
|
||||
span.with_hi(span.lo() + BytePos(u32::try_from(dot).unwrap())),
|
||||
|
132
tests/ui/redundant_clone.fixed
Normal file
132
tests/ui/redundant_clone.fixed
Normal file
@ -0,0 +1,132 @@
|
||||
// run-rustfix
|
||||
// rustfix-only-machine-applicable
|
||||
use std::ffi::OsString;
|
||||
use std::path::Path;
|
||||
|
||||
fn main() {
|
||||
let _s = ["lorem", "ipsum"].join(" ");
|
||||
|
||||
let s = String::from("foo");
|
||||
let _s = s;
|
||||
|
||||
let s = String::from("foo");
|
||||
let _s = s;
|
||||
|
||||
let s = String::from("foo");
|
||||
let _s = s;
|
||||
|
||||
let _s = Path::new("/a/b/").join("c");
|
||||
|
||||
let _s = Path::new("/a/b/").join("c");
|
||||
|
||||
let _s = OsString::new();
|
||||
|
||||
let _s = OsString::new();
|
||||
|
||||
// Check that lint level works
|
||||
#[allow(clippy::redundant_clone)]
|
||||
let _s = String::new().to_string();
|
||||
|
||||
let tup = (String::from("foo"),);
|
||||
let _t = tup.0;
|
||||
|
||||
let tup_ref = &(String::from("foo"),);
|
||||
let _s = tup_ref.0.clone(); // this `.clone()` cannot be removed
|
||||
|
||||
{
|
||||
let x = String::new();
|
||||
let y = &x;
|
||||
|
||||
let _x = x.clone(); // ok; `x` is borrowed by `y`
|
||||
|
||||
let _ = y.len();
|
||||
}
|
||||
|
||||
let x = (String::new(),);
|
||||
let _ = Some(String::new()).unwrap_or_else(|| x.0.clone()); // ok; closure borrows `x`
|
||||
|
||||
with_branch(Alpha, true);
|
||||
cannot_move_from_type_with_drop();
|
||||
borrower_propagation();
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct Alpha;
|
||||
fn with_branch(a: Alpha, b: bool) -> (Alpha, Alpha) {
|
||||
if b {
|
||||
(a.clone(), a)
|
||||
} 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
|
||||
}
|
||||
|
||||
fn borrower_propagation() {
|
||||
let s = String::new();
|
||||
let t = String::new();
|
||||
|
||||
{
|
||||
fn b() -> bool {
|
||||
unimplemented!()
|
||||
}
|
||||
let _u = if b() { &s } else { &t };
|
||||
|
||||
// ok; `s` and `t` are possibly borrowed
|
||||
let _s = s.clone();
|
||||
let _t = t.clone();
|
||||
}
|
||||
|
||||
{
|
||||
let _u = || s.len();
|
||||
let _v = [&t; 32];
|
||||
let _s = s.clone(); // ok
|
||||
let _t = t.clone(); // ok
|
||||
}
|
||||
|
||||
{
|
||||
let _u = {
|
||||
let u = Some(&s);
|
||||
let _ = s.clone(); // ok
|
||||
u
|
||||
};
|
||||
let _s = s.clone(); // ok
|
||||
}
|
||||
|
||||
{
|
||||
use std::convert::identity as id;
|
||||
let _u = id(id(&s));
|
||||
let _s = s.clone(); // ok, `u` borrows `s`
|
||||
}
|
||||
|
||||
let _s = s;
|
||||
let _t = t;
|
||||
|
||||
#[derive(Clone)]
|
||||
struct Foo {
|
||||
x: usize,
|
||||
}
|
||||
|
||||
{
|
||||
let f = Foo { x: 123 };
|
||||
let _x = Some(f.x);
|
||||
let _f = f;
|
||||
}
|
||||
|
||||
{
|
||||
let f = Foo { x: 123 };
|
||||
let _x = &f.x;
|
||||
let _f = f.clone(); // ok
|
||||
}
|
||||
}
|
@ -1,34 +1,34 @@
|
||||
#![warn(clippy::redundant_clone)]
|
||||
|
||||
// run-rustfix
|
||||
// rustfix-only-machine-applicable
|
||||
use std::ffi::OsString;
|
||||
use std::path::Path;
|
||||
|
||||
fn main() {
|
||||
let _ = ["lorem", "ipsum"].join(" ").to_string();
|
||||
let _s = ["lorem", "ipsum"].join(" ").to_string();
|
||||
|
||||
let s = String::from("foo");
|
||||
let _ = s.clone();
|
||||
let _s = s.clone();
|
||||
|
||||
let s = String::from("foo");
|
||||
let _ = s.to_string();
|
||||
let _s = s.to_string();
|
||||
|
||||
let s = String::from("foo");
|
||||
let _ = s.to_owned();
|
||||
let _s = s.to_owned();
|
||||
|
||||
let _ = Path::new("/a/b/").join("c").to_owned();
|
||||
let _s = Path::new("/a/b/").join("c").to_owned();
|
||||
|
||||
let _ = Path::new("/a/b/").join("c").to_path_buf();
|
||||
let _s = Path::new("/a/b/").join("c").to_path_buf();
|
||||
|
||||
let _ = OsString::new().to_owned();
|
||||
let _s = OsString::new().to_owned();
|
||||
|
||||
let _ = OsString::new().to_os_string();
|
||||
let _s = OsString::new().to_os_string();
|
||||
|
||||
// Check that lint level works
|
||||
#[allow(clippy::redundant_clone)]
|
||||
let _ = String::new().to_string();
|
||||
let _s = String::new().to_string();
|
||||
|
||||
let tup = (String::from("foo"),);
|
||||
let _ = tup.0.clone();
|
||||
let _t = tup.0.clone();
|
||||
|
||||
let tup_ref = &(String::from("foo"),);
|
||||
let _s = tup_ref.0.clone(); // this `.clone()` cannot be removed
|
||||
@ -37,13 +37,17 @@ fn main() {
|
||||
let x = String::new();
|
||||
let y = &x;
|
||||
|
||||
let _ = x.clone(); // ok; `x` is borrowed by `y`
|
||||
let _x = x.clone(); // ok; `x` is borrowed by `y`
|
||||
|
||||
let _ = y.len();
|
||||
}
|
||||
|
||||
let x = (String::new(),);
|
||||
let _ = Some(String::new()).unwrap_or_else(|| x.0.clone()); // ok; closure borrows `x`
|
||||
|
||||
with_branch(Alpha, true);
|
||||
cannot_move_from_type_with_drop();
|
||||
borrower_propagation();
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
@ -77,37 +81,37 @@ fn borrower_propagation() {
|
||||
fn b() -> bool {
|
||||
unimplemented!()
|
||||
}
|
||||
let u = if b() { &s } else { &t };
|
||||
let _u = if b() { &s } else { &t };
|
||||
|
||||
// ok; `s` and `t` are possibly borrowed
|
||||
let _ = s.clone();
|
||||
let _ = t.clone();
|
||||
let _s = s.clone();
|
||||
let _t = t.clone();
|
||||
}
|
||||
|
||||
{
|
||||
let u = || s.len();
|
||||
let v = [&t; 32];
|
||||
let _ = s.clone(); // ok
|
||||
let _ = t.clone(); // ok
|
||||
let _u = || s.len();
|
||||
let _v = [&t; 32];
|
||||
let _s = s.clone(); // ok
|
||||
let _t = t.clone(); // ok
|
||||
}
|
||||
|
||||
{
|
||||
let u = {
|
||||
let _u = {
|
||||
let u = Some(&s);
|
||||
let _ = s.clone(); // ok
|
||||
u
|
||||
};
|
||||
let _ = s.clone(); // ok
|
||||
let _s = s.clone(); // ok
|
||||
}
|
||||
|
||||
{
|
||||
use std::convert::identity as id;
|
||||
let u = id(id(&s));
|
||||
let _ = s.clone(); // ok, `u` borrows `s`
|
||||
let _u = id(id(&s));
|
||||
let _s = s.clone(); // ok, `u` borrows `s`
|
||||
}
|
||||
|
||||
let _ = s.clone();
|
||||
let _ = t.clone();
|
||||
let _s = s.clone();
|
||||
let _t = t.clone();
|
||||
|
||||
#[derive(Clone)]
|
||||
struct Foo {
|
||||
|
@ -1,156 +1,156 @@
|
||||
error: redundant clone
|
||||
--> $DIR/redundant_clone.rs:7:41
|
||||
--> $DIR/redundant_clone.rs:7:42
|
||||
|
|
||||
LL | let _ = ["lorem", "ipsum"].join(" ").to_string();
|
||||
| ^^^^^^^^^^^^ help: remove this
|
||||
LL | let _s = ["lorem", "ipsum"].join(" ").to_string();
|
||||
| ^^^^^^^^^^^^ help: remove this
|
||||
|
|
||||
= note: `-D clippy::redundant-clone` implied by `-D warnings`
|
||||
note: this value is dropped without further use
|
||||
--> $DIR/redundant_clone.rs:7:13
|
||||
--> $DIR/redundant_clone.rs:7:14
|
||||
|
|
||||
LL | let _ = ["lorem", "ipsum"].join(" ").to_string();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
LL | let _s = ["lorem", "ipsum"].join(" ").to_string();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: redundant clone
|
||||
--> $DIR/redundant_clone.rs:10:15
|
||||
|
|
||||
LL | let _s = s.clone();
|
||||
| ^^^^^^^^ help: remove this
|
||||
|
|
||||
note: this value is dropped without further use
|
||||
--> $DIR/redundant_clone.rs:10:14
|
||||
|
|
||||
LL | let _ = s.clone();
|
||||
| ^^^^^^^^ help: remove this
|
||||
|
|
||||
note: this value is dropped without further use
|
||||
--> $DIR/redundant_clone.rs:10:13
|
||||
|
|
||||
LL | let _ = s.clone();
|
||||
| ^
|
||||
LL | let _s = s.clone();
|
||||
| ^
|
||||
|
||||
error: redundant clone
|
||||
--> $DIR/redundant_clone.rs:13:15
|
||||
|
|
||||
LL | let _s = s.to_string();
|
||||
| ^^^^^^^^^^^^ help: remove this
|
||||
|
|
||||
note: this value is dropped without further use
|
||||
--> $DIR/redundant_clone.rs:13:14
|
||||
|
|
||||
LL | let _ = s.to_string();
|
||||
| ^^^^^^^^^^^^ help: remove this
|
||||
|
|
||||
note: this value is dropped without further use
|
||||
--> $DIR/redundant_clone.rs:13:13
|
||||
|
|
||||
LL | let _ = s.to_string();
|
||||
| ^
|
||||
LL | let _s = s.to_string();
|
||||
| ^
|
||||
|
||||
error: redundant clone
|
||||
--> $DIR/redundant_clone.rs:16:15
|
||||
|
|
||||
LL | let _s = s.to_owned();
|
||||
| ^^^^^^^^^^^ help: remove this
|
||||
|
|
||||
note: this value is dropped without further use
|
||||
--> $DIR/redundant_clone.rs:16:14
|
||||
|
|
||||
LL | let _ = s.to_owned();
|
||||
| ^^^^^^^^^^^ help: remove this
|
||||
|
|
||||
note: this value is dropped without further use
|
||||
--> $DIR/redundant_clone.rs:16:13
|
||||
|
|
||||
LL | let _ = s.to_owned();
|
||||
| ^
|
||||
LL | let _s = s.to_owned();
|
||||
| ^
|
||||
|
||||
error: redundant clone
|
||||
--> $DIR/redundant_clone.rs:18:41
|
||||
--> $DIR/redundant_clone.rs:18:42
|
||||
|
|
||||
LL | let _ = Path::new("/a/b/").join("c").to_owned();
|
||||
| ^^^^^^^^^^^ help: remove this
|
||||
LL | let _s = Path::new("/a/b/").join("c").to_owned();
|
||||
| ^^^^^^^^^^^ help: remove this
|
||||
|
|
||||
note: this value is dropped without further use
|
||||
--> $DIR/redundant_clone.rs:18:13
|
||||
--> $DIR/redundant_clone.rs:18:14
|
||||
|
|
||||
LL | let _ = Path::new("/a/b/").join("c").to_owned();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
LL | let _s = Path::new("/a/b/").join("c").to_owned();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: redundant clone
|
||||
--> $DIR/redundant_clone.rs:20:41
|
||||
--> $DIR/redundant_clone.rs:20:42
|
||||
|
|
||||
LL | let _ = Path::new("/a/b/").join("c").to_path_buf();
|
||||
| ^^^^^^^^^^^^^^ help: remove this
|
||||
LL | let _s = Path::new("/a/b/").join("c").to_path_buf();
|
||||
| ^^^^^^^^^^^^^^ help: remove this
|
||||
|
|
||||
note: this value is dropped without further use
|
||||
--> $DIR/redundant_clone.rs:20:13
|
||||
--> $DIR/redundant_clone.rs:20:14
|
||||
|
|
||||
LL | let _ = Path::new("/a/b/").join("c").to_path_buf();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
LL | let _s = Path::new("/a/b/").join("c").to_path_buf();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: redundant clone
|
||||
--> $DIR/redundant_clone.rs:22:28
|
||||
--> $DIR/redundant_clone.rs:22:29
|
||||
|
|
||||
LL | let _ = OsString::new().to_owned();
|
||||
| ^^^^^^^^^^^ help: remove this
|
||||
LL | let _s = OsString::new().to_owned();
|
||||
| ^^^^^^^^^^^ help: remove this
|
||||
|
|
||||
note: this value is dropped without further use
|
||||
--> $DIR/redundant_clone.rs:22:13
|
||||
--> $DIR/redundant_clone.rs:22:14
|
||||
|
|
||||
LL | let _ = OsString::new().to_owned();
|
||||
| ^^^^^^^^^^^^^^^
|
||||
LL | let _s = OsString::new().to_owned();
|
||||
| ^^^^^^^^^^^^^^^
|
||||
|
||||
error: redundant clone
|
||||
--> $DIR/redundant_clone.rs:24:28
|
||||
--> $DIR/redundant_clone.rs:24:29
|
||||
|
|
||||
LL | let _ = OsString::new().to_os_string();
|
||||
| ^^^^^^^^^^^^^^^ help: remove this
|
||||
LL | let _s = OsString::new().to_os_string();
|
||||
| ^^^^^^^^^^^^^^^ help: remove this
|
||||
|
|
||||
note: this value is dropped without further use
|
||||
--> $DIR/redundant_clone.rs:24:13
|
||||
--> $DIR/redundant_clone.rs:24:14
|
||||
|
|
||||
LL | let _ = OsString::new().to_os_string();
|
||||
| ^^^^^^^^^^^^^^^
|
||||
LL | let _s = OsString::new().to_os_string();
|
||||
| ^^^^^^^^^^^^^^^
|
||||
|
||||
error: redundant clone
|
||||
--> $DIR/redundant_clone.rs:31:18
|
||||
--> $DIR/redundant_clone.rs:31:19
|
||||
|
|
||||
LL | let _ = tup.0.clone();
|
||||
| ^^^^^^^^ help: remove this
|
||||
LL | let _t = tup.0.clone();
|
||||
| ^^^^^^^^ help: remove this
|
||||
|
|
||||
note: this value is dropped without further use
|
||||
--> $DIR/redundant_clone.rs:31:13
|
||||
--> $DIR/redundant_clone.rs:31:14
|
||||
|
|
||||
LL | let _ = tup.0.clone();
|
||||
| ^^^^^
|
||||
LL | let _t = tup.0.clone();
|
||||
| ^^^^^
|
||||
|
||||
error: redundant clone
|
||||
--> $DIR/redundant_clone.rs:53:22
|
||||
--> $DIR/redundant_clone.rs:57:22
|
||||
|
|
||||
LL | (a.clone(), a.clone())
|
||||
| ^^^^^^^^ help: remove this
|
||||
|
|
||||
note: this value is dropped without further use
|
||||
--> $DIR/redundant_clone.rs:53:21
|
||||
--> $DIR/redundant_clone.rs:57:21
|
||||
|
|
||||
LL | (a.clone(), a.clone())
|
||||
| ^
|
||||
|
||||
error: redundant clone
|
||||
--> $DIR/redundant_clone.rs:109:14
|
||||
--> $DIR/redundant_clone.rs:113:15
|
||||
|
|
||||
LL | let _ = s.clone();
|
||||
| ^^^^^^^^ help: remove this
|
||||
LL | let _s = s.clone();
|
||||
| ^^^^^^^^ help: remove this
|
||||
|
|
||||
note: this value is dropped without further use
|
||||
--> $DIR/redundant_clone.rs:109:13
|
||||
--> $DIR/redundant_clone.rs:113:14
|
||||
|
|
||||
LL | let _ = s.clone();
|
||||
| ^
|
||||
LL | let _s = s.clone();
|
||||
| ^
|
||||
|
||||
error: redundant clone
|
||||
--> $DIR/redundant_clone.rs:110:14
|
||||
--> $DIR/redundant_clone.rs:114:15
|
||||
|
|
||||
LL | let _ = t.clone();
|
||||
| ^^^^^^^^ help: remove this
|
||||
LL | let _t = t.clone();
|
||||
| ^^^^^^^^ help: remove this
|
||||
|
|
||||
note: this value is dropped without further use
|
||||
--> $DIR/redundant_clone.rs:110:13
|
||||
--> $DIR/redundant_clone.rs:114:14
|
||||
|
|
||||
LL | let _ = t.clone();
|
||||
| ^
|
||||
LL | let _t = t.clone();
|
||||
| ^
|
||||
|
||||
error: redundant clone
|
||||
--> $DIR/redundant_clone.rs:120:19
|
||||
--> $DIR/redundant_clone.rs:124:19
|
||||
|
|
||||
LL | let _f = f.clone();
|
||||
| ^^^^^^^^ help: remove this
|
||||
|
|
||||
note: this value is dropped without further use
|
||||
--> $DIR/redundant_clone.rs:120:18
|
||||
--> $DIR/redundant_clone.rs:124:18
|
||||
|
|
||||
LL | let _f = f.clone();
|
||||
| ^
|
||||
|
Loading…
x
Reference in New Issue
Block a user