]> git.lizzy.rs Git - rust.git/blob - tests/ui/methods.rs
Auto merge of #4634 - m-ober:feature/2320-map-or, r=llogiq
[rust.git] / tests / ui / methods.rs
1 // aux-build:option_helpers.rs
2 // compile-flags: --edition 2018
3
4 #![warn(clippy::all, clippy::pedantic)]
5 #![allow(
6     clippy::blacklisted_name,
7     clippy::default_trait_access,
8     clippy::missing_docs_in_private_items,
9     clippy::non_ascii_literal,
10     clippy::new_without_default,
11     clippy::needless_pass_by_value,
12     clippy::print_stdout,
13     clippy::must_use_candidate,
14     clippy::use_self,
15     clippy::useless_format,
16     clippy::wrong_self_convention,
17     clippy::unused_self,
18     unused
19 )]
20
21 #[macro_use]
22 extern crate option_helpers;
23
24 use std::collections::BTreeMap;
25 use std::collections::HashMap;
26 use std::collections::HashSet;
27 use std::collections::VecDeque;
28 use std::iter::FromIterator;
29 use std::ops::Mul;
30 use std::rc::{self, Rc};
31 use std::sync::{self, Arc};
32
33 use option_helpers::IteratorFalsePositives;
34
35 pub struct T;
36
37 impl T {
38     pub fn add(self, other: T) -> T {
39         self
40     }
41
42     // no error, not public interface
43     pub(crate) fn drop(&mut self) {}
44
45     // no error, private function
46     fn neg(self) -> Self {
47         self
48     }
49
50     // no error, private function
51     fn eq(&self, other: T) -> bool {
52         true
53     }
54
55     // No error; self is a ref.
56     fn sub(&self, other: T) -> &T {
57         self
58     }
59
60     // No error; different number of arguments.
61     fn div(self) -> T {
62         self
63     }
64
65     // No error; wrong return type.
66     fn rem(self, other: T) {}
67
68     // Fine
69     fn into_u32(self) -> u32 {
70         0
71     }
72
73     fn into_u16(&self) -> u16 {
74         0
75     }
76
77     fn to_something(self) -> u32 {
78         0
79     }
80
81     fn new(self) -> Self {
82         unimplemented!();
83     }
84 }
85
86 struct Lt<'a> {
87     foo: &'a u32,
88 }
89
90 impl<'a> Lt<'a> {
91     // The lifetime is different, but that’s irrelevant; see issue #734.
92     #[allow(clippy::needless_lifetimes)]
93     pub fn new<'b>(s: &'b str) -> Lt<'b> {
94         unimplemented!()
95     }
96 }
97
98 struct Lt2<'a> {
99     foo: &'a u32,
100 }
101
102 impl<'a> Lt2<'a> {
103     // The lifetime is different, but that’s irrelevant; see issue #734.
104     pub fn new(s: &str) -> Lt2 {
105         unimplemented!()
106     }
107 }
108
109 struct Lt3<'a> {
110     foo: &'a u32,
111 }
112
113 impl<'a> Lt3<'a> {
114     // The lifetime is different, but that’s irrelevant; see issue #734.
115     pub fn new() -> Lt3<'static> {
116         unimplemented!()
117     }
118 }
119
120 #[derive(Clone, Copy)]
121 struct U;
122
123 impl U {
124     fn new() -> Self {
125         U
126     }
127     // Ok because `U` is `Copy`.
128     fn to_something(self) -> u32 {
129         0
130     }
131 }
132
133 struct V<T> {
134     _dummy: T,
135 }
136
137 impl<T> V<T> {
138     fn new() -> Option<V<T>> {
139         None
140     }
141 }
142
143 struct AsyncNew;
144
145 impl AsyncNew {
146     async fn new() -> Option<Self> {
147         None
148     }
149 }
150
151 struct BadNew;
152
153 impl BadNew {
154     fn new() -> i32 {
155         0
156     }
157 }
158
159 impl Mul<T> for T {
160     type Output = T;
161     // No error, obviously.
162     fn mul(self, other: T) -> T {
163         self
164     }
165 }
166
167 /// Checks implementation of `FILTER_NEXT` lint.
168 #[rustfmt::skip]
169 fn filter_next() {
170     let v = vec![3, 2, 1, 0, -1, -2, -3];
171
172     // Single-line case.
173     let _ = v.iter().filter(|&x| *x < 0).next();
174
175     // Multi-line case.
176     let _ = v.iter().filter(|&x| {
177                                 *x < 0
178                             }
179                    ).next();
180
181     // Check that hat we don't lint if the caller is not an `Iterator`.
182     let foo = IteratorFalsePositives { foo: 0 };
183     let _ = foo.filter().next();
184 }
185
186 /// Checks implementation of `SEARCH_IS_SOME` lint.
187 #[rustfmt::skip]
188 fn search_is_some() {
189     let v = vec![3, 2, 1, 0, -1, -2, -3];
190     let y = &&42;
191
192     // Check `find().is_some()`, single-line case.
193     let _ = v.iter().find(|&x| *x < 0).is_some();
194     let _ = (0..1).find(|x| **y == *x).is_some(); // one dereference less
195     let _ = (0..1).find(|x| *x == 0).is_some();
196     let _ = v.iter().find(|x| **x == 0).is_some();
197
198     // Check `find().is_some()`, multi-line case.
199     let _ = v.iter().find(|&x| {
200                               *x < 0
201                           }
202                    ).is_some();
203
204     // Check `position().is_some()`, single-line case.
205     let _ = v.iter().position(|&x| x < 0).is_some();
206
207     // Check `position().is_some()`, multi-line case.
208     let _ = v.iter().position(|&x| {
209                                   x < 0
210                               }
211                    ).is_some();
212
213     // Check `rposition().is_some()`, single-line case.
214     let _ = v.iter().rposition(|&x| x < 0).is_some();
215
216     // Check `rposition().is_some()`, multi-line case.
217     let _ = v.iter().rposition(|&x| {
218                                    x < 0
219                                }
220                    ).is_some();
221
222     // Check that we don't lint if the caller is not an `Iterator`.
223     let foo = IteratorFalsePositives { foo: 0 };
224     let _ = foo.find().is_some();
225     let _ = foo.position().is_some();
226     let _ = foo.rposition().is_some();
227 }
228
229 fn main() {
230     filter_next();
231     search_is_some();
232 }