rust/tests/ui/redundant_clone.rs

139 lines
2.7 KiB
Rust
Raw Normal View History

2019-09-10 04:56:34 +02:00
// run-rustfix
// rustfix-only-machine-applicable
2018-10-23 09:01:45 +02:00
use std::ffi::OsString;
2018-12-09 23:26:16 +01:00
use std::path::Path;
2018-10-23 09:01:45 +02:00
fn main() {
2019-09-10 04:56:34 +02:00
let _s = ["lorem", "ipsum"].join(" ").to_string();
2018-10-23 09:01:45 +02:00
let s = String::from("foo");
2019-09-10 04:56:34 +02:00
let _s = s.clone();
2018-10-23 09:01:45 +02:00
let s = String::from("foo");
2019-09-10 04:56:34 +02:00
let _s = s.to_string();
2018-10-23 09:01:45 +02:00
let s = String::from("foo");
2019-09-10 04:56:34 +02:00
let _s = s.to_owned();
2018-10-23 09:01:45 +02:00
2019-09-10 04:56:34 +02:00
let _s = Path::new("/a/b/").join("c").to_owned();
2018-10-23 09:01:45 +02:00
2019-09-10 04:56:34 +02:00
let _s = Path::new("/a/b/").join("c").to_path_buf();
2018-10-23 09:01:45 +02:00
2019-09-10 04:56:34 +02:00
let _s = OsString::new().to_owned();
2018-10-23 09:01:45 +02:00
2019-09-10 04:56:34 +02:00
let _s = OsString::new().to_os_string();
2018-10-25 14:08:32 +02:00
// Check that lint level works
2018-12-09 23:26:16 +01:00
#[allow(clippy::redundant_clone)]
2019-09-10 04:56:34 +02:00
let _s = String::new().to_string();
2018-12-09 12:19:21 +01:00
let tup = (String::from("foo"),);
2019-09-10 04:56:34 +02:00
let _t = tup.0.clone();
2018-12-09 12:19:21 +01:00
let tup_ref = &(String::from("foo"),);
let _s = tup_ref.0.clone(); // this `.clone()` cannot be removed
{
let x = String::new();
let y = &x;
2019-09-10 04:56:34 +02:00
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`
2019-09-10 04:56:34 +02:00
with_branch(Alpha, true);
cannot_double_move(Alpha);
2019-09-10 04:56:34 +02:00
cannot_move_from_type_with_drop();
borrower_propagation();
2018-10-23 09:01:45 +02:00
}
#[derive(Clone)]
struct Alpha;
fn with_branch(a: Alpha, b: bool) -> (Alpha, Alpha) {
if b {
2018-10-23 09:01:45 +02:00
(a.clone(), a.clone())
} else {
(Alpha, a)
}
}
2018-12-09 12:19:21 +01:00
fn cannot_double_move(a: Alpha) -> (Alpha, Alpha) {
(a.clone(), a)
}
2018-12-09 12:19:21 +01:00
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() };
2018-12-09 12:19:21 +01:00
s.x.clone() // removing this `clone()` summons E0509
}
fn borrower_propagation() {
let s = String::new();
let t = String::new();
{
fn b() -> bool {
unimplemented!()
}
2019-09-10 04:56:34 +02:00
let _u = if b() { &s } else { &t };
// ok; `s` and `t` are possibly borrowed
2019-09-10 04:56:34 +02:00
let _s = s.clone();
let _t = t.clone();
}
{
2019-09-10 04:56:34 +02:00
let _u = || s.len();
let _v = [&t; 32];
let _s = s.clone(); // ok
let _t = t.clone(); // ok
}
{
2019-09-10 04:56:34 +02:00
let _u = {
let u = Some(&s);
let _ = s.clone(); // ok
u
};
2019-09-10 04:56:34 +02:00
let _s = s.clone(); // ok
}
{
use std::convert::identity as id;
2019-09-10 04:56:34 +02:00
let _u = id(id(&s));
let _s = s.clone(); // ok, `u` borrows `s`
}
2019-09-10 04:56:34 +02:00
let _s = s.clone();
let _t = t.clone();
#[derive(Clone)]
struct Foo {
x: usize,
}
{
let f = Foo { x: 123 };
let _x = Some(f.x);
let _f = f.clone();
}
{
let f = Foo { x: 123 };
let _x = &f.x;
let _f = f.clone(); // ok
}
}