3 // aux-build:proc_macro_derive.rs
5 #![warn(clippy::use_self)]
8 clippy
::should_implement_trait
,
9 clippy
::upper_case_acronyms
,
10 clippy
::from_over_into
,
11 clippy
::self_named_constructors
15 extern crate proc_macro_derive
;
31 impl Default
for Foo
{
50 impl Default
for Foo
{
51 fn default() -> Self {
63 // Cannot use `Self` as return type, because the function is actually `fn foo<'b>(s: &'b str) ->
65 fn foo(s
: &str) -> Foo
{
68 // cannot replace with `Self`, because that's `Foo<'a>`
69 fn bar() -> Foo
<'
static> {
70 Foo { foo_str: "foo" }
73 // FIXME: the lint does not handle lifetimed struct
74 // `Self` should be applicable here
75 fn clone(&self) -> Foo
<'a
> {
76 Foo { foo_str: self.foo_str }
83 fn to_bytes(self) -> Vec
<u8>;
86 // This should not be linted
87 impl IntoBytes
for u8 {
88 fn to_bytes(self) -> Vec
<u8> {
98 fn bad(foos
: &[Foo
]) -> impl Iterator
<Item
= &Foo
> {
102 fn good(foos
: &[Self]) -> impl Iterator
<Item
= &Self> {
112 pub fn ts() -> Self {
119 macro_rules
! use_self_expand
{
130 use_self_expand
!(); // Should not lint in local macros
133 #[derive(StructAUseSelf)] // Should not lint in derives
141 #[allow(unused_imports)]
142 use self::Foo
; // Can't use Self here
144 foo
: Foo
, // Foo != Self
153 // Can't use Self here
172 #[allow(unused_imports)]
173 use self::Enum
::*; // Issue 3425
174 static STATIC
: Enum
= Enum
::A
; // Can't use Self as type
179 let _
= Enum
::C { field: true }
;
194 impl Trait
<Vec
<A
>> for Vec
<B
> {
195 fn a(_
: Vec
<A
>) -> Self {
200 impl<T
> Trait
<Vec
<A
>> for Vec
<T
>
204 fn a(v
: Vec
<A
>) -> Self {
205 <Vec
<B
>>::a(v
).into_iter().map(Trait
::a
).collect()
210 #[allow(clippy::no_effect, path_statements)]
217 const A
: bool
= true;
233 fn from_something() -> Self {
239 fn test() -> TestStruct
;
242 impl Test
for TestStruct
{
243 fn test() -> TestStruct
{
244 TestStruct
::from_something()
249 mod paths_created_by_lowering
{
262 fn f
<'a
>(&self, p
: &'a
[u8]) -> &'a
[u8] {
268 fn f
<'a
>(&self, p
: &'a
[u8]) -> &'a
[u8];
271 impl T
for Range
<u8> {
272 fn f
<'a
>(&self, p
: &'a
[u8]) -> &'a
[u8] {
285 // `Self` is applicable here
286 fn foo(value
: T
) -> Foo
<T
> {
290 // `Cannot` use `Self` as a return type as the generic types are different
291 fn bar(value
: i32) -> Foo
<i32> {
298 pub struct Error
<From
, To
> {
307 fn from(value
: T
) -> Self;
317 fn try_from(value
: T
) -> Result
<Self, Error
<Self::From
, Self::To
>>;
320 // FIXME: Suggested fix results in infinite recursion.
321 // impl<F, T> TryFrom<F> for T
325 // type From = Self::From;
326 // type To = Self::To;
328 // fn try_from(value: F) -> Result<Self, Error<Self::From, Self::To>> {
329 // Ok(From::from(value))
333 impl From
<bool
> for i64 {
337 fn from(value
: bool
) -> Self {
338 if value { 100 }
else { 0 }
352 impl<T
: Foo
> Foo
for Option
<T
> {
353 type Bar
= Option
<T
::Bar
>;
359 pub struct Bar([usize; 3]);
362 pub const BAR
: usize = 3;
365 const _X
: usize = Foo
::BAR
;
366 // const _Y: usize = Self::BAR;
372 trait Foo
: '
static {}
378 impl<T
: Foo
> From
<T
> for Box
<dyn Foo
> {
379 fn from(t
: T
) -> Self {
385 mod lint_at_item_level
{
388 #[allow(clippy::use_self)]
395 #[allow(clippy::use_self)]
396 impl Default
for Foo
{
397 fn default() -> Foo
{
403 mod lint_at_impl_item_level
{
407 #[allow(clippy::use_self)]
413 impl Default
for Foo
{
414 #[allow(clippy::use_self)]
415 fn default() -> Foo
{
427 impl From
<X
> for u32 {
428 fn from(c
: X
) -> Self {
429 unsafe { core::mem::transmute(c) }
435 use std
::convert
::Into
;
452 fn new
<V
: Into
<T
>>(v
: V
) -> Self {
453 Self { t: Into::into(v) }
459 A
::new
::<submod
::B
>(submod
::B {}
)
465 #[derive(serde::Deserialize)]
472 struct MyStruct
<const C
: char>;
473 impl From
<MyStruct
<'a'
>> for MyStruct
<'b'
> {
474 fn from(_s
: MyStruct
<'a'
>) -> Self {
479 // keep linting non-`Const` generic args
495 fn new_again() -> Self {
501 mod self_is_ty_param
{
512 I
::Item
: Trait
, // changing this to Self would require <Self as Iterator>
519 let _
: I
; // this could lint, but is questionable