Allocation Result Ignored
ID |
javascript.allocation_result_ignored |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
medium |
Resource |
Code Smell |
Language |
JavaScript |
Tags |
code-smell, dead-code |
Description
Reports new expressions used as a free-standing statement, where the constructed object is neither assigned, returned, nor passed anywhere.
// Bad
new EventListener(target, fn);
new Logger();
Rationale
If the result of new is discarded, the program is using the constructor purely for its side effects — registering a listener, mutating shared state, opening a connection. That pattern is hard to read (the side effect is hidden behind an allocation that looks like it should produce a value) and easy to break later when someone "cleans up" the unused object.
It is also a common pattern of dead code: a refactor extracted the work into a function but left the original new behind.
Remediation
If the constructor is doing real work, replace the allocation with an explicit factory call or a top-level function:
// Good — name the action being performed.
EventListener.attach(target, fn);
configureLogger();
If the result is genuinely needed (e.g. to be released later), capture it in a variable:
// Good
const listener = new EventListener(target, fn);
// ... later
listener.detach();
If the line is dead code, delete it.