]> git.lizzy.rs Git - rust.git/blob - tests/ui/methods.rs
Merge remote-tracking branch 'upstream/master' into sync-from-rust
[rust.git] / 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     // Single-line case.
126     let _ = v.iter().filter(|&x| *x < 0).next();
127
128     // Multi-line case.
129     let _ = v.iter().filter(|&x| {
130                                 *x < 0
131                             }
132                    ).next();
133
134     // Check that hat we don't lint if the caller is not an `Iterator`.
135     let foo = IteratorFalsePositives { foo: 0 };
136     let _ = foo.filter().next();
137 }
138
139 /// Checks implementation of `SEARCH_IS_SOME` lint.
140 #[rustfmt::skip]
141 fn search_is_some() {
142     let v = vec![3, 2, 1, 0, -1, -2, -3];
143     let y = &&42;
144
145     // Check `find().is_some()`, single-line case.
146     let _ = v.iter().find(|&x| *x < 0).is_some();
147     let _ = (0..1).find(|x| **y == *x).is_some(); // one dereference less
148     let _ = (0..1).find(|x| *x == 0).is_some();
149     let _ = v.iter().find(|x| **x == 0).is_some();
150
151     // Check `find().is_some()`, multi-line case.
152     let _ = v.iter().find(|&x| {
153                               *x < 0
154                           }
155                    ).is_some();
156
157     // Check `position().is_some()`, single-line case.
158     let _ = v.iter().position(|&x| x < 0).is_some();
159
160     // Check `position().is_some()`, multi-line case.
161     let _ = v.iter().position(|&x| {
162                                   x < 0
163                               }
164                    ).is_some();
165
166     // Check `rposition().is_some()`, single-line case.
167     let _ = v.iter().rposition(|&x| x < 0).is_some();
168
169     // Check `rposition().is_some()`, multi-line case.
170     let _ = v.iter().rposition(|&x| {
171                                    x < 0
172                                }
173                    ).is_some();
174
175     // Check that we don't lint if the caller is not an `Iterator`.
176     let foo = IteratorFalsePositives { foo: 0 };
177     let _ = foo.find().is_some();
178     let _ = foo.position().is_some();
179     let _ = foo.rposition().is_some();
180 }
181
182 fn main() {
183     filter_next();
184     search_is_some();
185 }