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