Skip to content

Commit dc0feec

Browse files
committed
fix: initializing class in static method counts as usage (#1259)
1 parent 11825a2 commit dc0feec

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/rules/no_unused_vars.rs

Lines changed: 28 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
}
@@ -849,6 +855,7 @@ mod tests {
849855
"function foo(cb) { var b; cb = b = function(a) { cb(1 + a); }; b(); } foo();",
850856
"(class { set foo(UNUSED) {} })",
851857
"class Foo { set bar(UNUSED) {} } console.log(Foo)",
858+
"let foo; class Foo { instance; static { foo = new Foo(); } } foo.toString();",
852859
"var a = function () { a(); }; a();",
853860
"var a = function(){ return function () { a(); } }; a();",
854861
"const a = () => { a(); }; a();",
@@ -2379,6 +2386,27 @@ export const Foo = () => {
23792386
};
23802387
}
23812388

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

0 commit comments

Comments
 (0)