1 // aux-build:option_helpers.rs
2 // compile-flags: --edition 2018
4 #![feature(async_await)]
5 #![warn(clippy::all, clippy::pedantic, clippy::option_unwrap_used)]
7 clippy::blacklisted_name,
10 clippy::non_ascii_literal,
11 clippy::new_without_default,
12 clippy::missing_docs_in_private_items,
13 clippy::needless_pass_by_value,
14 clippy::default_trait_access,
16 clippy::useless_format,
17 clippy::wrong_self_convention
21 extern crate option_helpers;
23 use std::collections::BTreeMap;
24 use std::collections::HashMap;
25 use std::collections::HashSet;
26 use std::collections::VecDeque;
27 use std::iter::FromIterator;
29 use std::rc::{self, Rc};
30 use std::sync::{self, Arc};
32 use option_helpers::IteratorFalsePositives;
37 pub fn add(self, other: T) -> T {
41 // no error, not public interface
42 pub(crate) fn drop(&mut self) {}
44 // no error, private function
45 fn neg(self) -> Self {
49 // no error, private function
50 fn eq(&self, other: T) -> bool {
54 // No error; self is a ref.
55 fn sub(&self, other: T) -> &T {
59 // No error; different number of arguments.
64 // No error; wrong return type.
65 fn rem(self, other: T) {}
68 fn into_u32(self) -> u32 {
72 fn into_u16(&self) -> u16 {
76 fn to_something(self) -> u32 {
80 fn new(self) -> Self {
90 // The lifetime is different, but that’s irrelevant; see issue #734.
91 #[allow(clippy::needless_lifetimes)]
92 pub fn new<'b>(s: &'b str) -> Lt<'b> {
102 // The lifetime is different, but that’s irrelevant; see issue #734.
103 pub fn new(s: &str) -> Lt2 {
113 // The lifetime is different, but that’s irrelevant; see issue #734.
114 pub fn new() -> Lt3<'static> {
119 #[derive(Clone, Copy)]
126 // Ok because `U` is `Copy`.
127 fn to_something(self) -> u32 {
137 fn new() -> Option<V<T>> {
145 async fn new() -> Option<Self> {
160 // No error, obviously.
161 fn mul(self, other: T) -> T {
166 /// Checks implementation of the following lints:
167 /// * `OPTION_MAP_UNWRAP_OR`
168 /// * `OPTION_MAP_UNWRAP_OR_ELSE`
170 fn option_methods() {
173 // Check `OPTION_MAP_UNWRAP_OR`.
175 let _ = opt.map(|x| x + 1)
176 // Should lint even though this call is on a separate line.
179 let _ = opt.map(|x| {
183 let _ = opt.map(|x| x + 1)
187 // Single line `map(f).unwrap_or(None)` case.
188 let _ = opt.map(|x| Some(x + 1)).unwrap_or(None);
189 // Multi-line `map(f).unwrap_or(None)` cases.
190 let _ = opt.map(|x| {
195 .map(|x| Some(x + 1))
198 let _ = opt_map!(opt, |x| x + 1).unwrap_or(0); // should not lint
200 // Should not lint if not copyable
201 let id: String = "identifier".to_string();
202 let _ = Some("prefix").map(|p| format!("{}.{}", p, id)).unwrap_or(id);
203 // ...but DO lint if the `unwrap_or` argument is not used in the `map`
204 let id: String = "identifier".to_string();
205 let _ = Some("prefix").map(|p| format!("{}.", p)).unwrap_or(id);
207 // Check OPTION_MAP_UNWRAP_OR_ELSE
209 let _ = opt.map(|x| x + 1)
210 // Should lint even though this call is on a separate line.
211 .unwrap_or_else(|| 0);
213 let _ = opt.map(|x| {
216 ).unwrap_or_else(|| 0);
217 let _ = opt.map(|x| x + 1)
223 let _ = opt_map!(opt, |x| x + 1).unwrap_or_else(|| 0);
227 let mut frequencies = HashMap::new();
236 frequencies.insert(word.to_owned(), 1);
241 /// Checks implementation of `FILTER_NEXT` lint.
244 let v = vec![3, 2, 1, 0, -1, -2, -3];
247 let _ = v.iter().filter(|&x| *x < 0).next();
250 let _ = v.iter().filter(|&x| {
255 // Check that hat we don't lint if the caller is not an `Iterator`.
256 let foo = IteratorFalsePositives { foo: 0 };
257 let _ = foo.filter().next();
260 /// Checks implementation of `SEARCH_IS_SOME` lint.
262 fn search_is_some() {
263 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();
268 // Check `find().is_some()`, multi-line case.
269 let _ = v.iter().find(|&x| {
274 // Check `position().is_some()`, single-line case.
275 let _ = v.iter().position(|&x| x < 0).is_some();
277 // Check `position().is_some()`, multi-line case.
278 let _ = v.iter().position(|&x| {
283 // Check `rposition().is_some()`, single-line case.
284 let _ = v.iter().rposition(|&x| x < 0).is_some();
286 // Check `rposition().is_some()`, multi-line case.
287 let _ = v.iter().rposition(|&x| {
292 // Check that we don't lint if the caller is not an `Iterator`.
293 let foo = IteratorFalsePositives { foo: 0 };
294 let _ = foo.find().is_some();
295 let _ = foo.position().is_some();
296 let _ = foo.rposition().is_some();
299 #[allow(clippy::similar_names)]
302 let _ = opt.unwrap();