1 //@aux-build:proc_macro_derive.rs
3 #![warn(clippy::use_self)]
4 #![allow(dead_code, unreachable_code)]
6 clippy
::should_implement_trait
,
7 clippy
::upper_case_acronyms
,
8 clippy
::from_over_into
,
9 clippy
::self_named_constructors
13 extern crate proc_macro_derive
;
29 impl Default
for Foo
{
48 impl Default
for Foo
{
49 fn default() -> Self {
61 // Cannot use `Self` as return type, because the function is actually `fn foo<'b>(s: &'b str) ->
63 fn foo(s
: &str) -> Foo
{
66 // cannot replace with `Self`, because that's `Foo<'a>`
67 fn bar() -> Foo
<'
static> {
68 Foo { foo_str: "foo" }
71 // FIXME: the lint does not handle lifetimed struct
72 // `Self` should be applicable here
73 fn clone(&self) -> Foo
<'a
> {
74 Foo { foo_str: self.foo_str }
81 fn to_bytes(self) -> Vec
<u8>;
84 // This should not be linted
85 impl IntoBytes
for u8 {
86 fn to_bytes(self) -> Vec
<u8> {
96 fn bad(foos
: &[Foo
]) -> impl Iterator
<Item
= &Foo
> {
100 fn good(foos
: &[Self]) -> impl Iterator
<Item
= &Self> {
110 pub fn ts() -> Self {
117 macro_rules
! use_self_expand
{
128 use_self_expand
!(); // Should not lint in local macros
131 #[derive(StructAUseSelf)] // Should not lint in derives
139 #[allow(unused_imports)]
140 use self::Foo
; // Can't use Self here
142 foo
: Foo
, // Foo != Self
151 // Can't use Self here
170 #[allow(unused_imports)]
171 use self::Enum
::*; // Issue 3425
172 static STATIC
: Enum
= Enum
::A
; // Can't use Self as type
177 let _
= Enum
::C { field: true }
;
192 impl Trait
<Vec
<A
>> for Vec
<B
> {
193 fn a(_
: Vec
<A
>) -> Self {
198 impl<T
> Trait
<Vec
<A
>> for Vec
<T
>
202 fn a(v
: Vec
<A
>) -> Self {
203 <Vec
<B
>>::a(v
).into_iter().map(Trait
::a
).collect()
208 #[allow(clippy::no_effect, path_statements)]
215 const A
: bool
= true;
231 fn from_something() -> Self {
237 fn test() -> TestStruct
;
240 impl Test
for TestStruct
{
241 fn test() -> TestStruct
{
242 TestStruct
::from_something()
247 mod paths_created_by_lowering
{
260 fn f
<'a
>(&self, p
: &'a
[u8]) -> &'a
[u8] {
266 fn f
<'a
>(&self, p
: &'a
[u8]) -> &'a
[u8];
269 impl T
for Range
<u8> {
270 fn f
<'a
>(&self, p
: &'a
[u8]) -> &'a
[u8] {
283 // `Self` is applicable here
284 fn foo(value
: T
) -> Foo
<T
> {
288 // `Cannot` use `Self` as a return type as the generic types are different
289 fn bar(value
: i32) -> Foo
<i32> {
296 pub struct Error
<From
, To
> {
305 fn from(value
: T
) -> Self;
315 fn try_from(value
: T
) -> Result
<Self, Error
<Self::From
, Self::To
>>;
318 // FIXME: Suggested fix results in infinite recursion.
319 // impl<F, T> TryFrom<F> for T
323 // type From = Self::From;
324 // type To = Self::To;
326 // fn try_from(value: F) -> Result<Self, Error<Self::From, Self::To>> {
327 // Ok(From::from(value))
331 impl From
<bool
> for i64 {
335 fn from(value
: bool
) -> Self {
336 if value { 100 }
else { 0 }
350 impl<T
: Foo
> Foo
for Option
<T
> {
351 type Bar
= Option
<T
::Bar
>;
357 pub struct Bar([usize; 3]);
360 pub const BAR
: usize = 3;
363 const _X
: usize = Foo
::BAR
;
364 // const _Y: usize = Self::BAR;
370 trait Foo
: '
static {}
376 impl<T
: Foo
> From
<T
> for Box
<dyn Foo
> {
377 fn from(t
: T
) -> Self {
383 mod lint_at_item_level
{
386 #[allow(clippy::use_self)]
393 #[allow(clippy::use_self)]
394 impl Default
for Foo
{
395 fn default() -> Foo
{
401 mod lint_at_impl_item_level
{
405 #[allow(clippy::use_self)]
411 impl Default
for Foo
{
412 #[allow(clippy::use_self)]
413 fn default() -> Foo
{
425 impl From
<X
> for u32 {
426 fn from(c
: X
) -> Self {
427 unsafe { core::mem::transmute(c) }
433 use std
::convert
::Into
;
450 fn new
<V
: Into
<T
>>(v
: V
) -> Self {
451 Self { t: Into::into(v) }
457 A
::new
::<submod
::B
>(submod
::B {}
)
463 #[derive(serde::Deserialize)]
470 struct MyStruct
<const C
: char>;
471 impl From
<MyStruct
<'a'
>> for MyStruct
<'b'
> {
472 fn from(_s
: MyStruct
<'a'
>) -> Self {
477 // keep linting non-`Const` generic args
493 fn new_again() -> Self {
499 mod self_is_ty_param
{
510 I
::Item
: Trait
, // changing this to Self would require <Self as Iterator>
517 let _
: I
; // this could lint, but is questionable
522 mod use_self_in_pat
{
531 Foo
::Bar
=> unimplemented
!(),
532 Foo
::Baz
=> unimplemented
!(),
535 Some(_
) => unimplemented
!(),
536 None
=> unimplemented
!(),
538 if let Foo
::Bar
= self {
549 StructNums { one: u8, two: u8 }
,
560 fn get_value(&self) -> u8 {
562 Something
::Num(n
) => *n
,
563 Something
::TupleNums(n
, _m
) => *n
,
564 Something
::StructNums { one, two: _ }
=> *one
,
568 fn use_crate(&self) -> u8 {
570 crate::issue8845
::Something
::Num(n
) => *n
,
571 crate::issue8845
::Something
::TupleNums(n
, _m
) => *n
,
572 crate::issue8845
::Something
::StructNums { one, two: _ }
=> *one
,
576 fn imported_values(&self) -> u8 {
580 TupleNums(n
, _m
) => *n
,
581 StructNums { one, two: _ }
=> *one
,
587 fn get_value(&self) -> u8 {
592 fn use_crate(&self) -> u8 {
593 let crate::issue8845
::Foo(x
) = self;
599 fn get_value(&self) -> u8 {
600 let Bar { x, .. }
= self;
604 fn use_crate(&self) -> u8 {
605 let crate::issue8845
::Bar { x, .. }
= self;
612 use serde
::Serialize
;
620 #[clippy::msrv = "1.36"]
635 #[clippy::msrv = "1.37"]
651 struct Val
<const V
: i32> {}
653 impl<const V
: i32> From
<Val
<V
>> for i32 {
654 fn from(_
: Val
<V
>) -> Self {