1 // aux-build:option_helpers.rs
4 #![warn(clippy::all, clippy::pedantic)]
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,
15 clippy::must_use_candidate,
17 clippy::useless_format,
18 clippy::wrong_self_convention,
24 extern crate option_helpers;
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;
32 use std::rc::{self, Rc};
33 use std::sync::{self, Arc};
35 use option_helpers::IteratorFalsePositives;
40 pub fn add(self, other: T) -> T {
44 // no error, not public interface
45 pub(crate) fn drop(&mut self) {}
47 // no error, private function
48 fn neg(self) -> Self {
52 // no error, private function
53 fn eq(&self, other: T) -> bool {
57 // No error; self is a ref.
58 fn sub(&self, other: T) -> &T {
62 // No error; different number of arguments.
67 // No error; wrong return type.
68 fn rem(self, other: T) {}
71 fn into_u32(self) -> u32 {
75 fn into_u16(&self) -> u16 {
79 fn to_something(self) -> u32 {
83 fn new(self) -> Self {
87 pub fn next<'b>(&'b mut self) -> Option<&'b mut T> {
95 // Shouldn't trigger lint as it is unsafe.
96 pub unsafe fn add(self, rhs: T1) -> T1 {
100 // Should not trigger lint since this is an async function.
101 pub async fn next(&mut self) -> Option<T1> {
111 // The lifetime is different, but that’s irrelevant; see issue #734.
112 #[allow(clippy::needless_lifetimes)]
113 pub fn new<'b>(s: &'b str) -> Lt<'b> {
123 // The lifetime is different, but that’s irrelevant; see issue #734.
124 pub fn new(s: &str) -> Lt2 {
134 // The lifetime is different, but that’s irrelevant; see issue #734.
135 pub fn new() -> Lt3<'static> {
140 #[derive(Clone, Copy)]
147 // Ok because `U` is `Copy`.
148 fn to_something(self) -> u32 {
158 fn new() -> Option<V<T>> {
166 async fn new() -> Option<Self> {
181 // No error, obviously.
182 fn mul(self, other: T) -> T {
187 /// Checks implementation of `FILTER_NEXT` lint.
190 let v = vec![3, 2, 1, 0, -1, -2, -3];
193 let _ = v.iter().filter(|&x| *x < 0).next();
196 let _ = v.iter().filter(|&x| {
201 // Check that hat we don't lint if the caller is not an `Iterator`.
202 let foo = IteratorFalsePositives { foo: 0 };
203 let _ = foo.filter().next();
206 /// Checks implementation of `SEARCH_IS_SOME` lint.
208 fn search_is_some() {
209 let v = vec![3, 2, 1, 0, -1, -2, -3];
212 // Check `find().is_some()`, single-line case.
213 let _ = v.iter().find(|&x| *x < 0).is_some();
214 let _ = (0..1).find(|x| **y == *x).is_some(); // one dereference less
215 let _ = (0..1).find(|x| *x == 0).is_some();
216 let _ = v.iter().find(|x| **x == 0).is_some();
218 // Check `find().is_some()`, multi-line case.
219 let _ = v.iter().find(|&x| {
224 // Check `position().is_some()`, single-line case.
225 let _ = v.iter().position(|&x| x < 0).is_some();
227 // Check `position().is_some()`, multi-line case.
228 let _ = v.iter().position(|&x| {
233 // Check `rposition().is_some()`, single-line case.
234 let _ = v.iter().rposition(|&x| x < 0).is_some();
236 // Check `rposition().is_some()`, multi-line case.
237 let _ = v.iter().rposition(|&x| {
242 // Check that we don't lint if the caller is not an `Iterator`.
243 let foo = IteratorFalsePositives { foo: 0 };
244 let _ = foo.find().is_some();
245 let _ = foo.position().is_some();
246 let _ = foo.rposition().is_some();