6 clippy::redundant_closure_call,
7 clippy::many_single_char_names,
8 clippy::needless_pass_by_value,
9 clippy::option_map_unit_fn
12 clippy::redundant_closure,
13 clippy::redundant_closure_for_method_calls,
14 clippy::needless_borrow
17 use std::path::{Path, PathBuf};
25 macro_rules! closure_mac {
32 let a = Some(1u8).map(foo);
33 let c = Some(1u8).map(|a| {1+2; foo}(a));
34 true.then(|| mac!()); // don't lint function in macro expansion
35 Some(1).map(closure_mac!()); // don't lint closure in macro expansion
36 let _: Option<Vec<u8>> = true.then(std::vec::Vec::new); // special case vec!
37 let d = Some(1u8).map(|a| foo(foo2(a))); //is adjusted?
38 all(&[1, 2, 3], &2, below); //is adjusted
40 Some(1u8).map(|a| unsafe_fn(a)); // unsafe fn
44 let e = Some(1u8).map(divergent);
45 let e = Some(1u8).map(generic);
46 let e = Some(1u8).map(generic);
48 let a: Option<Box<dyn (::std::ops::Deref<Target = [i32]>)>> =
49 Some(vec![1i32, 2]).map(|v| -> Box<dyn (::std::ops::Deref<Target = [i32]>)> { Box::new(v) });
52 let _: Option<Vec<u32>> = Some(0).map(|_| vec![]);
56 fn trait_foo(self) -> bool;
57 fn trait_foo_ref(&self) -> bool;
60 struct TestStruct<'a> {
64 impl<'a> TestStruct<'a> {
65 fn foo(self) -> bool {
68 unsafe fn foo_unsafe(self) -> bool {
73 impl<'a> TestTrait for TestStruct<'a> {
74 fn trait_foo(self) -> bool {
77 fn trait_foo_ref(&self) -> bool {
82 impl<'a> std::ops::Deref for TestStruct<'a> {
84 fn deref(&self) -> &char {
89 fn test_redundant_closures_containing_method_calls() {
91 let e = Some(TestStruct { some_ref: &i }).map(TestStruct::foo);
92 let e = Some(TestStruct { some_ref: &i }).map(TestTrait::trait_foo);
93 let e = Some(TestStruct { some_ref: &i }).map(|a| a.trait_foo_ref());
94 let e = Some(&mut vec![1, 2, 3]).map(std::vec::Vec::clear);
96 let e = Some(TestStruct { some_ref: &i }).map(|a| a.foo_unsafe());
98 let e = Some("str").map(std::string::ToString::to_string);
99 let e = Some('a').map(char::to_uppercase);
100 let e: std::vec::Vec<usize> = vec!['a', 'b', 'c'].iter().map(|c| c.len_utf8()).collect();
101 let e: std::vec::Vec<char> = vec!['a', 'b', 'c'].iter().map(char::to_ascii_uppercase).collect();
102 let e = Some(PathBuf::new()).as_ref().and_then(|s| s.to_str());
103 let c = Some(TestStruct { some_ref: &i })
105 .map(|c| c.to_ascii_uppercase());
107 fn test_different_borrow_levels<T>(t: &[&T])
111 t.iter().filter(|x| x.trait_foo_ref());
112 t.iter().map(|x| x.trait_foo_ref());
116 struct Thunk<T>(Box<dyn FnMut() -> T>);
119 fn new<F: 'static + FnOnce() -> T>(f: F) -> Thunk<T> {
120 let mut option = Some(f);
121 // This should not trigger redundant_closure (#1439)
122 Thunk(Box::new(move || option.take().unwrap()()))
125 fn unwrap(self) -> T {
126 let Thunk(mut f) = self;
132 let thunk = Thunk::new(|| println!("Hello, world!"));
138 fn foo2(_: u8) -> u8 {
142 fn all<X, F>(x: &[X], y: &X, f: F) -> bool
144 F: Fn(&X, &X) -> bool,
146 x.iter().all(|e| f(e, y))
149 fn below(x: &u8, y: &u8) -> bool {
153 unsafe fn unsafe_fn(_: u8) {}
155 fn divergent(_: u8) -> ! {
159 fn generic<T>(_: T) -> u8 {
163 fn passes_fn_mut(mut x: Box<dyn FnMut()>) {
166 fn requires_fn_once<T: FnOnce()>(_: T) {}
168 fn test_redundant_closure_with_function_pointer() {
169 type FnPtrType = fn(u8);
170 let foo_ptr: FnPtrType = foo;
171 let a = Some(1u8).map(foo_ptr);
174 fn test_redundant_closure_with_another_closure() {
175 let closure = |a| println!("{}", a);
176 let a = Some(1u8).map(closure);
179 fn make_lazy(f: impl Fn() -> fn(u8) -> u8) -> impl Fn(u8) -> u8 {
180 // Currently f is called when result of make_lazy is called.
181 // If the closure is removed, f will be called when make_lazy itself is
182 // called. This changes semantics, so the closure must stay.
183 Box::new(move |x| f()(x))
186 fn call<F: FnOnce(&mut String) -> String>(f: F) -> String {
187 f(&mut "Hello".to_owned())
189 fn test_difference_in_mutability() {
194 impl std::ops::Deref for Bar {
196 fn deref(&self) -> &str {
201 fn test_deref_with_trait_method() {
202 let _ = [Bar].iter().map(|s| s.to_string()).collect::<Vec<_>>();
205 fn mutable_closure_used_again(x: Vec<i32>, y: Vec<i32>, z: Vec<i32>) {
206 let mut res = Vec::new();
207 let mut add_to_res = |n| res.push(n);
208 x.into_iter().for_each(&mut add_to_res);
209 y.into_iter().for_each(&mut add_to_res);
210 z.into_iter().for_each(add_to_res);
213 fn mutable_closure_in_loop() {
215 let mut closure = |n| value += n;
217 Some(1).map(&mut closure);
221 fn late_bound_lifetimes() {
222 fn take_asref_path<P: AsRef<Path>>(path: P) {}
224 fn map_str<F>(thunk: F)
230 fn map_str_to_path<F>(thunk: F)
232 F: FnOnce(&str) -> &Path,
235 map_str(|s| take_asref_path(s));
236 map_str_to_path(std::convert::AsRef::as_ref);
239 mod type_param_bound {
244 fn take<T: 'static>(_: T) {}
246 fn test<X: Trait>() {
247 // don't lint, but it's questionable that rust requires a cast
249 take(X::fun as fn());