Skip to content

Commit b2c2266

Browse files
committed
Fix FP for redundant_closure_call
Visit the nested things like function body when checking closure call.
1 parent 27ae4d3 commit b2c2266

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

clippy_lints/src/redundant_closure_call.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,17 @@ impl EarlyLintPass for RedundantClosureCall {
9595

9696
impl<'tcx> LateLintPass<'tcx> for RedundantClosureCall {
9797
fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx hir::Block<'_>) {
98-
fn count_closure_usage<'tcx>(block: &'tcx hir::Block<'_>, path: &'tcx hir::Path<'tcx>) -> usize {
99-
struct ClosureUsageCount<'tcx> {
98+
fn count_closure_usage<'a, 'tcx>(
99+
cx: &'a LateContext<'tcx>,
100+
block: &'tcx hir::Block<'_>,
101+
path: &'tcx hir::Path<'tcx>,
102+
) -> usize {
103+
struct ClosureUsageCount<'a, 'tcx> {
104+
cx: &'a LateContext<'tcx>,
100105
path: &'tcx hir::Path<'tcx>,
101106
count: usize,
102107
};
103-
impl<'tcx> hir_visit::Visitor<'tcx> for ClosureUsageCount<'tcx> {
108+
impl<'a, 'tcx> hir_visit::Visitor<'tcx> for ClosureUsageCount<'a, 'tcx> {
104109
type Map = Map<'tcx>;
105110

106111
fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
@@ -117,10 +122,10 @@ impl<'tcx> LateLintPass<'tcx> for RedundantClosureCall {
117122
}
118123

119124
fn nested_visit_map(&mut self) -> hir_visit::NestedVisitorMap<Self::Map> {
120-
hir_visit::NestedVisitorMap::None
125+
hir_visit::NestedVisitorMap::OnlyBodies(self.cx.tcx.hir())
121126
}
122127
};
123-
let mut closure_usage_count = ClosureUsageCount { path, count: 0 };
128+
let mut closure_usage_count = ClosureUsageCount { cx, path, count: 0 };
124129
closure_usage_count.visit_block(block);
125130
closure_usage_count.count
126131
}
@@ -136,7 +141,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantClosureCall {
136141
if let hir::ExprKind::Call(ref closure, _) = call.kind;
137142
if let hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) = closure.kind;
138143
if ident == path.segments[0].ident;
139-
if count_closure_usage(block, path) == 1;
144+
if count_closure_usage(cx, block, path) == 1;
140145
then {
141146
span_lint(
142147
cx,

tests/ui/redundant_closure_call_late.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,16 @@ fn main() {
2424
let shadowed_closure = || 2;
2525
i = shadowed_closure();
2626
i = shadowed_closure();
27+
28+
// Fix FP in #5916
29+
let mut x;
30+
let create = || 2 * 2;
31+
x = create();
32+
fun(move || {
33+
x = create();
34+
})
35+
}
36+
37+
fn fun<T: 'static + FnMut()>(mut f: T) {
38+
f();
2739
}

0 commit comments

Comments
 (0)