Movatterモバイル変換


[0]ホーム

URL:


CodeQL documentation
CodeQL resources

Unused variable, import, function or class

ID: js/unused-local-variableKind: problemSecurity severity: Severity: recommendationPrecision: very-highTags:   - quality   - maintainability   - useless-codeQuery suites:   - javascript-security-and-quality.qls

Click to see the query in the CodeQL repository

Unused local variables make code hard to read and understand. Any computation used to initialize an unused variable is wasted, which may lead to performance problems.

Similarly, unused imports and unused functions or classes can be confusing. They may even be a symptom of a bug caused, for example, by an incomplete refactoring.

Recommendation

Remove the unused program element.

Example

In this code, the functionf initializes a local variablex with a call to the functionexpensiveComputation, but later on this variable is never read. Removingx would improve code quality and performance.

functionf(){varx=expensiveComputation();return23;}

A slightly subtle case is shown below, where a function expression namedf is assigned to a variablef:

varf=functionf(){return"Hi!";};f();

Note that this example involves two distinct variables, both namedf: the global variable to which the function is assigned, and the variable implicitly declared by the function expression. The call tof() refers to the former variable, whereas the latter is unused. Hence the example can be rewritten as follows, eliminating the useless variable:

varf=function(){return"Hi!";};f();

A similar situation can occur with ECMAScript 2015 module exports, as shown in the following example:

exportdefaultfunctionf(){return"Hi!";};

Again, the named function expression implicitly declares a variablef, but because the export statement is a default export, this variable is unused and can be eliminated:

exportdefaultfunction(){return"Hi!";};

References


[8]ページ先頭

©2009-2025 Movatter.jp