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