]> git.lizzy.rs Git - rust.git/blob - tests/ui/eta.fixed
Auto merge of #7661 - camsteffen:eta, r=giraffate
[rust.git] / tests / ui / eta.fixed
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 )]
11 #![warn(
12     clippy::redundant_closure,
13     clippy::redundant_closure_for_method_calls,
14     clippy::needless_borrow
15 )]
16
17 use std::path::{Path, PathBuf};
18
19 macro_rules! mac {
20     () => {
21         foobar()
22     };
23 }
24
25 macro_rules! closure_mac {
26     () => {
27         |n| foo(n)
28     };
29 }
30
31 fn main() {
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
39     unsafe {
40         Some(1u8).map(|a| unsafe_fn(a)); // unsafe fn
41     }
42
43     // See #815
44     let e = Some(1u8).map(divergent);
45     let e = Some(1u8).map(generic);
46     let e = Some(1u8).map(generic);
47     // See #515
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) });
50
51     // issue #7224
52     let _: Option<Vec<u32>> = Some(0).map(|_| vec![]);
53 }
54
55 trait TestTrait {
56     fn trait_foo(self) -> bool;
57     fn trait_foo_ref(&self) -> bool;
58 }
59
60 struct TestStruct<'a> {
61     some_ref: &'a i32,
62 }
63
64 impl<'a> TestStruct<'a> {
65     fn foo(self) -> bool {
66         false
67     }
68     unsafe fn foo_unsafe(self) -> bool {
69         true
70     }
71 }
72
73 impl<'a> TestTrait for TestStruct<'a> {
74     fn trait_foo(self) -> bool {
75         false
76     }
77     fn trait_foo_ref(&self) -> bool {
78         false
79     }
80 }
81
82 impl<'a> std::ops::Deref for TestStruct<'a> {
83     type Target = char;
84     fn deref(&self) -> &char {
85         &'a'
86     }
87 }
88
89 fn test_redundant_closures_containing_method_calls() {
90     let i = 10;
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);
95     unsafe {
96         let e = Some(TestStruct { some_ref: &i }).map(|a| a.foo_unsafe());
97     }
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 })
104         .as_ref()
105         .map(|c| c.to_ascii_uppercase());
106
107     fn test_different_borrow_levels<T>(t: &[&T])
108     where
109         T: TestTrait,
110     {
111         t.iter().filter(|x| x.trait_foo_ref());
112         t.iter().map(|x| x.trait_foo_ref());
113     }
114 }
115
116 struct Thunk<T>(Box<dyn FnMut() -> T>);
117
118 impl<T> Thunk<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()()))
123     }
124
125     fn unwrap(self) -> T {
126         let Thunk(mut f) = self;
127         f()
128     }
129 }
130
131 fn foobar() {
132     let thunk = Thunk::new(|| println!("Hello, world!"));
133     thunk.unwrap()
134 }
135
136 fn foo(_: u8) {}
137
138 fn foo2(_: u8) -> u8 {
139     1u8
140 }
141
142 fn all<X, F>(x: &[X], y: &X, f: F) -> bool
143 where
144     F: Fn(&X, &X) -> bool,
145 {
146     x.iter().all(|e| f(e, y))
147 }
148
149 fn below(x: &u8, y: &u8) -> bool {
150     x < y
151 }
152
153 unsafe fn unsafe_fn(_: u8) {}
154
155 fn divergent(_: u8) -> ! {
156     unimplemented!()
157 }
158
159 fn generic<T>(_: T) -> u8 {
160     0
161 }
162
163 fn passes_fn_mut(mut x: Box<dyn FnMut()>) {
164     requires_fn_once(x);
165 }
166 fn requires_fn_once<T: FnOnce()>(_: T) {}
167
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);
172 }
173
174 fn test_redundant_closure_with_another_closure() {
175     let closure = |a| println!("{}", a);
176     let a = Some(1u8).map(closure);
177 }
178
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))
184 }
185
186 fn call<F: FnOnce(&mut String) -> String>(f: F) -> String {
187     f(&mut "Hello".to_owned())
188 }
189 fn test_difference_in_mutability() {
190     call(|s| s.clone());
191 }
192
193 struct Bar;
194 impl std::ops::Deref for Bar {
195     type Target = str;
196     fn deref(&self) -> &str {
197         "hi"
198     }
199 }
200
201 fn test_deref_with_trait_method() {
202     let _ = [Bar].iter().map(|s| s.to_string()).collect::<Vec<_>>();
203 }
204
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);
211 }
212
213 fn mutable_closure_in_loop() {
214     let mut value = 0;
215     let mut closure = |n| value += n;
216     for _ in 0..5 {
217         Some(1).map(&mut closure);
218     }
219 }
220
221 fn late_bound_lifetimes() {
222     fn take_asref_path<P: AsRef<Path>>(path: P) {}
223
224     fn map_str<F>(thunk: F)
225     where
226         F: FnOnce(&str),
227     {
228     }
229
230     fn map_str_to_path<F>(thunk: F)
231     where
232         F: FnOnce(&str) -> &Path,
233     {
234     }
235     map_str(|s| take_asref_path(s));
236     map_str_to_path(std::convert::AsRef::as_ref);
237 }
238
239 mod type_param_bound {
240     trait Trait {
241         fn fun();
242     }
243
244     fn take<T: 'static>(_: T) {}
245
246     fn test<X: Trait>() {
247         // don't lint, but it's questionable that rust requires a cast
248         take(|| X::fun());
249         take(X::fun as fn());
250     }
251 }