]> git.lizzy.rs Git - rust.git/blob - tests/ui/eta.fixed
Auto merge of #68717 - petrochenkov:stabexpat, r=varkor
[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::PathBuf;
18
19 fn main() {
20     let a = Some(1u8).map(foo);
21     meta(foo);
22     let c = Some(1u8).map(|a| {1+2; foo}(a));
23     let d = Some(1u8).map(|a| foo((|b| foo2(b))(a))); //is adjusted?
24     all(&[1, 2, 3], &2, |x, y| below(x, y)); //is adjusted
25     unsafe {
26         Some(1u8).map(|a| unsafe_fn(a)); // unsafe fn
27     }
28
29     // See #815
30     let e = Some(1u8).map(|a| divergent(a));
31     let e = Some(1u8).map(generic);
32     let e = Some(1u8).map(generic);
33     // See #515
34     let a: Option<Box<dyn (::std::ops::Deref<Target = [i32]>)>> =
35         Some(vec![1i32, 2]).map(|v| -> Box<dyn (::std::ops::Deref<Target = [i32]>)> { Box::new(v) });
36 }
37
38 trait TestTrait {
39     fn trait_foo(self) -> bool;
40     fn trait_foo_ref(&self) -> bool;
41 }
42
43 struct TestStruct<'a> {
44     some_ref: &'a i32,
45 }
46
47 impl<'a> TestStruct<'a> {
48     fn foo(self) -> bool {
49         false
50     }
51     unsafe fn foo_unsafe(self) -> bool {
52         true
53     }
54 }
55
56 impl<'a> TestTrait for TestStruct<'a> {
57     fn trait_foo(self) -> bool {
58         false
59     }
60     fn trait_foo_ref(&self) -> bool {
61         false
62     }
63 }
64
65 impl<'a> std::ops::Deref for TestStruct<'a> {
66     type Target = char;
67     fn deref(&self) -> &char {
68         &'a'
69     }
70 }
71
72 fn test_redundant_closures_containing_method_calls() {
73     let i = 10;
74     let e = Some(TestStruct { some_ref: &i }).map(TestStruct::foo);
75     let e = Some(TestStruct { some_ref: &i }).map(TestStruct::foo);
76     let e = Some(TestStruct { some_ref: &i }).map(TestTrait::trait_foo);
77     let e = Some(TestStruct { some_ref: &i }).map(|a| a.trait_foo_ref());
78     let e = Some(TestStruct { some_ref: &i }).map(TestTrait::trait_foo);
79     let e = Some(&mut vec![1, 2, 3]).map(std::vec::Vec::clear);
80     let e = Some(&mut vec![1, 2, 3]).map(std::vec::Vec::clear);
81     unsafe {
82         let e = Some(TestStruct { some_ref: &i }).map(|a| a.foo_unsafe());
83     }
84     let e = Some("str").map(std::string::ToString::to_string);
85     let e = Some("str").map(str::to_string);
86     let e = Some('a').map(char::to_uppercase);
87     let e = Some('a').map(char::to_uppercase);
88     let e: std::vec::Vec<usize> = vec!['a', 'b', 'c'].iter().map(|c| c.len_utf8()).collect();
89     let e: std::vec::Vec<char> = vec!['a', 'b', 'c'].iter().map(char::to_ascii_uppercase).collect();
90     let e: std::vec::Vec<char> = vec!['a', 'b', 'c'].iter().map(char::to_ascii_uppercase).collect();
91     let p = Some(PathBuf::new());
92     let e = p.as_ref().and_then(|s| s.to_str());
93     let c = Some(TestStruct { some_ref: &i })
94         .as_ref()
95         .map(|c| c.to_ascii_uppercase());
96
97     fn test_different_borrow_levels<T>(t: &[&T])
98     where
99         T: TestTrait,
100     {
101         t.iter().filter(|x| x.trait_foo_ref());
102         t.iter().map(|x| x.trait_foo_ref());
103     }
104
105     let mut some = Some(|x| x * x);
106     let arr = [Ok(1), Err(2)];
107     let _: Vec<_> = arr.iter().map(|x| x.map_err(|e| some.take().unwrap()(e))).collect();
108 }
109
110 struct Thunk<T>(Box<dyn FnMut() -> T>);
111
112 impl<T> Thunk<T> {
113     fn new<F: 'static + FnOnce() -> T>(f: F) -> Thunk<T> {
114         let mut option = Some(f);
115         // This should not trigger redundant_closure (#1439)
116         Thunk(Box::new(move || option.take().unwrap()()))
117     }
118
119     fn unwrap(self) -> T {
120         let Thunk(mut f) = self;
121         f()
122     }
123 }
124
125 fn foobar() {
126     let thunk = Thunk::new(|| println!("Hello, world!"));
127     thunk.unwrap()
128 }
129
130 fn meta<F>(f: F)
131 where
132     F: Fn(u8),
133 {
134     f(1u8)
135 }
136
137 fn foo(_: u8) {}
138
139 fn foo2(_: u8) -> u8 {
140     1u8
141 }
142
143 fn all<X, F>(x: &[X], y: &X, f: F) -> bool
144 where
145     F: Fn(&X, &X) -> bool,
146 {
147     x.iter().all(|e| f(e, y))
148 }
149
150 fn below(x: &u8, y: &u8) -> bool {
151     x < y
152 }
153
154 unsafe fn unsafe_fn(_: u8) {}
155
156 fn divergent(_: u8) -> ! {
157     unimplemented!()
158 }
159
160 fn generic<T>(_: T) -> u8 {
161     0
162 }
163
164 fn passes_fn_mut(mut x: Box<dyn FnMut()>) {
165     requires_fn_once(|| x());
166 }
167 fn requires_fn_once<T: FnOnce()>(_: T) {}
168
169 fn test_redundant_closure_with_function_pointer() {
170     type FnPtrType = fn(u8);
171     let foo_ptr: FnPtrType = foo;
172     let a = Some(1u8).map(foo_ptr);
173 }
174
175 fn test_redundant_closure_with_another_closure() {
176     let closure = |a| println!("{}", a);
177     let a = Some(1u8).map(closure);
178 }
179
180 fn make_lazy(f: impl Fn() -> fn(u8) -> u8) -> impl Fn(u8) -> u8 {
181     // Currently f is called when result of make_lazy is called.
182     // If the closure is removed, f will be called when make_lazy itself is
183     // called. This changes semantics, so the closure must stay.
184     Box::new(move |x| f()(x))
185 }
186
187 fn call<F: FnOnce(&mut String) -> String>(f: F) -> String {
188     f(&mut "Hello".to_owned())
189 }
190 fn test_difference_in_mutability() {
191     call(|s| s.clone());
192 }
193
194 struct Bar;
195 impl std::ops::Deref for Bar {
196     type Target = str;
197     fn deref(&self) -> &str {
198         "hi"
199     }
200 }
201
202 fn test_deref_with_trait_method() {
203     let _ = [Bar].iter().map(|s| s.to_string()).collect::<Vec<_>>();
204 }