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,
7 clippy::default_trait_access,
8 clippy::missing_docs_in_private_items,
9 clippy::non_ascii_literal,
10 clippy::new_without_default,
11 clippy::needless_pass_by_value,
13 clippy::must_use_candidate,
15 clippy::useless_format,
16 clippy::wrong_self_convention,
22 extern crate option_helpers;
24 use std::collections::BTreeMap;
25 use std::collections::HashMap;
26 use std::collections::HashSet;
27 use std::collections::VecDeque;
28 use std::iter::FromIterator;
30 use std::rc::{self, Rc};
31 use std::sync::{self, Arc};
33 use option_helpers::IteratorFalsePositives;
38 pub fn add(self, other: T) -> T {
42 // no error, not public interface
43 pub(crate) fn drop(&mut self) {}
45 // no error, private function
46 fn neg(self) -> Self {
50 // no error, private function
51 fn eq(&self, other: T) -> bool {
55 // No error; self is a ref.
56 fn sub(&self, other: T) -> &T {
60 // No error; different number of arguments.
65 // No error; wrong return type.
66 fn rem(self, other: T) {}
69 fn into_u32(self) -> u32 {
73 fn into_u16(&self) -> u16 {
77 fn to_something(self) -> u32 {
81 fn new(self) -> Self {
91 // The lifetime is different, but that’s irrelevant; see issue #734.
92 #[allow(clippy::needless_lifetimes)]
93 pub fn new<'b>(s: &'b str) -> Lt<'b> {
103 // The lifetime is different, but that’s irrelevant; see issue #734.
104 pub fn new(s: &str) -> Lt2 {
114 // The lifetime is different, but that’s irrelevant; see issue #734.
115 pub fn new() -> Lt3<'static> {
120 #[derive(Clone, Copy)]
127 // Ok because `U` is `Copy`.
128 fn to_something(self) -> u32 {
138 fn new() -> Option<V<T>> {
146 async fn new() -> Option<Self> {
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];
267 // Check `find().is_some()`, single-line case.
268 let _ = v.iter().find(|&x| *x < 0).is_some();
269 let _ = (0..1).find(|x| **y == *x).is_some(); // one dereference less
270 let _ = (0..1).find(|x| *x == 0).is_some();
271 let _ = v.iter().find(|x| **x == 0).is_some();
273 // Check `find().is_some()`, multi-line case.
274 let _ = v.iter().find(|&x| {
279 // Check `position().is_some()`, single-line case.
280 let _ = v.iter().position(|&x| x < 0).is_some();
282 // Check `position().is_some()`, multi-line case.
283 let _ = v.iter().position(|&x| {
288 // Check `rposition().is_some()`, single-line case.
289 let _ = v.iter().rposition(|&x| x < 0).is_some();
291 // Check `rposition().is_some()`, multi-line case.
292 let _ = v.iter().rposition(|&x| {
297 // Check that we don't lint if the caller is not an `Iterator`.
298 let foo = IteratorFalsePositives { foo: 0 };
299 let _ = foo.find().is_some();
300 let _ = foo.position().is_some();
301 let _ = foo.rposition().is_some();
304 #[allow(clippy::similar_names)]
307 let _ = opt.unwrap();