1 // aux-build:option_helpers.rs
3 #![warn(clippy::all, clippy::pedantic, clippy::option_unwrap_used)]
5 clippy::blacklisted_name,
8 clippy::non_ascii_literal,
9 clippy::new_without_default,
10 clippy::missing_docs_in_private_items,
11 clippy::needless_pass_by_value,
12 clippy::default_trait_access,
14 clippy::new_ret_no_self,
15 clippy::useless_format,
16 clippy::wrong_self_convention
20 extern crate option_helpers;
22 use std::collections::BTreeMap;
23 use std::collections::HashMap;
24 use std::collections::HashSet;
25 use std::collections::VecDeque;
26 use std::future::Future;
27 use std::iter::FromIterator;
30 use std::rc::{self, Rc};
31 use std::sync::{self, Arc};
32 use std::task::{Context, Poll};
34 use option_helpers::IteratorFalsePositives;
39 pub fn add(self, other: T) -> T {
43 // no error, not public interface
44 pub(crate) fn drop(&mut self) {}
46 // no error, private function
47 fn neg(self) -> Self {
51 // no error, private function
52 fn eq(&self, other: T) -> bool {
56 // No error; self is a ref.
57 fn sub(&self, other: T) -> &T {
61 // No error; different number of arguments.
66 // No error; wrong return type.
67 fn rem(self, other: T) {}
70 fn into_u32(self) -> u32 {
74 fn into_u16(&self) -> u16 {
78 fn to_something(self) -> u32 {
82 fn new(self) -> Self {
92 // The lifetime is different, but that’s irrelevant; see issue #734.
93 #[allow(clippy::needless_lifetimes)]
94 pub fn new<'b>(s: &'b str) -> Lt<'b> {
104 // The lifetime is different, but that’s irrelevant; see issue #734.
105 pub fn new(s: &str) -> Lt2 {
115 // The lifetime is different, but that’s irrelevant; see issue #734.
116 pub fn new() -> Lt3<'static> {
121 #[derive(Clone, Copy)]
128 // Ok because `U` is `Copy`.
129 fn to_something(self) -> u32 {
139 fn new() -> Option<V<T>> {
147 fn new() -> impl Future<Output = Option<Self>> {
150 type Output = Option<AsyncNew>;
151 fn poll(self: Pin<&mut Self>, _cx: &mut Context) -> Poll<Self::Output> {
161 // No error, obviously.
162 fn mul(self, other: T) -> T {
167 /// Checks implementation of the following lints:
168 /// * `OPTION_MAP_UNWRAP_OR`
169 /// * `OPTION_MAP_UNWRAP_OR_ELSE`
171 fn option_methods() {
174 // Check `OPTION_MAP_UNWRAP_OR`.
176 let _ = opt.map(|x| x + 1)
177 // Should lint even though this call is on a separate line.
180 let _ = opt.map(|x| {
184 let _ = opt.map(|x| x + 1)
188 // Single line `map(f).unwrap_or(None)` case.
189 let _ = opt.map(|x| Some(x + 1)).unwrap_or(None);
190 // Multi-line `map(f).unwrap_or(None)` cases.
191 let _ = opt.map(|x| {
196 .map(|x| Some(x + 1))
199 let _ = opt_map!(opt, |x| x + 1).unwrap_or(0); // should not lint
201 // Should not lint if not copyable
202 let id: String = "identifier".to_string();
203 let _ = Some("prefix").map(|p| format!("{}.{}", p, id)).unwrap_or(id);
204 // ...but DO lint if the `unwrap_or` argument is not used in the `map`
205 let id: String = "identifier".to_string();
206 let _ = Some("prefix").map(|p| format!("{}.", p)).unwrap_or(id);
208 // Check OPTION_MAP_UNWRAP_OR_ELSE
210 let _ = opt.map(|x| x + 1)
211 // Should lint even though this call is on a separate line.
212 .unwrap_or_else(|| 0);
214 let _ = opt.map(|x| {
217 ).unwrap_or_else(|| 0);
218 let _ = opt.map(|x| x + 1)
224 let _ = opt_map!(opt, |x| x + 1).unwrap_or_else(|| 0);
228 let mut frequencies = HashMap::new();
237 frequencies.insert(word.to_owned(), 1);
242 /// Checks implementation of `FILTER_NEXT` lint.
245 let v = vec![3, 2, 1, 0, -1, -2, -3];
248 let _ = v.iter().filter(|&x| *x < 0).next();
251 let _ = v.iter().filter(|&x| {
256 // Check that hat we don't lint if the caller is not an `Iterator`.
257 let foo = IteratorFalsePositives { foo: 0 };
258 let _ = foo.filter().next();
261 /// Checks implementation of `SEARCH_IS_SOME` lint.
263 fn search_is_some() {
264 let v = vec![3, 2, 1, 0, -1, -2, -3];
266 // Check `find().is_some()`, single-line case.
267 let _ = v.iter().find(|&x| *x < 0).is_some();
269 // Check `find().is_some()`, multi-line case.
270 let _ = v.iter().find(|&x| {
275 // Check `position().is_some()`, single-line case.
276 let _ = v.iter().position(|&x| x < 0).is_some();
278 // Check `position().is_some()`, multi-line case.
279 let _ = v.iter().position(|&x| {
284 // Check `rposition().is_some()`, single-line case.
285 let _ = v.iter().rposition(|&x| x < 0).is_some();
287 // Check `rposition().is_some()`, multi-line case.
288 let _ = v.iter().rposition(|&x| {
293 // Check that we don't lint if the caller is not an `Iterator`.
294 let foo = IteratorFalsePositives { foo: 0 };
295 let _ = foo.find().is_some();
296 let _ = foo.position().is_some();
297 let _ = foo.rposition().is_some();
300 #[allow(clippy::similar_names)]
303 let _ = opt.unwrap();