6 clippy::redundant_closure_call,
7 clippy::needless_pass_by_value,
8 clippy::option_map_unit_fn
11 clippy::redundant_closure,
12 clippy::redundant_closure_for_method_calls,
13 clippy::needless_borrow
16 use std::path::{Path, PathBuf};
24 macro_rules! closure_mac {
31 let a = Some(1u8).map(|a| foo(a));
32 let c = Some(1u8).map(|a| {1+2; foo}(a));
33 true.then(|| mac!()); // don't lint function in macro expansion
34 Some(1).map(closure_mac!()); // don't lint closure in macro expansion
35 let _: Option<Vec<u8>> = true.then(|| vec![]); // special case vec!
36 let d = Some(1u8).map(|a| foo((|b| foo2(b))(a))); //is adjusted?
37 all(&[1, 2, 3], &&2, |x, y| below(x, y)); //is adjusted
39 Some(1u8).map(|a| unsafe_fn(a)); // unsafe fn
43 let e = Some(1u8).map(|a| divergent(a));
44 let e = Some(1u8).map(|a| generic(a));
45 let e = Some(1u8).map(generic);
47 let a: Option<Box<dyn (::std::ops::Deref<Target = [i32]>)>> =
48 Some(vec![1i32, 2]).map(|v| -> Box<dyn (::std::ops::Deref<Target = [i32]>)> { Box::new(v) });
51 let _: Option<Vec<u32>> = Some(0).map(|_| vec![]);
55 fn trait_foo(self) -> bool;
56 fn trait_foo_ref(&self) -> bool;
59 struct TestStruct<'a> {
63 impl<'a> TestStruct<'a> {
64 fn foo(self) -> bool {
67 unsafe fn foo_unsafe(self) -> bool {
72 impl<'a> TestTrait for TestStruct<'a> {
73 fn trait_foo(self) -> bool {
76 fn trait_foo_ref(&self) -> bool {
81 impl<'a> std::ops::Deref for TestStruct<'a> {
83 fn deref(&self) -> &char {
88 fn test_redundant_closures_containing_method_calls() {
90 let e = Some(TestStruct { some_ref: &i }).map(|a| a.foo());
91 let e = Some(TestStruct { some_ref: &i }).map(|a| a.trait_foo());
92 let e = Some(TestStruct { some_ref: &i }).map(|a| a.trait_foo_ref());
93 let e = Some(&mut vec![1, 2, 3]).map(|v| v.clear());
95 let e = Some(TestStruct { some_ref: &i }).map(|a| a.foo_unsafe());
97 let e = Some("str").map(|s| s.to_string());
98 let e = Some('a').map(|s| s.to_uppercase());
99 let e: std::vec::Vec<usize> = vec!['a', 'b', 'c'].iter().map(|c| c.len_utf8()).collect();
100 let e: std::vec::Vec<char> = vec!['a', 'b', 'c'].iter().map(|c| c.to_ascii_uppercase()).collect();
101 let e = Some(PathBuf::new()).as_ref().and_then(|s| s.to_str());
102 let c = Some(TestStruct { some_ref: &i })
104 .map(|c| c.to_ascii_uppercase());
106 fn test_different_borrow_levels<T>(t: &[&T])
110 t.iter().filter(|x| x.trait_foo_ref());
111 t.iter().map(|x| x.trait_foo_ref());
115 struct Thunk<T>(Box<dyn FnMut() -> T>);
118 fn new<F: 'static + FnOnce() -> T>(f: F) -> Thunk<T> {
119 let mut option = Some(f);
120 // This should not trigger redundant_closure (#1439)
121 Thunk(Box::new(move || option.take().unwrap()()))
124 fn unwrap(self) -> T {
125 let Thunk(mut f) = self;
131 let thunk = Thunk::new(|| println!("Hello, world!"));
137 fn foo2(_: u8) -> u8 {
141 fn all<X, F>(x: &[X], y: &X, f: F) -> bool
143 F: Fn(&X, &X) -> bool,
145 x.iter().all(|e| f(e, y))
148 fn below(x: &u8, y: &u8) -> bool {
152 unsafe fn unsafe_fn(_: u8) {}
154 fn divergent(_: u8) -> ! {
158 fn generic<T>(_: T) -> u8 {
162 fn passes_fn_mut(mut x: Box<dyn FnMut()>) {
163 requires_fn_once(|| x());
165 fn requires_fn_once<T: FnOnce()>(_: T) {}
167 fn test_redundant_closure_with_function_pointer() {
168 type FnPtrType = fn(u8);
169 let foo_ptr: FnPtrType = foo;
170 let a = Some(1u8).map(|a| foo_ptr(a));
173 fn test_redundant_closure_with_another_closure() {
174 let closure = |a| println!("{}", a);
175 let a = Some(1u8).map(|a| closure(a));
178 fn make_lazy(f: impl Fn() -> fn(u8) -> u8) -> impl Fn(u8) -> u8 {
179 // Currently f is called when result of make_lazy is called.
180 // If the closure is removed, f will be called when make_lazy itself is
181 // called. This changes semantics, so the closure must stay.
182 Box::new(move |x| f()(x))
185 fn call<F: FnOnce(&mut String) -> String>(f: F) -> String {
186 f(&mut "Hello".to_owned())
188 fn test_difference_in_mutability() {
193 impl std::ops::Deref for Bar {
195 fn deref(&self) -> &str {
200 fn test_deref_with_trait_method() {
201 let _ = [Bar].iter().map(|s| s.to_string()).collect::<Vec<_>>();
204 fn mutable_closure_used_again(x: Vec<i32>, y: Vec<i32>, z: Vec<i32>) {
205 let mut res = Vec::new();
206 let mut add_to_res = |n| res.push(n);
207 x.into_iter().for_each(|x| add_to_res(x));
208 y.into_iter().for_each(|x| add_to_res(x));
209 z.into_iter().for_each(|x| add_to_res(x));
212 fn mutable_closure_in_loop() {
214 let mut closure = |n| value += n;
216 Some(1).map(|n| closure(n));
220 fn late_bound_lifetimes() {
221 fn take_asref_path<P: AsRef<Path>>(path: P) {}
223 fn map_str<F>(thunk: F)
229 fn map_str_to_path<F>(thunk: F)
231 F: FnOnce(&str) -> &Path,
234 map_str(|s| take_asref_path(s));
235 map_str_to_path(|s| s.as_ref());
238 mod type_param_bound {
243 fn take<T: 'static>(_: T) {}
245 fn test<X: Trait>() {
246 // don't lint, but it's questionable that rust requires a cast
248 take(X::fun as fn());