rust/tests/ui/trivially_copy_pass_by_ref.rs

115 lines
2.2 KiB
Rust
Raw Normal View History

// normalize-stderr-test "\(\d+ byte\)" -> "(N byte)"
// normalize-stderr-test "\(limit: \d+ byte\)" -> "(limit: N byte)"
#![deny(clippy::trivially_copy_pass_by_ref)]
2018-12-09 16:26:16 -06:00
#![allow(
clippy::many_single_char_names,
clippy::blacklisted_name,
clippy::redundant_field_names
)]
#[derive(Copy, Clone)]
struct Foo(u32);
#[derive(Copy, Clone)]
struct Bar([u8; 24]);
#[derive(Copy, Clone)]
pub struct Color {
2018-12-09 16:26:16 -06:00
pub r: u8,
pub g: u8,
pub b: u8,
pub a: u8,
}
struct FooRef<'a> {
foo: &'a Foo,
}
type Baz = u32;
2018-12-09 16:26:16 -06:00
fn good(a: &mut u32, b: u32, c: &Bar) {}
fn good_return_implicit_lt_ref(foo: &Foo) -> &u32 {
&foo.0
}
2018-07-28 10:34:52 -05:00
#[allow(clippy::needless_lifetimes)]
fn good_return_explicit_lt_ref<'a>(foo: &'a Foo) -> &'a u32 {
&foo.0
}
fn good_return_implicit_lt_struct(foo: &Foo) -> FooRef {
2018-12-09 16:26:16 -06:00
FooRef { foo }
}
2018-07-28 10:34:52 -05:00
#[allow(clippy::needless_lifetimes)]
fn good_return_explicit_lt_struct<'a>(foo: &'a Foo) -> FooRef<'a> {
2018-12-09 16:26:16 -06:00
FooRef { foo }
}
2018-12-09 16:26:16 -06:00
fn bad(x: &u32, y: &Foo, z: &Baz) {}
impl Foo {
2018-12-09 16:26:16 -06:00
fn good(self, a: &mut u32, b: u32, c: &Bar) {}
2018-12-09 16:26:16 -06:00
fn good2(&mut self) {}
2018-12-09 16:26:16 -06:00
fn bad(&self, x: &u32, y: &Foo, z: &Baz) {}
2018-12-09 16:26:16 -06:00
fn bad2(x: &u32, y: &Foo, z: &Baz) {}
}
impl AsRef<u32> for Foo {
fn as_ref(&self) -> &u32 {
&self.0
}
}
impl Bar {
2018-12-09 16:26:16 -06:00
fn good(&self, a: &mut u32, b: u32, c: &Bar) {}
2018-12-09 16:26:16 -06:00
fn bad2(x: &u32, y: &Foo, z: &Baz) {}
}
trait MyTrait {
fn trait_method(&self, _foo: &Foo);
}
pub trait MyTrait2 {
fn trait_method2(&self, _color: &Color);
}
impl MyTrait for Foo {
fn trait_method(&self, _foo: &Foo) {
unimplemented!()
}
}
#[allow(unused_variables)]
mod issue3992 {
pub trait A {
#[allow(clippy::trivially_copy_pass_by_ref)]
fn a(b: &u16) {}
}
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn c(d: &u16) {}
}
fn main() {
let (mut foo, bar) = (Foo(0), Bar([0; 24]));
let (mut a, b, c, x, y, z) = (0, 0, Bar([0; 24]), 0, Foo(0), 0);
good(&mut a, b, &c);
good_return_implicit_lt_ref(&y);
good_return_explicit_lt_ref(&y);
bad(&x, &y, &z);
foo.good(&mut a, b, &c);
foo.good2();
foo.bad(&x, &y, &z);
Foo::bad2(&x, &y, &z);
bar.good(&mut a, b, &c);
Bar::bad2(&x, &y, &z);
foo.as_ref();
}