2019-09-10 02:56:34 +00:00
|
|
|
// rustfix-only-machine-applicable
|
2022-06-25 12:16:29 +00:00
|
|
|
#![feature(lint_reasons)]
|
2023-06-02 15:13:55 +00:00
|
|
|
#![warn(clippy::redundant_clone)]
|
2023-05-31 17:47:10 +00:00
|
|
|
#![allow(
|
|
|
|
clippy::drop_non_drop,
|
|
|
|
clippy::implicit_clone,
|
|
|
|
clippy::uninlined_format_args,
|
|
|
|
clippy::unnecessary_literal_unwrap
|
|
|
|
)]
|
2022-10-02 19:13:22 +00:00
|
|
|
|
2018-10-23 07:01:45 +00:00
|
|
|
use std::ffi::OsString;
|
2018-12-09 22:26:16 +00:00
|
|
|
use std::path::Path;
|
2018-10-23 07:01:45 +00:00
|
|
|
|
|
|
|
fn main() {
|
2019-09-10 02:56:34 +00:00
|
|
|
let _s = ["lorem", "ipsum"].join(" ").to_string();
|
2018-10-23 07:01:45 +00:00
|
|
|
|
|
|
|
let s = String::from("foo");
|
2019-09-10 02:56:34 +00:00
|
|
|
let _s = s.clone();
|
2018-10-23 07:01:45 +00:00
|
|
|
|
|
|
|
let s = String::from("foo");
|
2019-09-10 02:56:34 +00:00
|
|
|
let _s = s.to_string();
|
2018-10-23 07:01:45 +00:00
|
|
|
|
|
|
|
let s = String::from("foo");
|
2019-09-10 02:56:34 +00:00
|
|
|
let _s = s.to_owned();
|
2018-10-23 07:01:45 +00:00
|
|
|
|
2019-09-10 02:56:34 +00:00
|
|
|
let _s = Path::new("/a/b/").join("c").to_owned();
|
2018-10-23 07:01:45 +00:00
|
|
|
|
2019-09-10 02:56:34 +00:00
|
|
|
let _s = Path::new("/a/b/").join("c").to_path_buf();
|
2018-10-23 07:01:45 +00:00
|
|
|
|
2019-09-10 02:56:34 +00:00
|
|
|
let _s = OsString::new().to_owned();
|
2018-10-23 07:01:45 +00:00
|
|
|
|
2019-09-10 02:56:34 +00:00
|
|
|
let _s = OsString::new().to_os_string();
|
2018-10-25 12:08:32 +00:00
|
|
|
|
|
|
|
// Check that lint level works
|
2018-12-09 22:26:16 +00:00
|
|
|
#[allow(clippy::redundant_clone)]
|
2019-09-10 02:56:34 +00:00
|
|
|
let _s = String::new().to_string();
|
2018-12-09 11:19:21 +00:00
|
|
|
|
2022-06-25 12:16:29 +00:00
|
|
|
// Check that lint level works
|
|
|
|
#[expect(clippy::redundant_clone)]
|
|
|
|
let _s = String::new().to_string();
|
|
|
|
|
2018-12-09 11:19:21 +00:00
|
|
|
let tup = (String::from("foo"),);
|
2019-09-10 02:56:34 +00:00
|
|
|
let _t = tup.0.clone();
|
2018-12-09 11:19:21 +00:00
|
|
|
|
|
|
|
let tup_ref = &(String::from("foo"),);
|
|
|
|
let _s = tup_ref.0.clone(); // this `.clone()` cannot be removed
|
2019-09-16 15:50:15 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
let x = String::new();
|
|
|
|
let y = &x;
|
|
|
|
|
2019-09-10 02:56:34 +00:00
|
|
|
let _x = x.clone(); // ok; `x` is borrowed by `y`
|
2019-09-16 15:50:15 +00:00
|
|
|
|
|
|
|
let _ = y.len();
|
|
|
|
}
|
|
|
|
|
|
|
|
let x = (String::new(),);
|
|
|
|
let _ = Some(String::new()).unwrap_or_else(|| x.0.clone()); // ok; closure borrows `x`
|
2019-09-10 02:56:34 +00:00
|
|
|
|
|
|
|
with_branch(Alpha, true);
|
2019-11-22 13:24:38 +00:00
|
|
|
cannot_double_move(Alpha);
|
2019-09-10 02:56:34 +00:00
|
|
|
cannot_move_from_type_with_drop();
|
|
|
|
borrower_propagation();
|
2020-03-12 15:31:09 +00:00
|
|
|
not_consumed();
|
2020-04-12 09:55:47 +00:00
|
|
|
issue_5405();
|
2020-07-20 00:00:00 +00:00
|
|
|
manually_drop();
|
2021-03-31 18:58:17 +00:00
|
|
|
clone_then_move_cloned();
|
2021-06-11 12:57:11 +00:00
|
|
|
hashmap_neg();
|
|
|
|
false_negative_5707();
|
2018-10-23 07:01:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct Alpha;
|
2018-12-09 10:18:44 +00:00
|
|
|
fn with_branch(a: Alpha, b: bool) -> (Alpha, Alpha) {
|
2021-03-01 17:53:33 +00:00
|
|
|
if b { (a.clone(), a.clone()) } else { (Alpha, a) }
|
2018-10-23 07:01:45 +00:00
|
|
|
}
|
2018-12-09 11:19:21 +00:00
|
|
|
|
2019-11-22 13:24:38 +00:00
|
|
|
fn cannot_double_move(a: Alpha) -> (Alpha, Alpha) {
|
|
|
|
(a.clone(), a)
|
|
|
|
}
|
|
|
|
|
2018-12-09 11:19:21 +00:00
|
|
|
struct TypeWithDrop {
|
|
|
|
x: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for TypeWithDrop {
|
|
|
|
fn drop(&mut self) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cannot_move_from_type_with_drop() -> String {
|
2018-12-11 00:41:59 +00:00
|
|
|
let s = TypeWithDrop { x: String::new() };
|
2018-12-09 11:19:21 +00:00
|
|
|
s.x.clone() // removing this `clone()` summons E0509
|
|
|
|
}
|
2019-09-16 15:50:15 +00:00
|
|
|
|
|
|
|
fn borrower_propagation() {
|
|
|
|
let s = String::new();
|
|
|
|
let t = String::new();
|
|
|
|
|
|
|
|
{
|
|
|
|
fn b() -> bool {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2019-09-10 02:56:34 +00:00
|
|
|
let _u = if b() { &s } else { &t };
|
2019-09-16 15:50:15 +00:00
|
|
|
|
|
|
|
// ok; `s` and `t` are possibly borrowed
|
2019-09-10 02:56:34 +00:00
|
|
|
let _s = s.clone();
|
|
|
|
let _t = t.clone();
|
2019-09-16 15:50:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-09-10 02:56:34 +00:00
|
|
|
let _u = || s.len();
|
|
|
|
let _v = [&t; 32];
|
|
|
|
let _s = s.clone(); // ok
|
|
|
|
let _t = t.clone(); // ok
|
2019-09-16 15:50:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-09-10 02:56:34 +00:00
|
|
|
let _u = {
|
2019-09-16 15:50:15 +00:00
|
|
|
let u = Some(&s);
|
|
|
|
let _ = s.clone(); // ok
|
|
|
|
u
|
|
|
|
};
|
2019-09-10 02:56:34 +00:00
|
|
|
let _s = s.clone(); // ok
|
2019-09-16 15:50:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
use std::convert::identity as id;
|
2019-09-10 02:56:34 +00:00
|
|
|
let _u = id(id(&s));
|
|
|
|
let _s = s.clone(); // ok, `u` borrows `s`
|
2019-09-16 15:50:15 +00:00
|
|
|
}
|
|
|
|
|
2019-09-10 02:56:34 +00:00
|
|
|
let _s = s.clone();
|
|
|
|
let _t = t.clone();
|
2019-09-16 15:50:15 +00:00
|
|
|
|
|
|
|
#[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
|
|
|
|
}
|
|
|
|
}
|
2020-03-12 15:31:09 +00:00
|
|
|
|
|
|
|
fn not_consumed() {
|
|
|
|
let x = std::path::PathBuf::from("home");
|
|
|
|
let y = x.clone().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 16:25:18 +00: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 17:06:47 +00:00
|
|
|
|
|
|
|
let t = Some(s);
|
|
|
|
// OK
|
|
|
|
if let Some(x) = t.clone() {
|
|
|
|
println!("{}", x);
|
|
|
|
}
|
|
|
|
if let Some(x) = t {
|
|
|
|
println!("{}", x);
|
|
|
|
}
|
2020-03-12 15:31:09 +00:00
|
|
|
}
|
2020-04-12 09:55:47 +00: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-20 00:00:00 +00: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);
|
|
|
|
}
|
|
|
|
}
|
2021-03-31 18:58:17 +00:00
|
|
|
|
|
|
|
fn clone_then_move_cloned() {
|
|
|
|
// issue #5973
|
|
|
|
let x = Some(String::new());
|
|
|
|
// ok, x is moved while the clone is in use.
|
|
|
|
assert_eq!(x.clone(), None, "not equal {}", x.unwrap());
|
|
|
|
|
|
|
|
// issue #5595
|
|
|
|
fn foo<F: Fn()>(_: &Alpha, _: F) {}
|
|
|
|
let x = Alpha;
|
|
|
|
// ok, data is moved while the clone is in use.
|
|
|
|
foo(&x.clone(), move || {
|
|
|
|
let _ = x;
|
|
|
|
});
|
|
|
|
|
|
|
|
// issue #6998
|
|
|
|
struct S(String);
|
|
|
|
impl S {
|
|
|
|
fn m(&mut self) {}
|
|
|
|
}
|
|
|
|
let mut x = S(String::new());
|
|
|
|
x.0.clone().chars().for_each(|_| x.m());
|
|
|
|
}
|
2021-06-11 12:57:11 +00:00
|
|
|
|
|
|
|
fn hashmap_neg() {
|
|
|
|
// issue 5707
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
let p = PathBuf::from("/");
|
|
|
|
|
|
|
|
let mut h: HashMap<&str, &str> = HashMap::new();
|
|
|
|
h.insert("orig-p", p.to_str().unwrap());
|
|
|
|
|
|
|
|
let mut q = p.clone();
|
|
|
|
q.push("foo");
|
|
|
|
|
|
|
|
println!("{:?} {}", h, q.display());
|
|
|
|
}
|
|
|
|
|
|
|
|
fn false_negative_5707() {
|
|
|
|
fn foo(_x: &Alpha, _y: &mut Alpha) {}
|
|
|
|
|
|
|
|
let x = Alpha;
|
|
|
|
let mut y = Alpha;
|
|
|
|
foo(&x, &mut y);
|
|
|
|
let _z = x.clone(); // pr 7346 can't lint on `x`
|
|
|
|
drop(y);
|
|
|
|
}
|