SonarLint: Java - Null checks should not be used with "instanceof" (squid:S4201)

SonarLint: Java - Null checks should not be used with "instanceof" (squid:S4201)

Null check should not be used with the instance of since it is redundant, there is no need to check the null with an instance of since null is not an instance of anything,

I think this makes sense

Non-Compliant Code

if((customFieldType!=null) && (customFieldType instanceof AttachmentCFType)){
                String mode = this.getMode();
            }

Compliant Solution

  • Removed the null check from the if block
if(customFieldType instanceof AttachmentCFType){
                String mode = this.getMode();
            }