1 // aux-build:option_helpers.rs
3 #![warn(clippy::all, clippy::pedantic, clippy::option_unwrap_used)]
5 clippy::blacklisted_name,
8 clippy::non_ascii_literal,
9 clippy::new_without_default,
10 clippy::missing_docs_in_private_items,
11 clippy::needless_pass_by_value,
12 clippy::default_trait_access,
14 clippy::new_ret_no_self,
15 clippy::useless_format,
16 clippy::wrong_self_convention
20 extern crate option_helpers;
22 use std::collections::BTreeMap;
23 use std::collections::HashMap;
24 use std::collections::HashSet;
25 use std::collections::VecDeque;
26 use std::iter::FromIterator;
28 use std::rc::{self, Rc};
29 use std::sync::{self, Arc};
31 use option_helpers::IteratorFalsePositives;
36 pub fn add(self, other: T) -> T {
40 // no error, not public interface
41 pub(crate) fn drop(&mut self) {}
43 // no error, private function
44 fn neg(self) -> Self {
48 // no error, private function
49 fn eq(&self, other: T) -> bool {
53 // No error; self is a ref.
54 fn sub(&self, other: T) -> &T {
58 // No error; different number of arguments.
63 // No error; wrong return type.
64 fn rem(self, other: T) {}
67 fn into_u32(self) -> u32 {
71 fn into_u16(&self) -> u16 {
75 fn to_something(self) -> u32 {
79 fn new(self) -> Self {
89 // The lifetime is different, but that’s irrelevant; see issue #734.
90 #[allow(clippy::needless_lifetimes)]
91 pub fn new<'b>(s: &'b str) -> Lt<'b> {
101 // The lifetime is different, but that’s irrelevant; see issue #734.
102 pub fn new(s: &str) -> Lt2 {
112 // The lifetime is different, but that’s irrelevant; see issue #734.
113 pub fn new() -> Lt3<'static> {
118 #[derive(Clone, Copy)]
125 // Ok because `U` is `Copy`.
126 fn to_something(self) -> u32 {
136 fn new() -> Option<V<T>> {
143 // No error, obviously.
144 fn mul(self, other: T) -> T {
149 /// Checks implementation of the following lints:
150 /// * `OPTION_MAP_UNWRAP_OR`
151 /// * `OPTION_MAP_UNWRAP_OR_ELSE`
153 fn option_methods() {
156 // Check `OPTION_MAP_UNWRAP_OR`.
158 let _ = opt.map(|x| x + 1)
159 // Should lint even though this call is on a separate line.
162 let _ = opt.map(|x| {
166 let _ = opt.map(|x| x + 1)
170 // Single line `map(f).unwrap_or(None)` case.
171 let _ = opt.map(|x| Some(x + 1)).unwrap_or(None);
172 // Multi-line `map(f).unwrap_or(None)` cases.
173 let _ = opt.map(|x| {
178 .map(|x| Some(x + 1))
181 let _ = opt_map!(opt, |x| x + 1).unwrap_or(0); // should not lint
183 // Should not lint if not copyable
184 let id: String = "identifier".to_string();
185 let _ = Some("prefix").map(|p| format!("{}.{}", p, id)).unwrap_or(id);
186 // ...but DO lint if the `unwrap_or` argument is not used in the `map`
187 let id: String = "identifier".to_string();
188 let _ = Some("prefix").map(|p| format!("{}.", p)).unwrap_or(id);
190 // Check OPTION_MAP_UNWRAP_OR_ELSE
192 let _ = opt.map(|x| x + 1)
193 // Should lint even though this call is on a separate line.
194 .unwrap_or_else(|| 0);
196 let _ = opt.map(|x| {
199 ).unwrap_or_else(|| 0);
200 let _ = opt.map(|x| x + 1)
206 let _ = opt_map!(opt, |x| x + 1).unwrap_or_else(|| 0);
210 let mut frequencies = HashMap::new();
219 frequencies.insert(word.to_owned(), 1);
224 /// Checks implementation of `FILTER_NEXT` lint.
227 let v = vec![3, 2, 1, 0, -1, -2, -3];
230 let _ = v.iter().filter(|&x| *x < 0).next();
233 let _ = v.iter().filter(|&x| {
238 // Check that hat we don't lint if the caller is not an `Iterator`.
239 let foo = IteratorFalsePositives { foo: 0 };
240 let _ = foo.filter().next();
243 /// Checks implementation of `SEARCH_IS_SOME` lint.
245 fn search_is_some() {
246 let v = vec![3, 2, 1, 0, -1, -2, -3];
248 // Check `find().is_some()`, single-line case.
249 let _ = v.iter().find(|&x| *x < 0).is_some();
251 // Check `find().is_some()`, multi-line case.
252 let _ = v.iter().find(|&x| {
257 // Check `position().is_some()`, single-line case.
258 let _ = v.iter().position(|&x| x < 0).is_some();
260 // Check `position().is_some()`, multi-line case.
261 let _ = v.iter().position(|&x| {
266 // Check `rposition().is_some()`, single-line case.
267 let _ = v.iter().rposition(|&x| x < 0).is_some();
269 // Check `rposition().is_some()`, multi-line case.
270 let _ = v.iter().rposition(|&x| {
275 // Check that we don't lint if the caller is not an `Iterator`.
276 let foo = IteratorFalsePositives { foo: 0 };
277 let _ = foo.find().is_some();
278 let _ = foo.position().is_some();
279 let _ = foo.rposition().is_some();
282 #[allow(clippy::similar_names)]
285 let _ = opt.unwrap();