]> git.lizzy.rs Git - rust.git/blob - tests/ui/len_without_is_empty.rs
Auto merge of #4478 - tsurai:master, r=flip1995
[rust.git] / tests / ui / len_without_is_empty.rs
1 #![warn(clippy::len_without_is_empty)]
2 #![allow(dead_code, unused)]
3
4 pub struct PubOne;
5
6 impl PubOne {
7     pub fn len(self: &Self) -> isize {
8         1
9     }
10 }
11
12 impl PubOne {
13     // A second impl for this struct -- the error span shouldn't mention this.
14     pub fn irrelevant(self: &Self) -> bool {
15         false
16     }
17 }
18
19 // Identical to `PubOne`, but with an `allow` attribute on the impl complaining `len`.
20 pub struct PubAllowed;
21
22 #[allow(clippy::len_without_is_empty)]
23 impl PubAllowed {
24     pub fn len(self: &Self) -> isize {
25         1
26     }
27 }
28
29 // No `allow` attribute on this impl block, but that doesn't matter -- we only require one on the
30 // impl containing `len`.
31 impl PubAllowed {
32     pub fn irrelevant(self: &Self) -> bool {
33         false
34     }
35 }
36
37 pub trait PubTraitsToo {
38     fn len(self: &Self) -> isize;
39 }
40
41 impl PubTraitsToo for One {
42     fn len(self: &Self) -> isize {
43         0
44     }
45 }
46
47 pub struct HasIsEmpty;
48
49 impl HasIsEmpty {
50     pub fn len(self: &Self) -> isize {
51         1
52     }
53
54     fn is_empty(self: &Self) -> bool {
55         false
56     }
57 }
58
59 pub struct HasWrongIsEmpty;
60
61 impl HasWrongIsEmpty {
62     pub fn len(self: &Self) -> isize {
63         1
64     }
65
66     pub fn is_empty(self: &Self, x: u32) -> bool {
67         false
68     }
69 }
70
71 struct NotPubOne;
72
73 impl NotPubOne {
74     pub fn len(self: &Self) -> isize {
75         // No error; `len` is pub but `NotPubOne` is not exported anyway.
76         1
77     }
78 }
79
80 struct One;
81
82 impl One {
83     fn len(self: &Self) -> isize {
84         // No error; `len` is private; see issue #1085.
85         1
86     }
87 }
88
89 trait TraitsToo {
90     fn len(self: &Self) -> isize;
91     // No error; `len` is private; see issue #1085.
92 }
93
94 impl TraitsToo for One {
95     fn len(self: &Self) -> isize {
96         0
97     }
98 }
99
100 struct HasPrivateIsEmpty;
101
102 impl HasPrivateIsEmpty {
103     pub fn len(self: &Self) -> isize {
104         1
105     }
106
107     fn is_empty(self: &Self) -> bool {
108         false
109     }
110 }
111
112 struct Wither;
113
114 pub trait WithIsEmpty {
115     fn len(self: &Self) -> isize;
116     fn is_empty(self: &Self) -> bool;
117 }
118
119 impl WithIsEmpty for Wither {
120     fn len(self: &Self) -> isize {
121         1
122     }
123
124     fn is_empty(self: &Self) -> bool {
125         false
126     }
127 }
128
129 pub trait Empty {
130     fn is_empty(&self) -> bool;
131 }
132
133 pub trait InheritingEmpty: Empty {
134     // Must not trigger `LEN_WITHOUT_IS_EMPTY`.
135     fn len(&self) -> isize;
136 }
137
138 // This used to ICE.
139 pub trait Foo: Sized {}
140
141 pub trait DependsOnFoo: Foo {
142     fn len(&mut self) -> usize;
143 }
144
145 fn main() {}