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::iter::FromIterator;
31 use std::rc::{self, Rc};
32 use std::sync::{self, Arc};
34 use option_helpers::IteratorFalsePositives;
39 pub fn add(self, other: T) -> T {
43 // no error, not public interface
44 pub(crate) fn drop(&mut self) {}
46 // no error, private function
47 fn neg(self) -> Self {
51 // no error, private function
52 fn eq(&self, other: T) -> bool {
56 // No error; self is a ref.
57 fn sub(&self, other: T) -> &T {
61 // No error; different number of arguments.
66 // No error; wrong return type.
67 fn rem(self, other: T) {}
70 fn into_u32(self) -> u32 {
74 fn into_u16(&self) -> u16 {
78 fn to_something(self) -> u32 {
82 fn new(self) -> Self {
92 // The lifetime is different, but that’s irrelevant; see issue #734.
93 #[allow(clippy::needless_lifetimes)]
94 pub fn new<'b>(s: &'b str) -> Lt<'b> {
104 // The lifetime is different, but that’s irrelevant; see issue #734.
105 pub fn new(s: &str) -> Lt2 {
115 // The lifetime is different, but that’s irrelevant; see issue #734.
116 pub fn new() -> Lt3<'static> {
121 #[derive(Clone, Copy)]
128 // Ok because `U` is `Copy`.
129 fn to_something(self) -> u32 {
139 fn new() -> Option<V<T>> {
147 async fn new() -> Option<Self> {
154 // No error, obviously.
155 fn mul(self, other: T) -> T {
160 /// Checks implementation of the following lints:
161 /// * `OPTION_MAP_UNWRAP_OR`
162 /// * `OPTION_MAP_UNWRAP_OR_ELSE`
164 fn option_methods() {
167 // Check `OPTION_MAP_UNWRAP_OR`.
169 let _ = opt.map(|x| x + 1)
170 // Should lint even though this call is on a separate line.
173 let _ = opt.map(|x| {
177 let _ = opt.map(|x| x + 1)
181 // Single line `map(f).unwrap_or(None)` case.
182 let _ = opt.map(|x| Some(x + 1)).unwrap_or(None);
183 // Multi-line `map(f).unwrap_or(None)` cases.
184 let _ = opt.map(|x| {
189 .map(|x| Some(x + 1))
192 let _ = opt_map!(opt, |x| x + 1).unwrap_or(0); // should not lint
194 // Should not lint if not copyable
195 let id: String = "identifier".to_string();
196 let _ = Some("prefix").map(|p| format!("{}.{}", p, id)).unwrap_or(id);
197 // ...but DO lint if the `unwrap_or` argument is not used in the `map`
198 let id: String = "identifier".to_string();
199 let _ = Some("prefix").map(|p| format!("{}.", p)).unwrap_or(id);
201 // Check OPTION_MAP_UNWRAP_OR_ELSE
203 let _ = opt.map(|x| x + 1)
204 // Should lint even though this call is on a separate line.
205 .unwrap_or_else(|| 0);
207 let _ = opt.map(|x| {
210 ).unwrap_or_else(|| 0);
211 let _ = opt.map(|x| x + 1)
217 let _ = opt_map!(opt, |x| x + 1).unwrap_or_else(|| 0);
221 let mut frequencies = HashMap::new();
230 frequencies.insert(word.to_owned(), 1);
235 /// Checks implementation of `FILTER_NEXT` lint.
238 let v = vec![3, 2, 1, 0, -1, -2, -3];
241 let _ = v.iter().filter(|&x| *x < 0).next();
244 let _ = v.iter().filter(|&x| {
249 // Check that hat we don't lint if the caller is not an `Iterator`.
250 let foo = IteratorFalsePositives { foo: 0 };
251 let _ = foo.filter().next();
254 /// Checks implementation of `SEARCH_IS_SOME` lint.
256 fn search_is_some() {
257 let v = vec![3, 2, 1, 0, -1, -2, -3];
259 // Check `find().is_some()`, single-line case.
260 let _ = v.iter().find(|&x| *x < 0).is_some();
262 // Check `find().is_some()`, multi-line case.
263 let _ = v.iter().find(|&x| {
268 // Check `position().is_some()`, single-line case.
269 let _ = v.iter().position(|&x| x < 0).is_some();
271 // Check `position().is_some()`, multi-line case.
272 let _ = v.iter().position(|&x| {
277 // Check `rposition().is_some()`, single-line case.
278 let _ = v.iter().rposition(|&x| x < 0).is_some();
280 // Check `rposition().is_some()`, multi-line case.
281 let _ = v.iter().rposition(|&x| {
286 // Check that we don't lint if the caller is not an `Iterator`.
287 let foo = IteratorFalsePositives { foo: 0 };
288 let _ = foo.find().is_some();
289 let _ = foo.position().is_some();
290 let _ = foo.rposition().is_some();
293 #[allow(clippy::similar_names)]
296 let _ = opt.unwrap();