SonarLint - False Positive for Useless Assignment (S1845) When Assigning to a Null Value

(John Leonard Javiniar) #1

I am using SonarLint for VS Code.

In code below assignment to a null value is reported as useless assignment (S1854) even though it is said in the docs that this is an exception to the rule

Lang: JavaScript
Version: 1.6.0
Sample Code:

function doSomething () {
    var foo = new Foo();;
    // I'm done using it and I want to clean it up early.
    foo = null // <- This line gets tagged as Useless Assignment (S1845)
(Oliver Goroll) #2

Hi John,

the documentation states that initializations to null are ignored [1]. In your case, though, the assignment is not an initialization. Further, if the scope of the variable foo is restricted to doSomething(), the object should be freed/garbage collected anyway and explicitely setting it to null would be unnecessary (assuming the JavaScript’s memory managment is comparable to Java/C++)