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
19 extern crate option_helpers;
21 use std::collections::BTreeMap;
22 use std::collections::HashMap;
23 use std::collections::HashSet;
24 use std::collections::VecDeque;
25 use std::iter::FromIterator;
27 use std::rc::{self, Rc};
28 use std::sync::{self, Arc};
30 use option_helpers::IteratorFalsePositives;
35 pub fn add(self, other: T) -> T {
39 // no error, not public interface
40 pub(crate) fn drop(&mut self) {}
42 // no error, private function
43 fn neg(self) -> Self {
47 // no error, private function
48 fn eq(&self, other: T) -> bool {
52 // No error; self is a ref.
53 fn sub(&self, other: T) -> &T {
57 // No error; different number of arguments.
62 // No error; wrong return type.
63 fn rem(self, other: T) {}
66 fn into_u32(self) -> u32 {
70 fn into_u16(&self) -> u16 {
74 fn to_something(self) -> u32 {
78 fn new(self) -> Self {
88 // The lifetime is different, but that’s irrelevant; see issue #734.
89 #[allow(clippy::needless_lifetimes)]
90 pub fn new<'b>(s: &'b str) -> Lt<'b> {
100 // The lifetime is different, but that’s irrelevant; see issue #734.
101 pub fn new(s: &str) -> Lt2 {
111 // The lifetime is different, but that’s irrelevant; see issue #734.
112 pub fn new() -> Lt3<'static> {
117 #[derive(Clone, Copy)]
124 // Ok because `U` is `Copy`.
125 fn to_something(self) -> u32 {
135 fn new() -> Option<V<T>> {
142 // No error, obviously.
143 fn mul(self, other: T) -> T {
148 /// Checks implementation of the following lints:
149 /// * `OPTION_MAP_UNWRAP_OR`
150 /// * `OPTION_MAP_UNWRAP_OR_ELSE`
152 fn option_methods() {
155 // Check `OPTION_MAP_UNWRAP_OR`.
157 let _ = opt.map(|x| x + 1)
158 // Should lint even though this call is on a separate line.
161 let _ = opt.map(|x| {
165 let _ = opt.map(|x| x + 1)
169 // Single line `map(f).unwrap_or(None)` case.
170 let _ = opt.map(|x| Some(x + 1)).unwrap_or(None);
171 // Multi-line `map(f).unwrap_or(None)` cases.
172 let _ = opt.map(|x| {
177 .map(|x| Some(x + 1))
180 let _ = opt_map!(opt, |x| x + 1).unwrap_or(0); // should not lint
182 // Should not lint if not copyable
183 let id: String = "identifier".to_string();
184 let _ = Some("prefix").map(|p| format!("{}.{}", p, id)).unwrap_or(id);
185 // ...but DO lint if the `unwrap_or` argument is not used in the `map`
186 let id: String = "identifier".to_string();
187 let _ = Some("prefix").map(|p| format!("{}.", p)).unwrap_or(id);
189 // Check OPTION_MAP_UNWRAP_OR_ELSE
191 let _ = opt.map(|x| x + 1)
192 // Should lint even though this call is on a separate line.
193 .unwrap_or_else(|| 0);
195 let _ = opt.map(|x| {
198 ).unwrap_or_else(|| 0);
199 let _ = opt.map(|x| x + 1)
205 let _ = opt_map!(opt, |x| x + 1).unwrap_or_else(|| 0);
208 /// Checks implementation of `FILTER_NEXT` lint.
211 let v = vec![3, 2, 1, 0, -1, -2, -3];
214 let _ = v.iter().filter(|&x| *x < 0).next();
217 let _ = v.iter().filter(|&x| {
222 // Check that hat we don't lint if the caller is not an `Iterator`.
223 let foo = IteratorFalsePositives { foo: 0 };
224 let _ = foo.filter().next();
227 /// Checks implementation of `SEARCH_IS_SOME` lint.
229 fn search_is_some() {
230 let v = vec![3, 2, 1, 0, -1, -2, -3];
232 // Check `find().is_some()`, single-line case.
233 let _ = v.iter().find(|&x| *x < 0).is_some();
235 // Check `find().is_some()`, multi-line case.
236 let _ = v.iter().find(|&x| {
241 // Check `position().is_some()`, single-line case.
242 let _ = v.iter().position(|&x| x < 0).is_some();
244 // Check `position().is_some()`, multi-line case.
245 let _ = v.iter().position(|&x| {
250 // Check `rposition().is_some()`, single-line case.
251 let _ = v.iter().rposition(|&x| x < 0).is_some();
253 // Check `rposition().is_some()`, multi-line case.
254 let _ = v.iter().rposition(|&x| {
259 // Check that we don't lint if the caller is not an `Iterator`.
260 let foo = IteratorFalsePositives { foo: 0 };
261 let _ = foo.find().is_some();
262 let _ = foo.position().is_some();
263 let _ = foo.rposition().is_some();
266 #[allow(clippy::similar_names)]
269 let _ = opt.unwrap();