]> git.lizzy.rs Git - rust.git/blob - tests/ui/eta.rs
Add two more tests for redundant_closure
[rust.git] / tests / ui / eta.rs
1 // run-rustfix
2
3 #![allow(
4     unused,
5     clippy::no_effect,
6     clippy::redundant_closure_call,
7     clippy::many_single_char_names,
8     clippy::needless_pass_by_value,
9     clippy::option_map_unit_fn,
10     clippy::trivially_copy_pass_by_ref
11 )]
12 #![warn(clippy::redundant_closure, clippy::needless_borrow)]
13
14 use std::path::PathBuf;
15
16 fn main() {
17     let a = Some(1u8).map(|a| foo(a));
18     meta(|a| foo(a));
19     let c = Some(1u8).map(|a| {1+2; foo}(a));
20     let d = Some(1u8).map(|a| foo((|b| foo2(b))(a))); //is adjusted?
21     all(&[1, 2, 3], &&2, |x, y| below(x, y)); //is adjusted
22     unsafe {
23         Some(1u8).map(|a| unsafe_fn(a)); // unsafe fn
24     }
25
26     // See #815
27     let e = Some(1u8).map(|a| divergent(a));
28     let e = Some(1u8).map(|a| generic(a));
29     let e = Some(1u8).map(generic);
30     // See #515
31     let a: Option<Box<::std::ops::Deref<Target = [i32]>>> =
32         Some(vec![1i32, 2]).map(|v| -> Box<::std::ops::Deref<Target = [i32]>> { Box::new(v) });
33 }
34
35 trait TestTrait {
36     fn trait_foo(self) -> bool;
37     fn trait_foo_ref(&self) -> bool;
38 }
39
40 struct TestStruct<'a> {
41     some_ref: &'a i32,
42 }
43
44 impl<'a> TestStruct<'a> {
45     fn foo(self) -> bool {
46         false
47     }
48     unsafe fn foo_unsafe(self) -> bool {
49         true
50     }
51 }
52
53 impl<'a> TestTrait for TestStruct<'a> {
54     fn trait_foo(self) -> bool {
55         false
56     }
57     fn trait_foo_ref(&self) -> bool {
58         false
59     }
60 }
61
62 impl<'a> std::ops::Deref for TestStruct<'a> {
63     type Target = char;
64     fn deref(&self) -> &char {
65         &'a'
66     }
67 }
68
69 fn test_redundant_closures_containing_method_calls() {
70     let i = 10;
71     let e = Some(TestStruct { some_ref: &i }).map(|a| a.foo());
72     let e = Some(TestStruct { some_ref: &i }).map(TestStruct::foo);
73     let e = Some(TestStruct { some_ref: &i }).map(|a| a.trait_foo());
74     let e = Some(TestStruct { some_ref: &i }).map(|a| a.trait_foo_ref());
75     let e = Some(TestStruct { some_ref: &i }).map(TestTrait::trait_foo);
76     let e = Some(&mut vec![1, 2, 3]).map(|v| v.clear());
77     let e = Some(&mut vec![1, 2, 3]).map(std::vec::Vec::clear);
78     unsafe {
79         let e = Some(TestStruct { some_ref: &i }).map(|a| a.foo_unsafe());
80     }
81     let e = Some("str").map(|s| s.to_string());
82     let e = Some("str").map(str::to_string);
83     let e = Some('a').map(|s| s.to_uppercase());
84     let e = Some('a').map(char::to_uppercase);
85     let e: std::vec::Vec<usize> = vec!['a', 'b', 'c'].iter().map(|c| c.len_utf8()).collect();
86     let e: std::vec::Vec<char> = vec!['a', 'b', 'c'].iter().map(|c| c.to_ascii_uppercase()).collect();
87     let e: std::vec::Vec<char> = vec!['a', 'b', 'c'].iter().map(char::to_ascii_uppercase).collect();
88     let p = Some(PathBuf::new());
89     let e = p.as_ref().and_then(|s| s.to_str());
90     let c = Some(TestStruct { some_ref: &i })
91         .as_ref()
92         .map(|c| c.to_ascii_uppercase());
93
94     fn test_different_borrow_levels<T>(t: &[&T])
95     where
96         T: TestTrait,
97     {
98         t.iter().filter(|x| x.trait_foo_ref());
99         t.iter().map(|x| x.trait_foo_ref());
100     }
101
102     let mut some = Some(|x| x * x);
103     let arr = [Ok(1), Err(2)];
104     let _: Vec<_> = arr.iter().map(|x| x.map_err(|e| some.take().unwrap()(e))).collect();
105 }
106
107 struct Thunk<T>(Box<FnMut() -> T>);
108
109 impl<T> Thunk<T> {
110     fn new<F: 'static + FnOnce() -> T>(f: F) -> Thunk<T> {
111         let mut option = Some(f);
112         // This should not trigger redundant_closure (#1439)
113         Thunk(Box::new(move || option.take().unwrap()()))
114     }
115
116     fn unwrap(self) -> T {
117         let Thunk(mut f) = self;
118         f()
119     }
120 }
121
122 fn foobar() {
123     let thunk = Thunk::new(|| println!("Hello, world!"));
124     thunk.unwrap()
125 }
126
127 fn meta<F>(f: F)
128 where
129     F: Fn(u8),
130 {
131     f(1u8)
132 }
133
134 fn foo(_: u8) {}
135
136 fn foo2(_: u8) -> u8 {
137     1u8
138 }
139
140 fn all<X, F>(x: &[X], y: &X, f: F) -> bool
141 where
142     F: Fn(&X, &X) -> bool,
143 {
144     x.iter().all(|e| f(e, y))
145 }
146
147 fn below(x: &u8, y: &u8) -> bool {
148     x < y
149 }
150
151 unsafe fn unsafe_fn(_: u8) {}
152
153 fn divergent(_: u8) -> ! {
154     unimplemented!()
155 }
156
157 fn generic<T>(_: T) -> u8 {
158     0
159 }
160
161 fn passes_fn_mut(mut x: Box<dyn FnMut()>) {
162     requires_fn_once(|| x());
163 }
164 fn requires_fn_once<T: FnOnce()>(_: T) {}
165
166 fn test_redundant_closure_with_function_pointer() {
167     type FnPtrType = fn(u8);
168     let foo_ptr: FnPtrType = foo;
169     let a = Some(1u8).map(|a| foo_ptr(a));
170 }
171
172 fn test_redundant_closure_with_another_closure() {
173     let closure = |a| println!("{}", a);
174     let a = Some(1u8).map(|a| closure(a));
175 }