Skip to content

Commit b7c330f

Browse files
committed
Auto merge of #10905 - y21:issue10684, r=Alexendoo
[`redundant_closure`]: special case inclusive ranges Fixes #10684. `x..=y` ranges need a bit of special handling in this lint because it desugars to a call to the lang item `RangeInclusiveNew`, where the callee span would be the same as the range expression itself, so the suggestion looked a bit weird. It now correctly suggests `RangeInclusive::new`. changelog: [`redundant_closure`]: special case `RangeInclusive`
2 parents 60258b0 + 9ff34ac commit b7c330f

File tree

4 files changed

+41
-21
lines changed

4 files changed

+41
-21
lines changed

clippy_lints/src/eta_reduction.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ impl<'tcx> LateLintPass<'tcx> for EtaReduction {
120120
if !is_type_diagnostic_item(cx, callee_ty_unadjusted, sym::Arc);
121121
if !is_type_diagnostic_item(cx, callee_ty_unadjusted, sym::Rc);
122122
if let ty::Closure(_, substs) = *closure_ty.kind();
123+
// Don't lint if this is an inclusive range expression.
124+
// They desugar to a call to `RangeInclusiveNew` which would have odd suggestions. (#10684)
125+
if !matches!(higher::Range::hir(body.value), Some(higher::Range {
126+
start: Some(_),
127+
end: Some(_),
128+
limits: rustc_ast::RangeLimits::Closed
129+
}));
123130
then {
124131
span_lint_and_then(cx, REDUNDANT_CLOSURE, expr.span, "redundant closure", |diag| {
125132
if let Some(mut snippet) = snippet_opt(cx, callee.span) {
@@ -136,6 +143,7 @@ impl<'tcx> LateLintPass<'tcx> for EtaReduction {
136143
// Mutable closure is used after current expr; we cannot consume it.
137144
snippet = format!("&mut {snippet}");
138145
}
146+
139147
diag.span_suggestion(
140148
expr.span,
141149
"replace the closure with the function itself",

tests/ui/eta.fixed

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ fn main() {
4646

4747
// issue #7224
4848
let _: Option<Vec<u32>> = Some(0).map(|_| vec![]);
49+
50+
// issue #10684
51+
fn test<T>(x: impl Fn(usize, usize) -> T) -> T {
52+
x(1, 2)
53+
}
54+
test(|start, end| start..=end);
4955
}
5056

5157
trait TestTrait {

tests/ui/eta.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ fn main() {
4646

4747
// issue #7224
4848
let _: Option<Vec<u32>> = Some(0).map(|_| vec![]);
49+
50+
// issue #10684
51+
fn test<T>(x: impl Fn(usize, usize) -> T) -> T {
52+
x(1, 2)
53+
}
54+
test(|start, end| start..=end);
4955
}
5056

5157
trait TestTrait {

tests/ui/eta.stderr

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,129 +31,129 @@ LL | let e = Some(1u8).map(|a| generic(a));
3131
| ^^^^^^^^^^^^^^ help: replace the closure with the function itself: `generic`
3232

3333
error: redundant closure
34-
--> $DIR/eta.rs:87:51
34+
--> $DIR/eta.rs:93:51
3535
|
3636
LL | let e = Some(TestStruct { some_ref: &i }).map(|a| a.foo());
3737
| ^^^^^^^^^^^ help: replace the closure with the method itself: `TestStruct::foo`
3838
|
3939
= note: `-D clippy::redundant-closure-for-method-calls` implied by `-D warnings`
4040

4141
error: redundant closure
42-
--> $DIR/eta.rs:88:51
42+
--> $DIR/eta.rs:94:51
4343
|
4444
LL | let e = Some(TestStruct { some_ref: &i }).map(|a| a.trait_foo());
4545
| ^^^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `TestTrait::trait_foo`
4646

4747
error: redundant closure
48-
--> $DIR/eta.rs:90:42
48+
--> $DIR/eta.rs:96:42
4949
|
5050
LL | let e = Some(&mut vec![1, 2, 3]).map(|v| v.clear());
5151
| ^^^^^^^^^^^^^ help: replace the closure with the method itself: `std::vec::Vec::clear`
5252

5353
error: redundant closure
54-
--> $DIR/eta.rs:94:29
54+
--> $DIR/eta.rs:100:29
5555
|
5656
LL | let e = Some("str").map(|s| s.to_string());
5757
| ^^^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `std::string::ToString::to_string`
5858

5959
error: redundant closure
60-
--> $DIR/eta.rs:95:27
60+
--> $DIR/eta.rs:101:27
6161
|
6262
LL | let e = Some('a').map(|s| s.to_uppercase());
6363
| ^^^^^^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `char::to_uppercase`
6464

6565
error: redundant closure
66-
--> $DIR/eta.rs:97:65
66+
--> $DIR/eta.rs:103:65
6767
|
6868
LL | let e: std::vec::Vec<char> = vec!['a', 'b', 'c'].iter().map(|c| c.to_ascii_uppercase()).collect();
6969
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `char::to_ascii_uppercase`
7070

7171
error: redundant closure
72-
--> $DIR/eta.rs:160:22
72+
--> $DIR/eta.rs:166:22
7373
|
7474
LL | requires_fn_once(|| x());
7575
| ^^^^^^ help: replace the closure with the function itself: `x`
7676

7777
error: redundant closure
78-
--> $DIR/eta.rs:167:27
78+
--> $DIR/eta.rs:173:27
7979
|
8080
LL | let a = Some(1u8).map(|a| foo_ptr(a));
8181
| ^^^^^^^^^^^^^^ help: replace the closure with the function itself: `foo_ptr`
8282

8383
error: redundant closure
84-
--> $DIR/eta.rs:172:27
84+
--> $DIR/eta.rs:178:27
8585
|
8686
LL | let a = Some(1u8).map(|a| closure(a));
8787
| ^^^^^^^^^^^^^^ help: replace the closure with the function itself: `closure`
8888

8989
error: redundant closure
90-
--> $DIR/eta.rs:204:28
90+
--> $DIR/eta.rs:210:28
9191
|
9292
LL | x.into_iter().for_each(|x| add_to_res(x));
9393
| ^^^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `&mut add_to_res`
9494

9595
error: redundant closure
96-
--> $DIR/eta.rs:205:28
96+
--> $DIR/eta.rs:211:28
9797
|
9898
LL | y.into_iter().for_each(|x| add_to_res(x));
9999
| ^^^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `&mut add_to_res`
100100

101101
error: redundant closure
102-
--> $DIR/eta.rs:206:28
102+
--> $DIR/eta.rs:212:28
103103
|
104104
LL | z.into_iter().for_each(|x| add_to_res(x));
105105
| ^^^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `add_to_res`
106106

107107
error: redundant closure
108-
--> $DIR/eta.rs:213:21
108+
--> $DIR/eta.rs:219:21
109109
|
110110
LL | Some(1).map(|n| closure(n));
111111
| ^^^^^^^^^^^^^^ help: replace the closure with the function itself: `&mut closure`
112112

113113
error: redundant closure
114-
--> $DIR/eta.rs:217:21
114+
--> $DIR/eta.rs:223:21
115115
|
116116
LL | Some(1).map(|n| in_loop(n));
117117
| ^^^^^^^^^^^^^^ help: replace the closure with the function itself: `in_loop`
118118

119119
error: redundant closure
120-
--> $DIR/eta.rs:310:18
120+
--> $DIR/eta.rs:316:18
121121
|
122122
LL | takes_fn_mut(|| f());
123123
| ^^^^^^ help: replace the closure with the function itself: `&mut f`
124124

125125
error: redundant closure
126-
--> $DIR/eta.rs:313:19
126+
--> $DIR/eta.rs:319:19
127127
|
128128
LL | takes_fn_once(|| f());
129129
| ^^^^^^ help: replace the closure with the function itself: `&mut f`
130130

131131
error: redundant closure
132-
--> $DIR/eta.rs:317:26
132+
--> $DIR/eta.rs:323:26
133133
|
134134
LL | move || takes_fn_mut(|| f_used_once())
135135
| ^^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `&mut f_used_once`
136136

137137
error: redundant closure
138-
--> $DIR/eta.rs:329:19
138+
--> $DIR/eta.rs:335:19
139139
|
140140
LL | array_opt.map(|a| a.as_slice());
141141
| ^^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `<[u8; 3]>::as_slice`
142142

143143
error: redundant closure
144-
--> $DIR/eta.rs:332:19
144+
--> $DIR/eta.rs:338:19
145145
|
146146
LL | slice_opt.map(|s| s.len());
147147
| ^^^^^^^^^^^ help: replace the closure with the method itself: `<[u8]>::len`
148148

149149
error: redundant closure
150-
--> $DIR/eta.rs:335:17
150+
--> $DIR/eta.rs:341:17
151151
|
152152
LL | ptr_opt.map(|p| p.is_null());
153153
| ^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `<*const usize>::is_null`
154154

155155
error: redundant closure
156-
--> $DIR/eta.rs:339:17
156+
--> $DIR/eta.rs:345:17
157157
|
158158
LL | dyn_opt.map(|d| d.method_on_dyn());
159159
| ^^^^^^^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `<dyn TestTrait>::method_on_dyn`

0 commit comments

Comments
 (0)