Please provide
- Operating system: Ubuntu 22.04.1 LTS
- SonarLint plugin version: v3.14.0
- Programming language you’re coding in: JavaScript
- Is connected mode used: no
And a thorough description of the problem / question:
class FileBase {
/**
* @type {'f'|'d'|'g'|'l'}
*/
type
}
class Container extends FileBase {
/**
* @type {{[fileName: string]: FileBase}}
*/
children = {}
}
class Folder extends Container {
type = 'd'
}
/**
* @param {FileBase} a
* @param {Folder} b
*/
function f(a, b) {
if (a === b) { // Remove this "===" check; it will always be false. Did you mean to use "=="? sonarlint(javascript:S3403)
return a
}
return b
}
In this code, SonarLint does not recognize correctly that a
of type FileBase
can in fact also be of the derived type Folder
, and hence a
and b
could be the same object.
This seems to be a rather subtle error as making small changes removes the error:
Removing @type {'f'|'d'|'g'|'l'}
from FileBase.type
will remove the error:
class FileBase {
type
}
class Container extends FileBase {
/**
* @type {{[fileName: string]: FileBase}}
*/
children = {}
}
class Folder extends Container {
type = 'd'
}
/**
* @param {FileBase} a
* @param {Folder} b
*/
function f(a, b) {
if (a === b) { // No error
return a
}
return b
}
Making Folder
extending FileBase
directly will also remove this error:
class FileBase {
/**
* @type {'f'|'d'|'g'|'l'}
*/
type
}
class Container extends FileBase {
/**
* @type {{[fileName: string]: FileBase}}
*/
children = {}
}
class Folder extends FileBase {
type = 'd'
}
/**
* @param {FileBase} a
* @param {Folder} b
*/
function f(a, b) {
if (a === b) { // No error
return a
}
return b
}