2 // aux-build:proc_macro_derive.rs
4 #![warn(clippy::use_self)]
7 clippy::should_implement_trait,
8 clippy::upper_case_acronyms,
9 clippy::from_over_into,
10 clippy::self_named_constructors
14 extern crate proc_macro_derive;
30 impl Default for Foo {
31 fn default() -> Self {
49 impl Default for Foo {
50 fn default() -> Self {
62 // Cannot use `Self` as return type, because the function is actually `fn foo<'b>(s: &'b str) ->
64 fn foo(s: &str) -> Foo {
67 // cannot replace with `Self`, because that's `Foo<'a>`
68 fn bar() -> Foo<'static> {
69 Foo { foo_str: "foo" }
72 // FIXME: the lint does not handle lifetimed struct
73 // `Self` should be applicable here
74 fn clone(&self) -> Foo<'a> {
75 Foo { foo_str: self.foo_str }
82 fn to_bytes(self) -> Vec<u8>;
85 // This should not be linted
86 impl IntoBytes for u8 {
87 fn to_bytes(self) -> Vec<u8> {
97 fn bad(foos: &[Self]) -> impl Iterator<Item = &Self> {
101 fn good(foos: &[Self]) -> impl Iterator<Item = &Self> {
111 pub fn ts() -> Self {
118 macro_rules! use_self_expand {
129 use_self_expand!(); // Should not lint in local macros
132 #[derive(StructAUseSelf)] // Should not lint in derives
140 #[allow(unused_imports)]
141 use self::Foo; // Can't use Self here
143 foo: Foo, // Foo != Self
152 // Can't use Self here
171 #[allow(unused_imports)]
172 use self::Enum::*; // Issue 3425
173 static STATIC: Enum = Enum::A; // Can't use Self as type
178 let _ = Self::C { field: true };
193 impl Trait<Vec<A>> for Vec<B> {
194 fn a(_: Vec<A>) -> Self {
199 impl<T> Trait<Vec<A>> for Vec<T>
203 fn a(v: Vec<A>) -> Self {
204 <Vec<B>>::a(v).into_iter().map(Trait::a).collect()
209 #[allow(clippy::no_effect, path_statements)]
216 const A: bool = true;
232 fn from_something() -> Self {
238 fn test() -> TestStruct;
241 impl Test for TestStruct {
242 fn test() -> TestStruct {
243 Self::from_something()
248 mod paths_created_by_lowering {
257 async fn g() -> Self {
261 fn f<'a>(&self, p: &'a [u8]) -> &'a [u8] {
267 fn f<'a>(&self, p: &'a [u8]) -> &'a [u8];
270 impl T for Range<u8> {
271 fn f<'a>(&self, p: &'a [u8]) -> &'a [u8] {
284 // `Self` is applicable here
285 fn foo(value: T) -> Self {
289 // `Cannot` use `Self` as a return type as the generic types are different
290 fn bar(value: i32) -> Foo<i32> {
297 pub struct Error<From, To> {
306 fn from(value: T) -> Self;
316 fn try_from(value: T) -> Result<Self, Error<Self::From, Self::To>>;
319 // FIXME: Suggested fix results in infinite recursion.
320 // impl<F, T> TryFrom<F> for T
324 // type From = Self::From;
325 // type To = Self::To;
327 // fn try_from(value: F) -> Result<Self, Error<Self::From, Self::To>> {
328 // Ok(From::from(value))
332 impl From<bool> for i64 {
336 fn from(value: bool) -> Self {
337 if value { 100 } else { 0 }
351 impl<T: Foo> Foo for Option<T> {
352 type Bar = Option<T::Bar>;
358 pub struct Bar([usize; 3]);
361 pub const BAR: usize = 3;
364 const _X: usize = Foo::BAR;
365 // const _Y: usize = Self::BAR;
371 trait Foo: 'static {}
377 impl<T: Foo> From<T> for Box<dyn Foo> {
378 fn from(t: T) -> Self {
384 mod lint_at_item_level {
387 #[allow(clippy::use_self)]
394 #[allow(clippy::use_self)]
395 impl Default for Foo {
396 fn default() -> Foo {
402 mod lint_at_impl_item_level {
406 #[allow(clippy::use_self)]
412 impl Default for Foo {
413 #[allow(clippy::use_self)]
414 fn default() -> Foo {
426 impl From<X> for u32 {
427 fn from(c: X) -> Self {
428 unsafe { core::mem::transmute(c) }
434 use std::convert::Into;
451 fn new<V: Into<T>>(v: V) -> Self {
452 Self { t: Into::into(v) }
458 Self::new::<submod::B>(submod::B {})
464 #[derive(serde::Deserialize)]
471 struct MyStruct<const C: char>;
472 impl From<MyStruct<'a'>> for MyStruct<'b'> {
473 fn from(_s: MyStruct<'a'>) -> Self {
478 // keep linting non-`Const` generic args
494 fn new_again() -> Self {
500 mod self_is_ty_param {
511 I::Item: Trait, // changing this to Self would require <Self as Iterator>
518 let _: I; // this could lint, but is questionable