Skip to content

Commit 64b53f3

Browse files
committed
Auto merge of rust-lang#5920 - giraffate:fix_fp_for_redundant_closure_call, r=mikerite
Fix FP for `redundant_closure_call` Fix rust-lang#5916 changelog: Fix FP for `redundant_closure_call` when called in function body
2 parents 8d9a485 + 9fe0ac3 commit 64b53f3

File tree

4 files changed

+27
-10
lines changed

4 files changed

+27
-10
lines changed

clippy_lints/src/redundant_closure_call.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl EarlyLintPass for RedundantClosureCall {
7777
cx,
7878
REDUNDANT_CLOSURE_CALL,
7979
expr.span,
80-
"try not to call a closure in the expression where it is declared.",
80+
"try not to call a closure in the expression where it is declared",
8181
|diag| {
8282
if decl.inputs.is_empty() {
8383
let mut app = Applicability::MachineApplicable;
@@ -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_early.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
error: try not to call a closure in the expression where it is declared.
1+
error: try not to call a closure in the expression where it is declared
22
--> $DIR/redundant_closure_call_early.rs:9:17
33
|
44
LL | let mut k = (|m| m + 1)(i);
55
| ^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::redundant-closure-call` implied by `-D warnings`
88

9-
error: try not to call a closure in the expression where it is declared.
9+
error: try not to call a closure in the expression where it is declared
1010
--> $DIR/redundant_closure_call_early.rs:12:9
1111
|
1212
LL | k = (|a, b| a * b)(1, 5);

tests/ui/redundant_closure_call_fixable.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: try not to call a closure in the expression where it is declared.
1+
error: try not to call a closure in the expression where it is declared
22
--> $DIR/redundant_closure_call_fixable.rs:7:13
33
|
44
LL | let a = (|| 42)();

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)