2023-07-02 12:35:19 +00:00
|
|
|
#![allow(
|
|
|
|
unused,
|
|
|
|
clippy::many_single_char_names,
|
|
|
|
clippy::needless_lifetimes,
|
2023-07-17 08:19:29 +00:00
|
|
|
clippy::redundant_clone,
|
|
|
|
clippy::needless_pass_by_ref_mut
|
2023-07-02 12:35:19 +00:00
|
|
|
)]
|
2018-07-28 15:34:52 +00:00
|
|
|
#![warn(clippy::ptr_arg)]
|
2023-08-24 19:32:12 +00:00
|
|
|
//@no-rustfix
|
2018-04-05 15:59:35 +00:00
|
|
|
use std::borrow::Cow;
|
2022-10-23 13:18:45 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2018-04-05 15:59:35 +00:00
|
|
|
|
2017-02-08 13:58:07 +00:00
|
|
|
fn do_vec(x: &Vec<i64>) {
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: writing `&Vec` instead of `&[_]` involves a new object where a slice will do
|
|
|
|
//~| NOTE: `-D clippy::ptr-arg` implied by `-D warnings`
|
2015-08-11 18:22:20 +00:00
|
|
|
//Nothing here
|
2015-05-04 06:15:24 +00:00
|
|
|
}
|
|
|
|
|
2018-12-09 22:26:16 +00:00
|
|
|
fn do_vec_mut(x: &mut Vec<i64>) {
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: writing `&mut Vec` instead of `&mut [_]` involves a new object where a slice w
|
2015-08-21 16:28:17 +00:00
|
|
|
//Nothing here
|
|
|
|
}
|
|
|
|
|
2023-12-01 17:21:58 +00:00
|
|
|
fn do_vec_mut2(x: &mut Vec<i64>) {
|
|
|
|
//~^ ERROR: writing `&mut Vec` instead of `&mut [_]` involves a new object where a slice w
|
|
|
|
x.len();
|
|
|
|
x.is_empty();
|
|
|
|
}
|
|
|
|
|
2017-02-08 13:58:07 +00:00
|
|
|
fn do_str(x: &String) {
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: writing `&String` instead of `&str` involves a new object where a slice will d
|
2015-08-11 18:22:20 +00:00
|
|
|
//Nothing here either
|
2015-05-04 06:15:24 +00:00
|
|
|
}
|
|
|
|
|
2018-12-09 22:26:16 +00:00
|
|
|
fn do_str_mut(x: &mut String) {
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: writing `&mut String` instead of `&mut str` involves a new object where a slic
|
2015-08-21 16:28:17 +00:00
|
|
|
//Nothing here either
|
|
|
|
}
|
|
|
|
|
2021-01-02 15:29:43 +00:00
|
|
|
fn do_path(x: &PathBuf) {
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: writing `&PathBuf` instead of `&Path` involves a new object where a slice will
|
2021-01-02 15:29:43 +00:00
|
|
|
//Nothing here either
|
|
|
|
}
|
|
|
|
|
|
|
|
fn do_path_mut(x: &mut PathBuf) {
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: writing `&mut PathBuf` instead of `&mut Path` involves a new object where a sl
|
2021-01-02 15:29:43 +00:00
|
|
|
//Nothing here either
|
|
|
|
}
|
|
|
|
|
2018-12-09 22:26:16 +00:00
|
|
|
fn main() {}
|
2015-10-30 23:48:05 +00:00
|
|
|
|
|
|
|
trait Foo {
|
|
|
|
type Item;
|
2017-02-08 13:58:07 +00:00
|
|
|
fn do_vec(x: &Vec<i64>);
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: writing `&Vec` instead of `&[_]` involves a new object where a slice will
|
2015-10-30 23:48:05 +00:00
|
|
|
fn do_item(x: &Self::Item);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Bar;
|
|
|
|
|
|
|
|
// no error, in trait impl (#425)
|
|
|
|
impl Foo for Bar {
|
|
|
|
type Item = Vec<u8>;
|
|
|
|
fn do_vec(x: &Vec<i64>) {}
|
2017-09-16 07:10:26 +00:00
|
|
|
fn do_item(x: &Vec<u8>) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cloned(x: &Vec<u8>) -> Vec<u8> {
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: writing `&Vec` instead of `&[_]` involves a new object where a slice will do
|
2017-09-16 07:10:26 +00:00
|
|
|
let e = x.clone();
|
|
|
|
let f = e.clone(); // OK
|
|
|
|
let g = x;
|
2022-01-27 14:12:45 +00:00
|
|
|
let h = g.clone();
|
2017-09-16 07:10:26 +00:00
|
|
|
let i = (e).clone();
|
|
|
|
x.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn str_cloned(x: &String) -> String {
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: writing `&String` instead of `&str` involves a new object where a slice will d
|
2017-09-16 07:10:26 +00:00
|
|
|
let a = x.clone();
|
|
|
|
let b = x.clone();
|
|
|
|
let c = b.clone();
|
2018-12-09 22:26:16 +00:00
|
|
|
let d = a.clone().clone().clone();
|
2017-09-16 07:10:26 +00:00
|
|
|
x.clone()
|
2015-10-30 23:48:05 +00:00
|
|
|
}
|
2017-09-20 21:59:23 +00:00
|
|
|
|
2021-01-02 15:29:43 +00:00
|
|
|
fn path_cloned(x: &PathBuf) -> PathBuf {
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: writing `&PathBuf` instead of `&Path` involves a new object where a slice will
|
2021-01-02 15:29:43 +00:00
|
|
|
let a = x.clone();
|
|
|
|
let b = x.clone();
|
|
|
|
let c = b.clone();
|
|
|
|
let d = a.clone().clone().clone();
|
|
|
|
x.clone()
|
|
|
|
}
|
|
|
|
|
2017-09-20 21:59:23 +00:00
|
|
|
fn false_positive_capacity(x: &Vec<u8>, y: &String) {
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: writing `&String` instead of `&str` involves a new object where a slice will d
|
2017-09-20 21:59:23 +00:00
|
|
|
let a = x.capacity();
|
|
|
|
let b = y.clone();
|
|
|
|
let c = y.as_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn false_positive_capacity_too(x: &String) -> String {
|
2018-12-09 22:26:16 +00:00
|
|
|
if x.capacity() > 1024 {
|
|
|
|
panic!("Too large!");
|
|
|
|
}
|
2017-09-20 21:59:23 +00:00
|
|
|
x.clone()
|
|
|
|
}
|
|
|
|
|
2018-04-05 15:59:35 +00:00
|
|
|
#[allow(dead_code)]
|
2018-12-09 22:26:16 +00:00
|
|
|
fn test_cow_with_ref(c: &Cow<[i32]>) {}
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: using a reference to `Cow` is not recommended
|
2018-04-05 15:59:35 +00:00
|
|
|
|
|
|
|
fn test_cow(c: Cow<[i32]>) {
|
|
|
|
let _c = c;
|
|
|
|
}
|
2018-09-05 12:59:07 +00:00
|
|
|
|
|
|
|
trait Foo2 {
|
|
|
|
fn do_string(&self);
|
|
|
|
}
|
|
|
|
|
|
|
|
// no error for &self references where self is of type String (#2293)
|
2018-12-09 22:26:16 +00:00
|
|
|
impl Foo2 for String {
|
|
|
|
fn do_string(&self) {}
|
|
|
|
}
|
2020-05-28 13:45:24 +00:00
|
|
|
|
|
|
|
// Check that the allow attribute on parameters is honored
|
|
|
|
mod issue_5644 {
|
|
|
|
use std::borrow::Cow;
|
2021-01-02 15:29:43 +00:00
|
|
|
use std::path::PathBuf;
|
2020-05-28 13:45:24 +00:00
|
|
|
|
|
|
|
fn allowed(
|
|
|
|
#[allow(clippy::ptr_arg)] _v: &Vec<u32>,
|
|
|
|
#[allow(clippy::ptr_arg)] _s: &String,
|
2021-01-02 15:29:43 +00:00
|
|
|
#[allow(clippy::ptr_arg)] _p: &PathBuf,
|
2020-05-28 13:45:24 +00:00
|
|
|
#[allow(clippy::ptr_arg)] _c: &Cow<[i32]>,
|
2022-06-30 08:50:09 +00:00
|
|
|
#[expect(clippy::ptr_arg)] _expect: &Cow<[i32]>,
|
2020-05-28 13:45:24 +00:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-06-30 08:50:09 +00:00
|
|
|
fn some_allowed(#[allow(clippy::ptr_arg)] _v: &Vec<u32>, _s: &String) {}
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: writing `&String` instead of `&str` involves a new object where a slice wi
|
2022-06-30 08:50:09 +00:00
|
|
|
|
2022-04-07 17:39:59 +00:00
|
|
|
struct S;
|
2020-05-28 13:45:24 +00:00
|
|
|
impl S {
|
|
|
|
fn allowed(
|
|
|
|
#[allow(clippy::ptr_arg)] _v: &Vec<u32>,
|
|
|
|
#[allow(clippy::ptr_arg)] _s: &String,
|
2021-01-02 15:29:43 +00:00
|
|
|
#[allow(clippy::ptr_arg)] _p: &PathBuf,
|
2020-05-28 13:45:24 +00:00
|
|
|
#[allow(clippy::ptr_arg)] _c: &Cow<[i32]>,
|
2022-06-30 08:50:09 +00:00
|
|
|
#[expect(clippy::ptr_arg)] _expect: &Cow<[i32]>,
|
2020-05-28 13:45:24 +00:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
trait T {
|
|
|
|
fn allowed(
|
|
|
|
#[allow(clippy::ptr_arg)] _v: &Vec<u32>,
|
|
|
|
#[allow(clippy::ptr_arg)] _s: &String,
|
2021-01-02 15:29:43 +00:00
|
|
|
#[allow(clippy::ptr_arg)] _p: &PathBuf,
|
2020-05-28 13:45:24 +00:00
|
|
|
#[allow(clippy::ptr_arg)] _c: &Cow<[i32]>,
|
2022-06-30 08:50:09 +00:00
|
|
|
#[expect(clippy::ptr_arg)] _expect: &Cow<[i32]>,
|
2020-05-28 13:45:24 +00:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-02 15:29:43 +00:00
|
|
|
|
|
|
|
mod issue6509 {
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
fn foo_vec(vec: &Vec<u8>) {
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: writing `&Vec` instead of `&[_]` involves a new object where a slice will
|
2021-01-02 15:29:43 +00:00
|
|
|
let _ = vec.clone().pop();
|
|
|
|
let _ = vec.clone().clone();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo_path(path: &PathBuf) {
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: writing `&PathBuf` instead of `&Path` involves a new object where a slice
|
2021-01-02 15:29:43 +00:00
|
|
|
let _ = path.clone().pop();
|
|
|
|
let _ = path.clone().clone();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo_str(str: &PathBuf) {
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: writing `&PathBuf` instead of `&Path` involves a new object where a slice
|
2021-01-02 15:29:43 +00:00
|
|
|
let _ = str.clone().pop();
|
|
|
|
let _ = str.clone().clone();
|
|
|
|
}
|
|
|
|
}
|
2021-11-04 12:52:36 +00:00
|
|
|
|
2022-01-27 14:12:45 +00:00
|
|
|
fn mut_vec_slice_methods(v: &mut Vec<u32>) {
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: writing `&mut Vec` instead of `&mut [_]` involves a new object where a slice w
|
2022-01-27 14:12:45 +00:00
|
|
|
v.copy_within(1..5, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mut_vec_vec_methods(v: &mut Vec<u32>) {
|
|
|
|
v.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn vec_contains(v: &Vec<u32>) -> bool {
|
|
|
|
[vec![], vec![0]].as_slice().contains(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fn_requires_vec(v: &Vec<u32>) -> bool {
|
|
|
|
vec_contains(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn impl_fn_requires_vec(v: &Vec<u32>, f: impl Fn(&Vec<u32>)) {
|
|
|
|
f(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dyn_fn_requires_vec(v: &Vec<u32>, f: &dyn Fn(&Vec<u32>)) {
|
|
|
|
f(v);
|
|
|
|
}
|
|
|
|
|
2021-11-04 12:52:36 +00:00
|
|
|
// No error for types behind an alias (#7699)
|
|
|
|
type A = Vec<u8>;
|
|
|
|
fn aliased(a: &A) {}
|
2022-02-10 17:40:06 +00:00
|
|
|
|
|
|
|
// Issue #8366
|
|
|
|
pub trait Trait {
|
|
|
|
fn f(v: &mut Vec<i32>);
|
|
|
|
fn f2(v: &mut Vec<i32>) {}
|
|
|
|
}
|
2022-02-26 13:26:21 +00:00
|
|
|
|
|
|
|
// Issue #8463
|
|
|
|
fn two_vecs(a: &mut Vec<u32>, b: &mut Vec<u32>) {
|
|
|
|
a.push(0);
|
|
|
|
a.push(0);
|
|
|
|
a.push(0);
|
|
|
|
b.push(1);
|
|
|
|
}
|
2022-03-24 13:50:04 +00:00
|
|
|
|
|
|
|
// Issue #8495
|
|
|
|
fn cow_conditional_to_mut(a: &mut Cow<str>) {
|
|
|
|
if a.is_empty() {
|
|
|
|
a.to_mut().push_str("foo");
|
|
|
|
}
|
|
|
|
}
|
2022-10-23 13:18:45 +00:00
|
|
|
|
|
|
|
// Issue #9542
|
|
|
|
fn dyn_trait_ok(a: &mut Vec<u32>, b: &mut String, c: &mut PathBuf) {
|
|
|
|
trait T {}
|
|
|
|
impl<U> T for Vec<U> {}
|
|
|
|
impl T for String {}
|
|
|
|
impl T for PathBuf {}
|
|
|
|
fn takes_dyn(_: &mut dyn T) {}
|
|
|
|
|
|
|
|
takes_dyn(a);
|
|
|
|
takes_dyn(b);
|
|
|
|
takes_dyn(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dyn_trait(a: &mut Vec<u32>, b: &mut String, c: &mut PathBuf) {
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: writing `&mut Vec` instead of `&mut [_]` involves a new object where a slice w
|
|
|
|
//~| ERROR: writing `&mut String` instead of `&mut str` involves a new object where a slic
|
|
|
|
//~| ERROR: writing `&mut PathBuf` instead of `&mut Path` involves a new object where a sl
|
2022-10-23 13:18:45 +00:00
|
|
|
trait T {}
|
|
|
|
impl<U> T for Vec<U> {}
|
|
|
|
impl<U> T for [U] {}
|
|
|
|
impl T for String {}
|
|
|
|
impl T for str {}
|
|
|
|
impl T for PathBuf {}
|
|
|
|
impl T for Path {}
|
|
|
|
fn takes_dyn(_: &mut dyn T) {}
|
|
|
|
|
|
|
|
takes_dyn(a);
|
|
|
|
takes_dyn(b);
|
|
|
|
takes_dyn(c);
|
|
|
|
}
|
2023-07-02 12:35:19 +00:00
|
|
|
|
|
|
|
mod issue_9218 {
|
|
|
|
use std::borrow::Cow;
|
|
|
|
|
|
|
|
fn cow_non_elided_lifetime<'a>(input: &Cow<'a, str>) -> &'a str {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
|
|
|
// This one has an anonymous lifetime so it's not okay
|
|
|
|
fn cow_elided_lifetime<'a>(input: &'a Cow<str>) -> &'a str {
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: using a reference to `Cow` is not recommended
|
2023-07-02 12:35:19 +00:00
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
2024-05-18 16:08:11 +00:00
|
|
|
// These two's return types don't use 'a so it's not okay
|
2023-07-02 12:35:19 +00:00
|
|
|
fn cow_bad_ret_ty_1<'a>(input: &'a Cow<'a, str>) -> &'static str {
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: using a reference to `Cow` is not recommended
|
2023-07-02 12:35:19 +00:00
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
fn cow_bad_ret_ty_2<'a, 'b>(input: &'a Cow<'a, str>) -> &'b str {
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: using a reference to `Cow` is not recommended
|
2023-07-02 12:35:19 +00:00
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inferred to be `&'a str`, afaik.
|
|
|
|
fn cow_good_ret_ty<'a>(input: &'a Cow<'a, str>) -> &str {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
2023-07-31 21:53:53 +00:00
|
|
|
|
|
|
|
mod issue_11181 {
|
|
|
|
extern "C" fn allowed(_v: &Vec<u32>) {}
|
|
|
|
|
|
|
|
struct S;
|
|
|
|
impl S {
|
|
|
|
extern "C" fn allowed(_v: &Vec<u32>) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
trait T {
|
|
|
|
extern "C" fn allowed(_v: &Vec<u32>) {}
|
|
|
|
}
|
|
|
|
}
|
2024-08-26 17:24:13 +00:00
|
|
|
|
|
|
|
mod issue_13308 {
|
|
|
|
use std::ops::Deref;
|
|
|
|
|
|
|
|
fn repro(source: &str, destination: &mut String) {
|
|
|
|
source.clone_into(destination);
|
|
|
|
}
|
|
|
|
fn repro2(source: &str, destination: &mut String) {
|
|
|
|
ToOwned::clone_into(source, destination);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn h1(_: &<String as Deref>::Target) {}
|
|
|
|
fn h2<T: Deref>(_: T, _: &T::Target) {}
|
|
|
|
|
|
|
|
// Other cases that are still ok to lint and ideally shouldn't regress
|
|
|
|
fn good(v1: &String, v2: &String) {
|
|
|
|
//~^ ERROR: writing `&String` instead of `&str`
|
|
|
|
//~^^ ERROR: writing `&String` instead of `&str`
|
|
|
|
h1(v1);
|
|
|
|
h2(String::new(), v2);
|
|
|
|
}
|
|
|
|
}
|