X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=tests%2Fui%2Fuse_self.rs;h=609625abdec0a70d06c9ef8a50432f57c2e3c108;hb=ada8c72f3f21013f789f774d4c0219c58264e663;hp=e6900b915341e6a7adc8302a1fffdf43af637b02;hpb=cfcd53dd4c222eba2697cd7843f38066a69fca44;p=rust.git diff --git a/tests/ui/use_self.rs b/tests/ui/use_self.rs index e6900b91534..609625abdec 100644 --- a/tests/ui/use_self.rs +++ b/tests/ui/use_self.rs @@ -1,8 +1,13 @@ // run-rustfix +// edition:2018 +// aux-build:proc_macro_derive.rs #![warn(clippy::use_self)] #![allow(dead_code)] -#![allow(clippy::should_implement_trait)] +#![allow(clippy::should_implement_trait, clippy::upper_case_acronyms, clippy::from_over_into)] + +#[macro_use] +extern crate proc_macro_derive; fn main() {} @@ -68,125 +73,14 @@ fn clone(&self) -> Foo<'a> { } } -#[allow(clippy::boxed_local)] -mod traits { - - use std::ops::Mul; - - trait SelfTrait { - fn refs(p1: &Self) -> &Self; - fn ref_refs<'a>(p1: &'a &'a Self) -> &'a &'a Self; - fn mut_refs(p1: &mut Self) -> &mut Self; - fn nested(p1: Box, p2: (&u8, &Self)); - fn vals(r: Self) -> Self; - } - - #[derive(Default)] - struct Bad; - - impl SelfTrait for Bad { - fn refs(p1: &Bad) -> &Bad { - p1 - } - - fn ref_refs<'a>(p1: &'a &'a Bad) -> &'a &'a Bad { - p1 - } - - fn mut_refs(p1: &mut Bad) -> &mut Bad { - p1 - } - - fn nested(_p1: Box, _p2: (&u8, &Bad)) {} - - fn vals(_: Bad) -> Bad { - Bad::default() - } - } - - impl Mul for Bad { - type Output = Bad; - - fn mul(self, rhs: Bad) -> Bad { - rhs - } - } - - impl Clone for Bad { - fn clone(&self) -> Self { - Bad - } - } - - #[derive(Default)] - struct Good; - - impl SelfTrait for Good { - fn refs(p1: &Self) -> &Self { - p1 - } - - fn ref_refs<'a>(p1: &'a &'a Self) -> &'a &'a Self { - p1 - } - - fn mut_refs(p1: &mut Self) -> &mut Self { - p1 - } - - fn nested(_p1: Box, _p2: (&u8, &Self)) {} - - fn vals(_: Self) -> Self { - Self::default() - } - } - - impl Mul for Good { - type Output = Self; - - fn mul(self, rhs: Self) -> Self { - rhs - } - } - - trait NameTrait { - fn refs(p1: &u8) -> &u8; - fn ref_refs<'a>(p1: &'a &'a u8) -> &'a &'a u8; - fn mut_refs(p1: &mut u8) -> &mut u8; - fn nested(p1: Box, p2: (&u8, &u8)); - fn vals(p1: u8) -> u8; - } - - // Using `Self` instead of the type name is OK - impl NameTrait for u8 { - fn refs(p1: &Self) -> &Self { - p1 - } - - fn ref_refs<'a>(p1: &'a &'a Self) -> &'a &'a Self { - p1 - } - - fn mut_refs(p1: &mut Self) -> &mut Self { - p1 - } - - fn nested(_p1: Box, _p2: (&Self, &Self)) {} - - fn vals(_: Self) -> Self { - Self::default() - } - } -} - mod issue2894 { trait IntoBytes { - fn into_bytes(&self) -> Vec; + fn to_bytes(&self) -> Vec; } // This should not be linted impl IntoBytes for u8 { - fn into_bytes(&self) -> Vec { + fn to_bytes(&self) -> Vec { vec![*self] } } @@ -196,7 +90,7 @@ mod existential { struct Foo; impl Foo { - fn bad(foos: &[Self]) -> impl Iterator { + fn bad(foos: &[Foo]) -> impl Iterator { foos.iter() } @@ -228,8 +122,11 @@ fn new() -> Foo { struct Foo {} impl Foo { - use_self_expand!(); // Should lint in local macros + use_self_expand!(); // Should not lint in local macros } + + #[derive(StructAUseSelf)] // Should not lint in derives + struct A; } mod nesting { @@ -286,11 +183,22 @@ mod issue3410 { struct B; trait Trait { - fn a(v: T); + fn a(v: T) -> Self; } impl Trait> for Vec { - fn a(_: Vec) {} + fn a(_: Vec) -> Self { + unimplemented!() + } + } + + impl Trait> for Vec + where + T: Trait, + { + fn a(v: Vec) -> Self { + >::a(v).into_iter().map(Trait::a).collect() + } } } @@ -332,3 +240,217 @@ fn test() -> TestStruct { } } } + +mod paths_created_by_lowering { + use std::ops::Range; + + struct S {} + + impl S { + const A: usize = 0; + const B: usize = 1; + + async fn g() -> S { + S {} + } + + fn f<'a>(&self, p: &'a [u8]) -> &'a [u8] { + &p[S::A..S::B] + } + } + + trait T { + fn f<'a>(&self, p: &'a [u8]) -> &'a [u8]; + } + + impl T for Range { + fn f<'a>(&self, p: &'a [u8]) -> &'a [u8] { + &p[0..1] + } + } +} + +// reused from #1997 +mod generics { + struct Foo { + value: T, + } + + impl Foo { + // `Self` is applicable here + fn foo(value: T) -> Foo { + Foo { value } + } + + // `Cannot` use `Self` as a return type as the generic types are different + fn bar(value: i32) -> Foo { + Foo { value } + } + } +} + +mod issue4140 { + pub struct Error { + _from: From, + _too: To, + } + + pub trait From { + type From; + type To; + + fn from(value: T) -> Self; + } + + pub trait TryFrom + where + Self: Sized, + { + type From; + type To; + + fn try_from(value: T) -> Result>; + } + + impl TryFrom for T + where + T: From, + { + type From = T::From; + type To = T::To; + + fn try_from(value: F) -> Result> { + Ok(From::from(value)) + } + } + + impl From for i64 { + type From = bool; + type To = Self; + + fn from(value: bool) -> Self { + if value { 100 } else { 0 } + } + } +} + +mod issue2843 { + trait Foo { + type Bar; + } + + impl Foo for usize { + type Bar = u8; + } + + impl Foo for Option { + type Bar = Option; + } +} + +mod issue3859 { + pub struct Foo; + pub struct Bar([usize; 3]); + + impl Foo { + pub const BAR: usize = 3; + + pub fn foo() { + const _X: usize = Foo::BAR; + // const _Y: usize = Self::BAR; + } + } +} + +mod issue4305 { + trait Foo: 'static {} + + struct Bar; + + impl Foo for Bar {} + + impl From for Box { + fn from(t: T) -> Self { + Box::new(t) + } + } +} + +mod lint_at_item_level { + struct Foo {} + + #[allow(clippy::use_self)] + impl Foo { + fn new() -> Foo { + Foo {} + } + } + + #[allow(clippy::use_self)] + impl Default for Foo { + fn default() -> Foo { + Foo::new() + } + } +} + +mod lint_at_impl_item_level { + struct Foo {} + + impl Foo { + #[allow(clippy::use_self)] + fn new() -> Foo { + Foo {} + } + } + + impl Default for Foo { + #[allow(clippy::use_self)] + fn default() -> Foo { + Foo::new() + } + } +} + +mod issue4734 { + #[repr(C, packed)] + pub struct X { + pub x: u32, + } + + impl From for u32 { + fn from(c: X) -> Self { + unsafe { core::mem::transmute(c) } + } + } +} + +mod nested_paths { + use std::convert::Into; + mod submod { + pub struct B {} + pub struct C {} + + impl Into for B { + fn into(self) -> C { + C {} + } + } + } + + struct A { + t: T, + } + + impl A { + fn new>(v: V) -> Self { + Self { t: Into::into(v) } + } + } + + impl A { + fn test() -> Self { + A::new::(submod::B {}) + } + } +}