Skip to content

Commit 8787186

Browse files
committed
Auto merge of rust-lang#7263 - Jarcho:redundant_closure_macro, r=llogiq
Fix `redundant_closure` for `vec![]` macro in a closure with arguments fixes: rust-lang#7224 changelog: Fix `redundant_closure` for `vec![]` macro in a closure with arguments
2 parents f694f85 + 60dd2b6 commit 8787186

File tree

4 files changed

+27
-19
lines changed

4 files changed

+27
-19
lines changed

clippy_lints/src/eta_reduction.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,19 @@ fn check_closure(cx: &LateContext<'_>, expr: &Expr<'_>) {
9292
let ex = &body.value;
9393

9494
if ex.span.ctxt() != expr.span.ctxt() {
95-
if let Some(VecArgs::Vec(&[])) = higher::vec_macro(cx, ex) {
96-
// replace `|| vec![]` with `Vec::new`
97-
span_lint_and_sugg(
98-
cx,
99-
REDUNDANT_CLOSURE,
100-
expr.span,
101-
"redundant closure",
102-
"replace the closure with `Vec::new`",
103-
"std::vec::Vec::new".into(),
104-
Applicability::MachineApplicable,
105-
);
95+
if decl.inputs.is_empty() {
96+
if let Some(VecArgs::Vec(&[])) = higher::vec_macro(cx, ex) {
97+
// replace `|| vec![]` with `Vec::new`
98+
span_lint_and_sugg(
99+
cx,
100+
REDUNDANT_CLOSURE,
101+
expr.span,
102+
"redundant closure",
103+
"replace the closure with `Vec::new`",
104+
"std::vec::Vec::new".into(),
105+
Applicability::MachineApplicable,
106+
);
107+
}
106108
}
107109
// skip `foo(|| macro!())`
108110
return;

tests/ui/eta.fixed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ fn main() {
4848
// See #515
4949
let a: Option<Box<dyn (::std::ops::Deref<Target = [i32]>)>> =
5050
Some(vec![1i32, 2]).map(|v| -> Box<dyn (::std::ops::Deref<Target = [i32]>)> { Box::new(v) });
51+
52+
// issue #7224
53+
let _: Option<Vec<u32>> = Some(0).map(|_| vec![]);
5154
}
5255

5356
trait TestTrait {

tests/ui/eta.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ fn main() {
4848
// See #515
4949
let a: Option<Box<dyn (::std::ops::Deref<Target = [i32]>)>> =
5050
Some(vec![1i32, 2]).map(|v| -> Box<dyn (::std::ops::Deref<Target = [i32]>)> { Box::new(v) });
51+
52+
// issue #7224
53+
let _: Option<Vec<u32>> = Some(0).map(|_| vec![]);
5154
}
5255

5356
trait TestTrait {

tests/ui/eta.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,51 +33,51 @@ LL | let e = Some(1u8).map(|a| generic(a));
3333
| ^^^^^^^^^^^^^^ help: replace the closure with the function itself: `generic`
3434

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

4343
error: redundant closure
44-
--> $DIR/eta.rs:91:51
44+
--> $DIR/eta.rs:94: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-
--> $DIR/eta.rs:94:42
50+
--> $DIR/eta.rs:97: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-
--> $DIR/eta.rs:99:29
56+
--> $DIR/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-
--> $DIR/eta.rs:101:27
62+
--> $DIR/eta.rs:104: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-
--> $DIR/eta.rs:104:65
68+
--> $DIR/eta.rs:107: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-
--> $DIR/eta.rs:187:27
74+
--> $DIR/eta.rs:190:27
7575
|
7676
LL | let a = Some(1u8).map(|a| foo_ptr(a));
7777
| ^^^^^^^^^^^^^^ help: replace the closure with the function itself: `foo_ptr`
7878

7979
error: redundant closure
80-
--> $DIR/eta.rs:192:27
80+
--> $DIR/eta.rs:195:27
8181
|
8282
LL | let a = Some(1u8).map(|a| closure(a));
8383
| ^^^^^^^^^^^^^^ help: replace the closure with the function itself: `closure`

0 commit comments

Comments
 (0)