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::new_ret_no_self,
17 clippy::useless_format,
18 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::future::Future;
29 use std::iter::FromIterator;
32 use std::rc::{self, Rc};
33 use std::sync::{self, Arc};
34 use std::task::{Context, Poll};
36 use option_helpers::IteratorFalsePositives;
41 pub fn add(self, other: T) -> T {
45 // no error, not public interface
46 pub(crate) fn drop(&mut self) {}
48 // no error, private function
49 fn neg(self) -> Self {
53 // no error, private function
54 fn eq(&self, other: T) -> bool {
58 // No error; self is a ref.
59 fn sub(&self, other: T) -> &T {
63 // No error; different number of arguments.
68 // No error; wrong return type.
69 fn rem(self, other: T) {}
72 fn into_u32(self) -> u32 {
76 fn into_u16(&self) -> u16 {
80 fn to_something(self) -> u32 {
84 fn new(self) -> Self {
94 // The lifetime is different, but that’s irrelevant; see issue #734.
95 #[allow(clippy::needless_lifetimes)]
96 pub fn new<'b>(s: &'b str) -> Lt<'b> {
106 // The lifetime is different, but that’s irrelevant; see issue #734.
107 pub fn new(s: &str) -> Lt2 {
117 // The lifetime is different, but that’s irrelevant; see issue #734.
118 pub fn new() -> Lt3<'static> {
123 #[derive(Clone, Copy)]
130 // Ok because `U` is `Copy`.
131 fn to_something(self) -> u32 {
141 fn new() -> Option<V<T>> {
149 async fn new() -> Option<Self> {
156 // No error, obviously.
157 fn mul(self, other: T) -> T {
162 /// Checks implementation of the following lints:
163 /// * `OPTION_MAP_UNWRAP_OR`
164 /// * `OPTION_MAP_UNWRAP_OR_ELSE`
166 fn option_methods() {
169 // Check `OPTION_MAP_UNWRAP_OR`.
171 let _ = opt.map(|x| x + 1)
172 // Should lint even though this call is on a separate line.
175 let _ = opt.map(|x| {
179 let _ = opt.map(|x| x + 1)
183 // Single line `map(f).unwrap_or(None)` case.
184 let _ = opt.map(|x| Some(x + 1)).unwrap_or(None);
185 // Multi-line `map(f).unwrap_or(None)` cases.
186 let _ = opt.map(|x| {
191 .map(|x| Some(x + 1))
194 let _ = opt_map!(opt, |x| x + 1).unwrap_or(0); // should not lint
196 // Should not lint if not copyable
197 let id: String = "identifier".to_string();
198 let _ = Some("prefix").map(|p| format!("{}.{}", p, id)).unwrap_or(id);
199 // ...but DO lint if the `unwrap_or` argument is not used in the `map`
200 let id: String = "identifier".to_string();
201 let _ = Some("prefix").map(|p| format!("{}.", p)).unwrap_or(id);
203 // Check OPTION_MAP_UNWRAP_OR_ELSE
205 let _ = opt.map(|x| x + 1)
206 // Should lint even though this call is on a separate line.
207 .unwrap_or_else(|| 0);
209 let _ = opt.map(|x| {
212 ).unwrap_or_else(|| 0);
213 let _ = opt.map(|x| x + 1)
219 let _ = opt_map!(opt, |x| x + 1).unwrap_or_else(|| 0);
223 let mut frequencies = HashMap::new();
232 frequencies.insert(word.to_owned(), 1);
237 /// Checks implementation of `FILTER_NEXT` lint.
240 let v = vec![3, 2, 1, 0, -1, -2, -3];
243 let _ = v.iter().filter(|&x| *x < 0).next();
246 let _ = v.iter().filter(|&x| {
251 // Check that hat we don't lint if the caller is not an `Iterator`.
252 let foo = IteratorFalsePositives { foo: 0 };
253 let _ = foo.filter().next();
256 /// Checks implementation of `SEARCH_IS_SOME` lint.
258 fn search_is_some() {
259 let v = vec![3, 2, 1, 0, -1, -2, -3];
261 // Check `find().is_some()`, single-line case.
262 let _ = v.iter().find(|&x| *x < 0).is_some();
264 // Check `find().is_some()`, multi-line case.
265 let _ = v.iter().find(|&x| {
270 // Check `position().is_some()`, single-line case.
271 let _ = v.iter().position(|&x| x < 0).is_some();
273 // Check `position().is_some()`, multi-line case.
274 let _ = v.iter().position(|&x| {
279 // Check `rposition().is_some()`, single-line case.
280 let _ = v.iter().rposition(|&x| x < 0).is_some();
282 // Check `rposition().is_some()`, multi-line case.
283 let _ = v.iter().rposition(|&x| {
288 // Check that we don't lint if the caller is not an `Iterator`.
289 let foo = IteratorFalsePositives { foo: 0 };
290 let _ = foo.find().is_some();
291 let _ = foo.position().is_some();
292 let _ = foo.rposition().is_some();
295 #[allow(clippy::similar_names)]
298 let _ = opt.unwrap();