1 // aux-build:option_helpers.rs
2 // compile-flags: --edition 2018
4 #![warn(clippy::all, clippy::pedantic, clippy::option_unwrap_used)]
6 clippy::blacklisted_name,
9 clippy::non_ascii_literal,
10 clippy::new_without_default,
11 clippy::missing_docs_in_private_items,
12 clippy::needless_pass_by_value,
13 clippy::default_trait_access,
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::iter::FromIterator;
28 use std::rc::{self, Rc};
29 use std::sync::{self, Arc};
31 use option_helpers::IteratorFalsePositives;
36 pub fn add(self, other: T) -> T {
40 // no error, not public interface
41 pub(crate) fn drop(&mut self) {}
43 // no error, private function
44 fn neg(self) -> Self {
48 // no error, private function
49 fn eq(&self, other: T) -> bool {
53 // No error; self is a ref.
54 fn sub(&self, other: T) -> &T {
58 // No error; different number of arguments.
63 // No error; wrong return type.
64 fn rem(self, other: T) {}
67 fn into_u32(self) -> u32 {
71 fn into_u16(&self) -> u16 {
75 fn to_something(self) -> u32 {
79 fn new(self) -> Self {
89 // The lifetime is different, but that’s irrelevant; see issue #734.
90 #[allow(clippy::needless_lifetimes)]
91 pub fn new<'b>(s: &'b str) -> Lt<'b> {
101 // The lifetime is different, but that’s irrelevant; see issue #734.
102 pub fn new(s: &str) -> Lt2 {
112 // The lifetime is different, but that’s irrelevant; see issue #734.
113 pub fn new() -> Lt3<'static> {
118 #[derive(Clone, Copy)]
125 // Ok because `U` is `Copy`.
126 fn to_something(self) -> u32 {
136 fn new() -> Option<V<T>> {
144 async fn new() -> Option<Self> {
159 // No error, obviously.
160 fn mul(self, other: T) -> T {
165 /// Checks implementation of the following lints:
166 /// * `OPTION_MAP_UNWRAP_OR`
167 /// * `OPTION_MAP_UNWRAP_OR_ELSE`
169 fn option_methods() {
172 // Check `OPTION_MAP_UNWRAP_OR`.
174 let _ = opt.map(|x| x + 1)
175 // Should lint even though this call is on a separate line.
178 let _ = opt.map(|x| {
182 let _ = opt.map(|x| x + 1)
186 // Single line `map(f).unwrap_or(None)` case.
187 let _ = opt.map(|x| Some(x + 1)).unwrap_or(None);
188 // Multi-line `map(f).unwrap_or(None)` cases.
189 let _ = opt.map(|x| {
194 .map(|x| Some(x + 1))
197 let _ = opt_map!(opt, |x| x + 1).unwrap_or(0); // should not lint
199 // Should not lint if not copyable
200 let id: String = "identifier".to_string();
201 let _ = Some("prefix").map(|p| format!("{}.{}", p, id)).unwrap_or(id);
202 // ...but DO lint if the `unwrap_or` argument is not used in the `map`
203 let id: String = "identifier".to_string();
204 let _ = Some("prefix").map(|p| format!("{}.", p)).unwrap_or(id);
206 // Check OPTION_MAP_UNWRAP_OR_ELSE
208 let _ = opt.map(|x| x + 1)
209 // Should lint even though this call is on a separate line.
210 .unwrap_or_else(|| 0);
212 let _ = opt.map(|x| {
215 ).unwrap_or_else(|| 0);
216 let _ = opt.map(|x| x + 1)
222 let _ = opt_map!(opt, |x| x + 1).unwrap_or_else(|| 0);
226 let mut frequencies = HashMap::new();
235 frequencies.insert(word.to_owned(), 1);
240 /// Checks implementation of `FILTER_NEXT` lint.
243 let v = vec![3, 2, 1, 0, -1, -2, -3];
246 let _ = v.iter().filter(|&x| *x < 0).next();
249 let _ = v.iter().filter(|&x| {
254 // Check that hat we don't lint if the caller is not an `Iterator`.
255 let foo = IteratorFalsePositives { foo: 0 };
256 let _ = foo.filter().next();
259 /// Checks implementation of `SEARCH_IS_SOME` lint.
261 fn search_is_some() {
262 let v = vec![3, 2, 1, 0, -1, -2, -3];
265 // Check `find().is_some()`, single-line case.
266 let _ = v.iter().find(|&x| *x < 0).is_some();
267 let _ = (0..1).find(|x| **y == *x).is_some(); // one dereference less
268 let _ = (0..1).find(|x| *x == 0).is_some();
269 let _ = v.iter().find(|x| **x == 0).is_some();
271 // Check `find().is_some()`, multi-line case.
272 let _ = v.iter().find(|&x| {
277 // Check `position().is_some()`, single-line case.
278 let _ = v.iter().position(|&x| x < 0).is_some();
280 // Check `position().is_some()`, multi-line case.
281 let _ = v.iter().position(|&x| {
286 // Check `rposition().is_some()`, single-line case.
287 let _ = v.iter().rposition(|&x| x < 0).is_some();
289 // Check `rposition().is_some()`, multi-line case.
290 let _ = v.iter().rposition(|&x| {
295 // Check that we don't lint if the caller is not an `Iterator`.
296 let foo = IteratorFalsePositives { foo: 0 };
297 let _ = foo.find().is_some();
298 let _ = foo.position().is_some();
299 let _ = foo.rposition().is_some();
302 #[allow(clippy::similar_names)]
305 let _ = opt.unwrap();