4 clippy::redundant_closure_call,
5 clippy::many_single_char_names,
6 clippy::needless_pass_by_value,
7 clippy::option_map_unit_fn,
8 clippy::trivially_copy_pass_by_ref
10 #![warn(clippy::redundant_closure, clippy::needless_borrow)]
12 use std::path::PathBuf;
15 let a = Some(1u8).map(|a| foo(a));
17 let c = Some(1u8).map(|a| {1+2; foo}(a));
18 let d = Some(1u8).map(|a| foo((|b| foo2(b))(a))); //is adjusted?
19 all(&[1, 2, 3], &&2, |x, y| below(x, y)); //is adjusted
21 Some(1u8).map(|a| unsafe_fn(a)); // unsafe fn
25 let e = Some(1u8).map(|a| divergent(a));
26 let e = Some(1u8).map(|a| generic(a));
27 let e = Some(1u8).map(generic);
29 let a: Option<Box<::std::ops::Deref<Target = [i32]>>> =
30 Some(vec![1i32, 2]).map(|v| -> Box<::std::ops::Deref<Target = [i32]>> { Box::new(v) });
35 fn trait_foo(self) -> bool;
36 fn trait_foo_ref(&self) -> bool;
39 struct TestStruct<'a> {
43 impl<'a> TestStruct<'a> {
44 fn foo(self) -> bool { false }
45 unsafe fn foo_unsafe(self) -> bool { true }
48 impl<'a> TestTrait for TestStruct<'a> {
49 fn trait_foo(self) -> bool { false }
50 fn trait_foo_ref(&self) -> bool { false }
53 impl<'a> std::ops::Deref for TestStruct<'a> {
55 fn deref(&self) -> &char { &'a' }
58 fn test_redundant_closures_containing_method_calls() {
60 let e = Some(TestStruct{some_ref: &i}).map(|a| a.foo());
61 let e = Some(TestStruct{some_ref: &i}).map(TestStruct::foo);
62 let e = Some(TestStruct{some_ref: &i}).map(|a| a.trait_foo());
63 let e = Some(TestStruct{some_ref: &i}).map(|a| a.trait_foo_ref());
64 let e = Some(TestStruct{some_ref: &i}).map(TestTrait::trait_foo);
65 let e = Some(&mut vec!(1,2,3)).map(|v| v.clear());
66 let e = Some(&mut vec!(1,2,3)).map(std::vec::Vec::clear);
68 let e = Some(TestStruct{some_ref: &i}).map(|a| a.foo_unsafe());
70 let e = Some("str").map(|s| s.to_string());
71 let e = Some("str").map(str::to_string);
72 let e = Some('a').map(|s| s.to_uppercase());
73 let e = Some('a').map(char::to_uppercase);
74 let e: std::vec::Vec<usize> = vec!('a','b','c').iter().map(|c| c.len_utf8()).collect();
75 let e: std::vec::Vec<char> = vec!('a','b','c').iter().map(|c| c.to_ascii_uppercase()).collect();
76 let e: std::vec::Vec<char> = vec!('a','b','c').iter().map(char::to_ascii_uppercase).collect();
77 let p = Some(PathBuf::new());
78 let e = p.as_ref().and_then(|s| s.to_str());
79 //let e = p.as_ref().and_then(std::path::Path::to_str);
80 let c = Some(TestStruct{some_ref: &i}).as_ref().map(|c| c.to_ascii_uppercase());
81 //let c = Some(TestStruct{some_ref: &i}).as_ref().map(char::to_ascii_uppercase);
93 fn foo2(_: u8) -> u8 {
97 fn all<X, F>(x: &[X], y: &X, f: F) -> bool
99 F: Fn(&X, &X) -> bool,
101 x.iter().all(|e| f(e, y))
104 fn below(x: &u8, y: &u8) -> bool {
108 unsafe fn unsafe_fn(_: u8) {}
110 fn divergent(_: u8) -> ! {
114 fn generic<T>(_: T) -> u8 {