Skip to content

Commit 9603d96

Browse files
committed
redundant_closure_call - add support for shadowed closures
1 parent 0fecaf1 commit 9603d96

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

clippy_lints/src/redundant_closure_call.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintCon
1111
use rustc_middle::hir::map::Map;
1212
use rustc_middle::lint::in_external_macro;
1313
use rustc_session::{declare_lint_pass, declare_tool_lint};
14-
use rustc_span::symbol::Ident;
1514

1615
declare_clippy_lint! {
1716
/// **What it does:** Detects closures called in the same expression where they
@@ -96,9 +95,9 @@ impl EarlyLintPass for RedundantClosureCall {
9695

9796
impl<'tcx> LateLintPass<'tcx> for RedundantClosureCall {
9897
fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx hir::Block<'_>) {
99-
fn count_closure_usage<'tcx>(block: &'tcx hir::Block<'_>, ident: &'tcx Ident) -> usize {
98+
fn count_closure_usage<'tcx>(block: &'tcx hir::Block<'_>, path: &'tcx hir::Path<'tcx>) -> usize {
10099
struct ClosureUsageCount<'tcx> {
101-
ident: &'tcx Ident,
100+
path: &'tcx hir::Path<'tcx>,
102101
count: usize,
103102
};
104103
impl<'tcx> hir_visit::Visitor<'tcx> for ClosureUsageCount<'tcx> {
@@ -108,7 +107,8 @@ impl<'tcx> LateLintPass<'tcx> for RedundantClosureCall {
108107
if_chain! {
109108
if let hir::ExprKind::Call(ref closure, _) = expr.kind;
110109
if let hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) = closure.kind;
111-
if self.ident == &path.segments[0].ident;
110+
if self.path.segments[0].ident == path.segments[0].ident
111+
&& self.path.res == path.res;
112112
then {
113113
self.count += 1;
114114
}
@@ -120,7 +120,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantClosureCall {
120120
hir_visit::NestedVisitorMap::None
121121
}
122122
};
123-
let mut closure_usage_count = ClosureUsageCount { ident, count: 0 };
123+
let mut closure_usage_count = ClosureUsageCount { path, count: 0 };
124124
closure_usage_count.visit_block(block);
125125
closure_usage_count.count
126126
}
@@ -136,7 +136,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantClosureCall {
136136
if let hir::ExprKind::Call(ref closure, _) = call.kind;
137137
if let hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) = closure.kind;
138138
if ident == path.segments[0].ident;
139-
if count_closure_usage(block, &ident) == 1;
139+
if count_closure_usage(block, path) == 1;
140140
then {
141141
span_lint(
142142
cx,

tests/ui/redundant_closure_call_late.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@ fn main() {
1414
let redun_closure = || 1;
1515
i = redun_closure();
1616

17-
// the lint is applicable here but the lint doesn't support redefinition
18-
let redefined_closure = || 1;
19-
i = redefined_closure();
20-
let redefined_closure = || 2;
21-
i = redefined_closure();
17+
// shadowed closures are supported, lint here
18+
let shadowed_closure = || 1;
19+
i = shadowed_closure();
20+
let shadowed_closure = || 2;
21+
i = shadowed_closure();
22+
23+
// don't lint here
24+
let shadowed_closure = || 2;
25+
i = shadowed_closure();
26+
i = shadowed_closure();
2227
}

tests/ui/redundant_closure_call_late.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,17 @@ LL | i = redun_closure();
66
|
77
= note: `-D clippy::redundant-closure-call` implied by `-D warnings`
88

9-
error: aborting due to previous error
9+
error: Closure called just once immediately after it was declared
10+
--> $DIR/redundant_closure_call_late.rs:19:5
11+
|
12+
LL | i = shadowed_closure();
13+
| ^^^^^^^^^^^^^^^^^^^^^^
14+
15+
error: Closure called just once immediately after it was declared
16+
--> $DIR/redundant_closure_call_late.rs:21:5
17+
|
18+
LL | i = shadowed_closure();
19+
| ^^^^^^^^^^^^^^^^^^^^^^
20+
21+
error: aborting due to 3 previous errors
1022

0 commit comments

Comments
 (0)