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 // *******************************************
41 // complete trait method list, should lint all
42 // *******************************************
43 pub fn add(self, other: T) -> T {
47 pub fn as_mut(&mut self) -> &mut T {
51 pub fn as_ref(&self) -> &T {
55 pub fn bitand(self, rhs: T) -> T {
59 pub fn bitor(self, rhs: Self) -> Self {
63 pub fn bitxor(self, rhs: Self) -> Self {
67 pub fn borrow(&self) -> &str {
71 pub fn borrow_mut(&mut self) -> &mut str {
75 pub fn clone(&self) -> Self {
79 pub fn cmp(&self, other: &Self) -> Self {
83 pub fn default() -> Self {
87 pub fn deref(&self) -> &Self {
91 pub fn deref_mut(&mut self) -> &mut Self {
95 pub fn div(self, rhs: Self) -> Self {
99 pub fn drop(&mut self) {
103 pub fn eq(&self, other: &Self) -> bool {
107 pub fn from_iter<T>(iter: T) -> Self {
111 pub fn from_str(s: &str) -> Result<Self, Self> {
115 pub fn hash(&self, state: &mut T) {
119 pub fn index(&self, index: usize) -> &Self {
123 pub fn index_mut(&mut self, index: usize) -> &mut Self {
127 pub fn into_iter(self) -> Self {
131 pub fn mul(self, rhs: Self) -> Self {
135 pub fn neg(self) -> Self {
139 pub fn next(&mut self) -> Option<Self> {
143 pub fn not(self) -> Self {
147 pub fn rem(self, rhs: Self) -> Self {
151 pub fn shl(self, rhs: Self) -> Self {
155 pub fn shr(self, rhs: Self) -> Self {
159 pub fn sub(self, rhs: Self) -> Self {
169 // corner cases: should not lint
171 // no error, not public interface
172 pub(crate) fn drop(&mut self) {}
174 // no error, private function
175 fn neg(self) -> Self {
179 // no error, private function
180 fn eq(&self, other: Self) -> bool {
184 // No error; self is a ref.
185 fn sub(&self, other: Self) -> &Self {
189 // No error; different number of arguments.
190 fn div(self) -> Self {
194 // No error; wrong return type.
195 fn rem(self, other: Self) {}
198 fn into_u32(self) -> u32 {
202 fn into_u16(&self) -> u16 {
206 fn to_something(self) -> u32 {
210 fn new(self) -> Self {
214 pub fn next<'b>(&'b mut self) -> Option<&'b mut T> {
221 // Shouldn't trigger lint as it is unsafe.
222 pub unsafe fn add(self, rhs: Self) -> Self {
226 // Should not trigger lint since this is an async function.
227 pub async fn next(&mut self) -> Option<Self> {
237 // The lifetime is different, but that’s irrelevant; see issue #734.
238 #[allow(clippy::needless_lifetimes)]
239 pub fn new<'b>(s: &'b str) -> Lt<'b> {
249 // The lifetime is different, but that’s irrelevant; see issue #734.
250 pub fn new(s: &str) -> Lt2 {
260 // The lifetime is different, but that’s irrelevant; see issue #734.
261 pub fn new() -> Lt3<'static> {
266 #[derive(Clone, Copy)]
273 // Ok because `U` is `Copy`.
274 fn to_something(self) -> u32 {
284 fn new() -> Option<V<T>> {
292 async fn new() -> Option<Self> {
307 // No error, obviously.
308 fn mul(self, other: T) -> T {
313 /// Checks implementation of `FILTER_NEXT` lint.
316 let v = vec![3, 2, 1, 0, -1, -2, -3];
319 let _ = v.iter().filter(|&x| *x < 0).next();
322 let _ = v.iter().filter(|&x| {
327 // Check that hat we don't lint if the caller is not an `Iterator`.
328 let foo = IteratorFalsePositives { foo: 0 };
329 let _ = foo.filter().next();
332 /// Checks implementation of `SEARCH_IS_SOME` lint.
334 fn search_is_some() {
335 let v = vec![3, 2, 1, 0, -1, -2, -3];
338 // Check `find().is_some()`, single-line case.
339 let _ = v.iter().find(|&x| *x < 0).is_some();
340 let _ = (0..1).find(|x| **y == *x).is_some(); // one dereference less
341 let _ = (0..1).find(|x| *x == 0).is_some();
342 let _ = v.iter().find(|x| **x == 0).is_some();
344 // Check `find().is_some()`, multi-line case.
345 let _ = v.iter().find(|&x| {
350 // Check `position().is_some()`, single-line case.
351 let _ = v.iter().position(|&x| x < 0).is_some();
353 // Check `position().is_some()`, multi-line case.
354 let _ = v.iter().position(|&x| {
359 // Check `rposition().is_some()`, single-line case.
360 let _ = v.iter().rposition(|&x| x < 0).is_some();
362 // Check `rposition().is_some()`, multi-line case.
363 let _ = v.iter().rposition(|&x| {
368 // Check that we don't lint if the caller is not an `Iterator`.
369 let foo = IteratorFalsePositives { foo: 0 };
370 let _ = foo.find().is_some();
371 let _ = foo.position().is_some();
372 let _ = foo.rposition().is_some();