Skip to content

Commit 1d8a4b8

Browse files
authored
fix: initializing class in static method counts as usage (#1462)
1 parent f5db797 commit 1d8a4b8

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/rules/no_unused_vars.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,12 @@ impl Visit for Collector {
306306
}
307307
AssignTarget::Pat(_) => n.visit_children_with(self),
308308
}
309+
310+
if let Expr::New(new_expr) = &*n.right {
311+
self.without_cur_defining(|a| {
312+
new_expr.visit_children_with(a);
313+
});
314+
}
309315
} else {
310316
n.visit_children_with(self)
311317
}
@@ -2379,6 +2385,27 @@ export const Foo = () => {
23792385
};
23802386
}
23812387

2388+
#[test]
2389+
fn no_unused_vars_static_init_is_usage() {
2390+
assert_lint_err! {
2391+
NoUnusedVars,
2392+
"let foo: Foo;
2393+
class Foo {
2394+
instance;
2395+
static {
2396+
foo = new Foo();
2397+
}
2398+
}
2399+
": [
2400+
{
2401+
col: 4,
2402+
message: variant!(NoUnusedVarsMessage, NeverUsed, "foo"),
2403+
hint: variant!(NoUnusedVarsHint, AddPrefix, "foo"),
2404+
}
2405+
],
2406+
};
2407+
}
2408+
23822409
// TODO(magurotuna): deals with this using ControlFlow
23832410
#[test]
23842411
#[ignore = "control flow analysis is not implemented yet"]

0 commit comments

Comments
 (0)