about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/use_self_trait.fixed
blob: c5d800dc94e1861fe61209474c9f82d93d5f20ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#![warn(clippy::use_self)]
#![allow(dead_code)]
#![allow(clippy::should_implement_trait, clippy::boxed_local)]

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<Self>, p2: (&u8, &Self));
    fn vals(r: Self) -> Self;
}

#[derive(Default)]
struct Bad;

impl SelfTrait for Bad {
    fn refs(p1: &Self) -> &Self {
        //~^ use_self
        //~| use_self
        p1
    }

    fn ref_refs<'a>(p1: &'a &'a Self) -> &'a &'a Self {
        //~^ use_self
        //~| use_self
        p1
    }

    fn mut_refs(p1: &mut Self) -> &mut Self {
        //~^ use_self
        //~| use_self
        p1
    }

    fn nested(_p1: Box<Self>, _p2: (&u8, &Self)) {}
    //~^ use_self
    //~| use_self

    fn vals(_: Self) -> Self {
        //~^ use_self
        //~| use_self
        Self
        //~^ use_self
    }
}

impl Mul for Bad {
    type Output = Self;
    //~^ use_self

    fn mul(self, rhs: Self) -> Self {
        //~^ use_self
        //~| use_self
        rhs
    }
}

impl Clone for Bad {
    fn clone(&self) -> Self {
        Self
        //~^ use_self
    }
}

#[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<Self>, _p2: (&u8, &Self)) {}

    fn vals(_: Self) -> Self {
        Self
    }
}

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<u8>, 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<Self>, _p2: (&Self, &Self)) {}

    fn vals(_: Self) -> Self {
        Self::default()
    }
}

mod impl_in_macro {
    macro_rules! parse_ip_impl {
        // minimized from serde=1.0.118
        ($ty:ty) => {
            impl FooTrait for $ty {
                fn new() -> Self {
                    <$ty>::bar()
                }
            }
        };
    }

    struct Foo;

    trait FooTrait {
        fn new() -> Self;
    }

    impl Foo {
        fn bar() -> Self {
            Self
        }
    }
    parse_ip_impl!(Foo); // Should not lint
}

mod full_path_replacement {
    trait Error {
        fn custom<T: std::fmt::Display>(_msg: T) -> Self;
    }

    impl Error for std::fmt::Error {
        fn custom<T: std::fmt::Display>(_msg: T) -> Self {
            Self // Should lint
            //
            //~^^ use_self
        }
    }
}

fn main() {}