2019-09-09 21:56:34 -05:00
|
|
|
// run-rustfix
|
|
|
|
// rustfix-only-machine-applicable
|
2019-11-19 02:32:35 -06:00
|
|
|
|
2019-09-09 21:56:34 -05:00
|
|
|
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");
|
|
|
|
|
2020-01-07 22:59:58 -06:00
|
|
|
let _s = Path::new("/a/b/").join("c");
|
2019-09-09 21:56:34 -05:00
|
|
|
|
|
|
|
let _s = OsString::new();
|
|
|
|
|
2020-01-07 22:59:58 -06:00
|
|
|
let _s = OsString::new();
|
2019-09-09 21:56:34 -05:00
|
|
|
|
|
|
|
// 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);
|
2019-11-22 07:24:38 -06:00
|
|
|
cannot_double_move(Alpha);
|
2019-09-09 21:56:34 -05:00
|
|
|
cannot_move_from_type_with_drop();
|
|
|
|
borrower_propagation();
|
2020-03-12 10:31:09 -05:00
|
|
|
not_consumed();
|
2020-04-12 04:55:47 -05:00
|
|
|
issue_5405();
|
2020-07-19 19:00:00 -05:00
|
|
|
manually_drop();
|
2019-09-09 21:56:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct Alpha;
|
|
|
|
fn with_branch(a: Alpha, b: bool) -> (Alpha, Alpha) {
|
|
|
|
if b {
|
|
|
|
(a.clone(), a)
|
|
|
|
} else {
|
|
|
|
(Alpha, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-22 07:24:38 -06:00
|
|
|
fn cannot_double_move(a: Alpha) -> (Alpha, Alpha) {
|
|
|
|
(a.clone(), a)
|
|
|
|
}
|
|
|
|
|
2019-09-09 21:56:34 -05: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() };
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2020-03-12 10:31:09 -05:00
|
|
|
|
|
|
|
fn not_consumed() {
|
|
|
|
let x = std::path::PathBuf::from("home");
|
|
|
|
let y = x.join("matthias");
|
|
|
|
// join() creates a new owned PathBuf, does not take a &mut to x variable, thus the .clone() is
|
|
|
|
// redundant. (It also does not consume the PathBuf)
|
|
|
|
|
|
|
|
println!("x: {:?}, y: {:?}", x, y);
|
2020-03-12 11:25:18 -05:00
|
|
|
|
|
|
|
let mut s = String::new();
|
|
|
|
s.clone().push_str("foo"); // OK, removing this `clone()` will change the behavior.
|
|
|
|
s.push_str("bar");
|
|
|
|
assert_eq!(s, "bar");
|
2020-03-12 12:06:47 -05:00
|
|
|
|
|
|
|
let t = Some(s);
|
|
|
|
// OK
|
|
|
|
if let Some(x) = t.clone() {
|
|
|
|
println!("{}", x);
|
|
|
|
}
|
|
|
|
if let Some(x) = t {
|
|
|
|
println!("{}", x);
|
|
|
|
}
|
2020-03-12 10:31:09 -05:00
|
|
|
}
|
2020-04-12 04:55:47 -05:00
|
|
|
|
|
|
|
#[allow(clippy::clone_on_copy)]
|
|
|
|
fn issue_5405() {
|
|
|
|
let a: [String; 1] = [String::from("foo")];
|
|
|
|
let _b: String = a[0].clone();
|
|
|
|
|
|
|
|
let c: [usize; 2] = [2, 3];
|
|
|
|
let _d: usize = c[1].clone();
|
|
|
|
}
|
2020-07-19 19:00:00 -05:00
|
|
|
|
|
|
|
fn manually_drop() {
|
|
|
|
use std::mem::ManuallyDrop;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
let a = ManuallyDrop::new(Arc::new("Hello!".to_owned()));
|
|
|
|
let _ = a.clone(); // OK
|
|
|
|
|
|
|
|
let p: *const String = Arc::into_raw(ManuallyDrop::into_inner(a));
|
|
|
|
unsafe {
|
|
|
|
Arc::from_raw(p);
|
|
|
|
Arc::from_raw(p);
|
|
|
|
}
|
|
|
|
}
|