fork/rust

git clone https://git.julia.blue/fork/rust

borsAuto merge of #103046 - JanBeh:PR_clarify_cmp_terminology, r=workingjubilee3bcad65fbf6

master
2.6 KiB150 linesraw
1#![warn(clippy::use_self)]
2#![allow(dead_code)]
3#![allow(clippy::should_implement_trait, clippy::boxed_local)]
4
5use std::ops::Mul;
6
7trait SelfTrait {
8    fn refs(p1: &Self) -> &Self;
9    fn ref_refs<'a>(p1: &'a &'a Self) -> &'a &'a Self;
10    fn mut_refs(p1: &mut Self) -> &mut Self;
11    fn nested(p1: Box<Self>, p2: (&u8, &Self));
12    fn vals(r: Self) -> Self;
13}
14
15#[derive(Default)]
16struct Bad;
17
18impl SelfTrait for Bad {
19    fn refs(p1: &Self) -> &Self {
20        p1
21    }
22
23    fn ref_refs<'a>(p1: &'a &'a Self) -> &'a &'a Self {
24        p1
25    }
26
27    fn mut_refs(p1: &mut Self) -> &mut Self {
28        p1
29    }
30
31    fn nested(_p1: Box<Self>, _p2: (&u8, &Self)) {}
32
33    fn vals(_: Self) -> Self {
34        Self
35    }
36}
37
38impl Mul for Bad {
39    type Output = Self;
40
41    fn mul(self, rhs: Self) -> Self {
42        rhs
43    }
44}
45
46impl Clone for Bad {
47    fn clone(&self) -> Self {
48        Self
49    }
50}
51
52#[derive(Default)]
53struct Good;
54
55impl SelfTrait for Good {
56    fn refs(p1: &Self) -> &Self {
57        p1
58    }
59
60    fn ref_refs<'a>(p1: &'a &'a Self) -> &'a &'a Self {
61        p1
62    }
63
64    fn mut_refs(p1: &mut Self) -> &mut Self {
65        p1
66    }
67
68    fn nested(_p1: Box<Self>, _p2: (&u8, &Self)) {}
69
70    fn vals(_: Self) -> Self {
71        Self
72    }
73}
74
75impl Mul for Good {
76    type Output = Self;
77
78    fn mul(self, rhs: Self) -> Self {
79        rhs
80    }
81}
82
83trait NameTrait {
84    fn refs(p1: &u8) -> &u8;
85    fn ref_refs<'a>(p1: &'a &'a u8) -> &'a &'a u8;
86    fn mut_refs(p1: &mut u8) -> &mut u8;
87    fn nested(p1: Box<u8>, p2: (&u8, &u8));
88    fn vals(p1: u8) -> u8;
89}
90
91// Using `Self` instead of the type name is OK
92impl NameTrait for u8 {
93    fn refs(p1: &Self) -> &Self {
94        p1
95    }
96
97    fn ref_refs<'a>(p1: &'a &'a Self) -> &'a &'a Self {
98        p1
99    }
100
101    fn mut_refs(p1: &mut Self) -> &mut Self {
102        p1
103    }
104
105    fn nested(_p1: Box<Self>, _p2: (&Self, &Self)) {}
106
107    fn vals(_: Self) -> Self {
108        Self::default()
109    }
110}
111
112mod impl_in_macro {
113    macro_rules! parse_ip_impl {
114        // minimized from serde=1.0.118
115        ($ty:ty) => {
116            impl FooTrait for $ty {
117                fn new() -> Self {
118                    <$ty>::bar()
119                }
120            }
121        };
122    }
123
124    struct Foo;
125
126    trait FooTrait {
127        fn new() -> Self;
128    }
129
130    impl Foo {
131        fn bar() -> Self {
132            Self
133        }
134    }
135    parse_ip_impl!(Foo); // Should not lint
136}
137
138mod full_path_replacement {
139    trait Error {
140        fn custom<T: std::fmt::Display>(_msg: T) -> Self;
141    }
142
143    impl Error for std::fmt::Error {
144        fn custom<T: std::fmt::Display>(_msg: T) -> Self {
145            Self // Should lint
146        }
147    }
148}
149
150fn main() {}