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