]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/methods.rs
Rollup merge of #78769 - est31:remove_lifetimes, r=KodrAus
[rust.git] / src / tools / clippy / tests / ui / methods.rs
1 // aux-build:option_helpers.rs
2 // 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::missing_safety_doc,
10     clippy::non_ascii_literal,
11     clippy::new_without_default,
12     clippy::needless_pass_by_value,
13     clippy::needless_lifetimes,
14     clippy::print_stdout,
15     clippy::must_use_candidate,
16     clippy::use_self,
17     clippy::useless_format,
18     clippy::wrong_self_convention,
19     clippy::unused_self,
20     unused
21 )]
22
23 #[macro_use]
24 extern crate option_helpers;
25
26 use std::collections::BTreeMap;
27 use std::collections::HashMap;
28 use std::collections::HashSet;
29 use std::collections::VecDeque;
30 use std::iter::FromIterator;
31 use std::ops::Mul;
32 use std::rc::{self, Rc};
33 use std::sync::{self, Arc};
34
35 use option_helpers::IteratorFalsePositives;
36
37 struct Lt<'a> {
38     foo: &'a u32,
39 }
40
41 impl<'a> Lt<'a> {
42     // The lifetime is different, but that’s irrelevant; see issue #734.
43     #[allow(clippy::needless_lifetimes)]
44     pub fn new<'b>(s: &'b str) -> Lt<'b> {
45         unimplemented!()
46     }
47 }
48
49 struct Lt2<'a> {
50     foo: &'a u32,
51 }
52
53 impl<'a> Lt2<'a> {
54     // The lifetime is different, but that’s irrelevant; see issue #734.
55     pub fn new(s: &str) -> Lt2 {
56         unimplemented!()
57     }
58 }
59
60 struct Lt3<'a> {
61     foo: &'a u32,
62 }
63
64 impl<'a> Lt3<'a> {
65     // The lifetime is different, but that’s irrelevant; see issue #734.
66     pub fn new() -> Lt3<'static> {
67         unimplemented!()
68     }
69 }
70
71 #[derive(Clone, Copy)]
72 struct U;
73
74 impl U {
75     fn new() -> Self {
76         U
77     }
78     // Ok because `U` is `Copy`.
79     fn to_something(self) -> u32 {
80         0
81     }
82 }
83
84 struct V<T> {
85     _dummy: T,
86 }
87
88 impl<T> V<T> {
89     fn new() -> Option<V<T>> {
90         None
91     }
92 }
93
94 struct AsyncNew;
95
96 impl AsyncNew {
97     async fn new() -> Option<Self> {
98         None
99     }
100 }
101
102 struct BadNew;
103
104 impl BadNew {
105     fn new() -> i32 {
106         0
107     }
108 }
109
110 struct T;
111
112 impl Mul<T> for T {
113     type Output = T;
114     // No error, obviously.
115     fn mul(self, other: T) -> T {
116         self
117     }
118 }
119
120 /// Checks implementation of `FILTER_NEXT` lint.
121 #[rustfmt::skip]
122 fn filter_next() {
123     let v = vec![3, 2, 1, 0, -1, -2, -3];
124
125     // Multi-line case.
126     let _ = v.iter().filter(|&x| {
127                                 *x < 0
128                             }
129                    ).next();
130
131     // Check that we don't lint if the caller is not an `Iterator`.
132     let foo = IteratorFalsePositives { foo: 0 };
133     let _ = foo.filter().next();
134 }
135
136 /// Checks implementation of `SEARCH_IS_SOME` lint.
137 #[rustfmt::skip]
138 fn search_is_some() {
139     let v = vec![3, 2, 1, 0, -1, -2, -3];
140     let y = &&42;
141
142     // Check `find().is_some()`, single-line case.
143     let _ = v.iter().find(|&x| *x < 0).is_some();
144     let _ = (0..1).find(|x| **y == *x).is_some(); // one dereference less
145     let _ = (0..1).find(|x| *x == 0).is_some();
146     let _ = v.iter().find(|x| **x == 0).is_some();
147
148     // Check `find().is_some()`, multi-line case.
149     let _ = v.iter().find(|&x| {
150                               *x < 0
151                           }
152                    ).is_some();
153
154     // Check `position().is_some()`, single-line case.
155     let _ = v.iter().position(|&x| x < 0).is_some();
156
157     // Check `position().is_some()`, multi-line case.
158     let _ = v.iter().position(|&x| {
159                                   x < 0
160                               }
161                    ).is_some();
162
163     // Check `rposition().is_some()`, single-line case.
164     let _ = v.iter().rposition(|&x| x < 0).is_some();
165
166     // Check `rposition().is_some()`, multi-line case.
167     let _ = v.iter().rposition(|&x| {
168                                    x < 0
169                                }
170                    ).is_some();
171
172     // Check that we don't lint if the caller is not an `Iterator`.
173     let foo = IteratorFalsePositives { foo: 0 };
174     let _ = foo.find().is_some();
175     let _ = foo.position().is_some();
176     let _ = foo.rposition().is_some();
177 }
178
179 fn main() {
180     filter_next();
181     search_is_some();
182 }