]> git.lizzy.rs Git - rust.git/blob - tests/ui/eta.rs
Backport #4101
[rust.git] / tests / ui / eta.rs
1 #![allow(
2     unused,
3     clippy::no_effect,
4     clippy::redundant_closure_call,
5     clippy::many_single_char_names,
6     clippy::needless_pass_by_value,
7     clippy::option_map_unit_fn,
8     clippy::trivially_copy_pass_by_ref
9 )]
10 #![warn(
11     clippy::redundant_closure,
12     clippy::redundant_closure_for_method_calls,
13     clippy::needless_borrow
14 )]
15
16 use std::path::PathBuf;
17
18 fn main() {
19     let a = Some(1u8).map(|a| foo(a));
20     meta(|a| foo(a));
21     let c = Some(1u8).map(|a| {1+2; foo}(a));
22     let d = Some(1u8).map(|a| foo((|b| foo2(b))(a))); //is adjusted?
23     all(&[1, 2, 3], &&2, |x, y| below(x, y)); //is adjusted
24     unsafe {
25         Some(1u8).map(|a| unsafe_fn(a)); // unsafe fn
26     }
27
28     // See #815
29     let e = Some(1u8).map(|a| divergent(a));
30     let e = Some(1u8).map(|a| generic(a));
31     let e = Some(1u8).map(generic);
32     // See #515
33     let a: Option<Box<::std::ops::Deref<Target = [i32]>>> =
34         Some(vec![1i32, 2]).map(|v| -> Box<::std::ops::Deref<Target = [i32]>> { Box::new(v) });
35 }
36
37 trait TestTrait {
38     fn trait_foo(self) -> bool;
39     fn trait_foo_ref(&self) -> bool;
40 }
41
42 struct TestStruct<'a> {
43     some_ref: &'a i32,
44 }
45
46 impl<'a> TestStruct<'a> {
47     fn foo(self) -> bool {
48         false
49     }
50     unsafe fn foo_unsafe(self) -> bool {
51         true
52     }
53 }
54
55 impl<'a> TestTrait for TestStruct<'a> {
56     fn trait_foo(self) -> bool {
57         false
58     }
59     fn trait_foo_ref(&self) -> bool {
60         false
61     }
62 }
63
64 impl<'a> std::ops::Deref for TestStruct<'a> {
65     type Target = char;
66     fn deref(&self) -> &char {
67         &'a'
68     }
69 }
70
71 fn test_redundant_closures_containing_method_calls() {
72     let i = 10;
73     let e = Some(TestStruct { some_ref: &i }).map(|a| a.foo());
74     let e = Some(TestStruct { some_ref: &i }).map(TestStruct::foo);
75     let e = Some(TestStruct { some_ref: &i }).map(|a| a.trait_foo());
76     let e = Some(TestStruct { some_ref: &i }).map(|a| a.trait_foo_ref());
77     let e = Some(TestStruct { some_ref: &i }).map(TestTrait::trait_foo);
78     let e = Some(&mut vec![1, 2, 3]).map(|v| v.clear());
79     let e = Some(&mut vec![1, 2, 3]).map(std::vec::Vec::clear);
80     unsafe {
81         let e = Some(TestStruct { some_ref: &i }).map(|a| a.foo_unsafe());
82     }
83     let e = Some("str").map(|s| s.to_string());
84     let e = Some("str").map(str::to_string);
85     let e = Some('a').map(|s| s.to_uppercase());
86     let e = Some('a').map(char::to_uppercase);
87     let e: std::vec::Vec<usize> = vec!['a', 'b', 'c'].iter().map(|c| c.len_utf8()).collect();
88     let e: std::vec::Vec<char> = vec!['a', 'b', 'c'].iter().map(|c| c.to_ascii_uppercase()).collect();
89     let e: std::vec::Vec<char> = vec!['a', 'b', 'c'].iter().map(char::to_ascii_uppercase).collect();
90     let p = Some(PathBuf::new());
91     let e = p.as_ref().and_then(|s| s.to_str());
92     let c = Some(TestStruct { some_ref: &i })
93         .as_ref()
94         .map(|c| c.to_ascii_uppercase());
95
96     fn test_different_borrow_levels<T>(t: &[&T])
97     where
98         T: TestTrait,
99     {
100         t.iter().filter(|x| x.trait_foo_ref());
101         t.iter().map(|x| x.trait_foo_ref());
102     }
103 }
104
105 fn meta<F>(f: F)
106 where
107     F: Fn(u8),
108 {
109     f(1u8)
110 }
111
112 fn foo(_: u8) {}
113
114 fn foo2(_: u8) -> u8 {
115     1u8
116 }
117
118 fn all<X, F>(x: &[X], y: &X, f: F) -> bool
119 where
120     F: Fn(&X, &X) -> bool,
121 {
122     x.iter().all(|e| f(e, y))
123 }
124
125 fn below(x: &u8, y: &u8) -> bool {
126     x < y
127 }
128
129 unsafe fn unsafe_fn(_: u8) {}
130
131 fn divergent(_: u8) -> ! {
132     unimplemented!()
133 }
134
135 fn generic<T>(_: T) -> u8 {
136     0
137 }