Skip to content

Commit 32dc02a

Browse files
committed
Add regression test for issue 13073
1 parent 37f4fbb commit 32dc02a

File tree

3 files changed

+86
-34
lines changed

3 files changed

+86
-34
lines changed

tests/ui/eta.fixed

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#![allow(unused)]
33
#![allow(
44
clippy::needless_borrow,
5+
clippy::needless_option_as_deref,
56
clippy::needless_pass_by_value,
67
clippy::no_effect,
78
clippy::option_map_unit_fn,
@@ -482,3 +483,19 @@ mod issue_12853 {
482483
x();
483484
}
484485
}
486+
487+
mod issue_13073 {
488+
fn get_default() -> Option<&'static str> {
489+
Some("foo")
490+
}
491+
492+
pub fn foo() {
493+
// shouldn't lint
494+
let bind: Option<String> = None;
495+
let _field = bind.as_deref().or_else(get_default).unwrap();
496+
let bind: Option<&'static str> = None;
497+
let _field = bind.as_deref().or_else(get_default).unwrap();
498+
// should lint
499+
let _field = bind.or_else(get_default).unwrap();
500+
}
501+
}

tests/ui/eta.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#![allow(unused)]
33
#![allow(
44
clippy::needless_borrow,
5+
clippy::needless_option_as_deref,
56
clippy::needless_pass_by_value,
67
clippy::no_effect,
78
clippy::option_map_unit_fn,
@@ -482,3 +483,19 @@ mod issue_12853 {
482483
x();
483484
}
484485
}
486+
487+
mod issue_13073 {
488+
fn get_default() -> Option<&'static str> {
489+
Some("foo")
490+
}
491+
492+
pub fn foo() {
493+
// shouldn't lint
494+
let bind: Option<String> = None;
495+
let _field = bind.as_deref().or_else(|| get_default()).unwrap();
496+
let bind: Option<&'static str> = None;
497+
let _field = bind.as_deref().or_else(|| get_default()).unwrap();
498+
// should lint
499+
let _field = bind.or_else(|| get_default()).unwrap();
500+
}
501+
}

tests/ui/eta.stderr

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: redundant closure
2-
--> tests/ui/eta.rs:29:27
2+
--> tests/ui/eta.rs:30:27
33
|
44
LL | let a = Some(1u8).map(|a| foo(a));
55
| ^^^^^^^^^^ help: replace the closure with the function itself: `foo`
@@ -8,31 +8,31 @@ LL | let a = Some(1u8).map(|a| foo(a));
88
= help: to override `-D warnings` add `#[allow(clippy::redundant_closure)]`
99

1010
error: redundant closure
11-
--> tests/ui/eta.rs:33:40
11+
--> tests/ui/eta.rs:34:40
1212
|
1313
LL | let _: Option<Vec<u8>> = true.then(|| vec![]); // special case vec!
1414
| ^^^^^^^^^ help: replace the closure with `Vec::new`: `std::vec::Vec::new`
1515

1616
error: redundant closure
17-
--> tests/ui/eta.rs:34:35
17+
--> tests/ui/eta.rs:35:35
1818
|
1919
LL | let d = Some(1u8).map(|a| foo((|b| foo2(b))(a))); //is adjusted?
2020
| ^^^^^^^^^^^^^ help: replace the closure with the function itself: `foo2`
2121

2222
error: redundant closure
23-
--> tests/ui/eta.rs:35:26
23+
--> tests/ui/eta.rs:36:26
2424
|
2525
LL | all(&[1, 2, 3], &&2, |x, y| below(x, y)); //is adjusted
2626
| ^^^^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `below`
2727

2828
error: redundant closure
29-
--> tests/ui/eta.rs:42:27
29+
--> tests/ui/eta.rs:43:27
3030
|
3131
LL | let e = Some(1u8).map(|a| generic(a));
3232
| ^^^^^^^^^^^^^^ help: replace the closure with the function itself: `generic`
3333

3434
error: redundant closure
35-
--> tests/ui/eta.rs:94:51
35+
--> tests/ui/eta.rs:95:51
3636
|
3737
LL | let e = Some(TestStruct { some_ref: &i }).map(|a| a.foo());
3838
| ^^^^^^^^^^^ help: replace the closure with the method itself: `TestStruct::foo`
@@ -41,166 +41,184 @@ LL | let e = Some(TestStruct { some_ref: &i }).map(|a| a.foo());
4141
= help: to override `-D warnings` add `#[allow(clippy::redundant_closure_for_method_calls)]`
4242

4343
error: redundant closure
44-
--> tests/ui/eta.rs:95:51
44+
--> tests/ui/eta.rs:96:51
4545
|
4646
LL | let e = Some(TestStruct { some_ref: &i }).map(|a| a.trait_foo());
4747
| ^^^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `TestTrait::trait_foo`
4848

4949
error: redundant closure
50-
--> tests/ui/eta.rs:97:42
50+
--> tests/ui/eta.rs:98:42
5151
|
5252
LL | let e = Some(&mut vec![1, 2, 3]).map(|v| v.clear());
5353
| ^^^^^^^^^^^^^ help: replace the closure with the method itself: `std::vec::Vec::clear`
5454

5555
error: redundant closure
56-
--> tests/ui/eta.rs:101:29
56+
--> tests/ui/eta.rs:102:29
5757
|
5858
LL | let e = Some("str").map(|s| s.to_string());
5959
| ^^^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `std::string::ToString::to_string`
6060

6161
error: redundant closure
62-
--> tests/ui/eta.rs:102:27
62+
--> tests/ui/eta.rs:103:27
6363
|
6464
LL | let e = Some('a').map(|s| s.to_uppercase());
6565
| ^^^^^^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `char::to_uppercase`
6666

6767
error: redundant closure
68-
--> tests/ui/eta.rs:104:65
68+
--> tests/ui/eta.rs:105:65
6969
|
7070
LL | let e: std::vec::Vec<char> = vec!['a', 'b', 'c'].iter().map(|c| c.to_ascii_uppercase()).collect();
7171
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `char::to_ascii_uppercase`
7272

7373
error: redundant closure
74-
--> tests/ui/eta.rs:167:22
74+
--> tests/ui/eta.rs:168:22
7575
|
7676
LL | requires_fn_once(|| x());
7777
| ^^^^^^ help: replace the closure with the function itself: `x`
7878

7979
error: redundant closure
80-
--> tests/ui/eta.rs:174:27
80+
--> tests/ui/eta.rs:175:27
8181
|
8282
LL | let a = Some(1u8).map(|a| foo_ptr(a));
8383
| ^^^^^^^^^^^^^^ help: replace the closure with the function itself: `foo_ptr`
8484

8585
error: redundant closure
86-
--> tests/ui/eta.rs:179:27
86+
--> tests/ui/eta.rs:180:27
8787
|
8888
LL | let a = Some(1u8).map(|a| closure(a));
8989
| ^^^^^^^^^^^^^^ help: replace the closure with the function itself: `closure`
9090

9191
error: redundant closure
92-
--> tests/ui/eta.rs:211:28
92+
--> tests/ui/eta.rs:212:28
9393
|
9494
LL | x.into_iter().for_each(|x| add_to_res(x));
9595
| ^^^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `&mut add_to_res`
9696

9797
error: redundant closure
98-
--> tests/ui/eta.rs:212:28
98+
--> tests/ui/eta.rs:213:28
9999
|
100100
LL | y.into_iter().for_each(|x| add_to_res(x));
101101
| ^^^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `&mut add_to_res`
102102

103103
error: redundant closure
104-
--> tests/ui/eta.rs:213:28
104+
--> tests/ui/eta.rs:214:28
105105
|
106106
LL | z.into_iter().for_each(|x| add_to_res(x));
107107
| ^^^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `add_to_res`
108108

109109
error: redundant closure
110-
--> tests/ui/eta.rs:220:21
110+
--> tests/ui/eta.rs:221:21
111111
|
112112
LL | Some(1).map(|n| closure(n));
113113
| ^^^^^^^^^^^^^^ help: replace the closure with the function itself: `&mut closure`
114114

115115
error: redundant closure
116-
--> tests/ui/eta.rs:224:21
116+
--> tests/ui/eta.rs:225:21
117117
|
118118
LL | Some(1).map(|n| in_loop(n));
119119
| ^^^^^^^^^^^^^^ help: replace the closure with the function itself: `in_loop`
120120

121121
error: redundant closure
122-
--> tests/ui/eta.rs:317:18
122+
--> tests/ui/eta.rs:318:18
123123
|
124124
LL | takes_fn_mut(|| f());
125125
| ^^^^^^ help: replace the closure with the function itself: `&mut f`
126126

127127
error: redundant closure
128-
--> tests/ui/eta.rs:320:19
128+
--> tests/ui/eta.rs:321:19
129129
|
130130
LL | takes_fn_once(|| f());
131131
| ^^^^^^ help: replace the closure with the function itself: `&mut f`
132132

133133
error: redundant closure
134-
--> tests/ui/eta.rs:324:26
134+
--> tests/ui/eta.rs:325:26
135135
|
136136
LL | move || takes_fn_mut(|| f_used_once())
137137
| ^^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `&mut f_used_once`
138138

139139
error: redundant closure
140-
--> tests/ui/eta.rs:336:19
140+
--> tests/ui/eta.rs:337:19
141141
|
142142
LL | array_opt.map(|a| a.as_slice());
143143
| ^^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `<[u8; 3]>::as_slice`
144144

145145
error: redundant closure
146-
--> tests/ui/eta.rs:339:19
146+
--> tests/ui/eta.rs:340:19
147147
|
148148
LL | slice_opt.map(|s| s.len());
149149
| ^^^^^^^^^^^ help: replace the closure with the method itself: `<[u8]>::len`
150150

151151
error: redundant closure
152-
--> tests/ui/eta.rs:342:17
152+
--> tests/ui/eta.rs:343:17
153153
|
154154
LL | ptr_opt.map(|p| p.is_null());
155155
| ^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `<*const usize>::is_null`
156156

157157
error: redundant closure
158-
--> tests/ui/eta.rs:346:17
158+
--> tests/ui/eta.rs:347:17
159159
|
160160
LL | dyn_opt.map(|d| d.method_on_dyn());
161161
| ^^^^^^^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `<dyn TestTrait>::method_on_dyn`
162162

163163
error: redundant closure
164-
--> tests/ui/eta.rs:406:19
164+
--> tests/ui/eta.rs:407:19
165165
|
166166
LL | let _ = f(&0, |x, y| f2(x, y));
167167
| ^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `f2`
168168

169169
error: redundant closure
170-
--> tests/ui/eta.rs:434:22
170+
--> tests/ui/eta.rs:435:22
171171
|
172172
LL | test.map(|t| t.method())
173173
| ^^^^^^^^^^^^^^ help: replace the closure with the method itself: `Test::method`
174174

175175
error: redundant closure
176-
--> tests/ui/eta.rs:438:22
176+
--> tests/ui/eta.rs:439:22
177177
|
178178
LL | test.map(|t| t.method())
179179
| ^^^^^^^^^^^^^^ help: replace the closure with the method itself: `super::Outer::method`
180180

181181
error: redundant closure
182-
--> tests/ui/eta.rs:451:18
182+
--> tests/ui/eta.rs:452:18
183183
|
184184
LL | test.map(|t| t.method())
185185
| ^^^^^^^^^^^^^^ help: replace the closure with the method itself: `test_mod::Test::method`
186186

187187
error: redundant closure
188-
--> tests/ui/eta.rs:458:30
188+
--> tests/ui/eta.rs:459:30
189189
|
190190
LL | test.map(|t| t.method())
191191
| ^^^^^^^^^^^^^^ help: replace the closure with the method itself: `crate::issue_10854::d::Test::method`
192192

193193
error: redundant closure
194-
--> tests/ui/eta.rs:477:38
194+
--> tests/ui/eta.rs:478:38
195195
|
196196
LL | let x = Box::new(|| None.map(|x| f(x)));
197197
| ^^^^^^^^ help: replace the closure with the function itself: `&f`
198198

199199
error: redundant closure
200-
--> tests/ui/eta.rs:481:38
200+
--> tests/ui/eta.rs:482:38
201201
|
202202
LL | let x = Box::new(|| None.map(|x| f(x)));
203203
| ^^^^^^^^ help: replace the closure with the function itself: `f`
204204

205-
error: aborting due to 33 previous errors
205+
error: redundant closure
206+
--> tests/ui/eta.rs:495:46
207+
|
208+
LL | let _field = bind.as_deref().or_else(|| get_default()).unwrap();
209+
| ^^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `get_default`
210+
211+
error: redundant closure
212+
--> tests/ui/eta.rs:497:46
213+
|
214+
LL | let _field = bind.as_deref().or_else(|| get_default()).unwrap();
215+
| ^^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `get_default`
216+
217+
error: redundant closure
218+
--> tests/ui/eta.rs:499:35
219+
|
220+
LL | let _field = bind.or_else(|| get_default()).unwrap();
221+
| ^^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `get_default`
222+
223+
error: aborting due to 36 previous errors
206224

0 commit comments

Comments
 (0)