[False positive] Remove this identity check; it will always be True

  • Operating system: Ubuntu 20.04.5 LTS x86_64
  • IDE name and flavor/env: vscode and PyCharm

I get this warning when checking the return value of pydoc.locate not to be None:

Remove this identity check; it will always be True.sonarlint(python:S5727)

But this is a false positive as the None is a subtype of object, and the pydoc.locate could return a None if the object not exits (see: pydoc.py:L1730)
To reproduce you can use this dummy function:

import pydoc

def f():
    res = pydoc.locate("modul.Obj")
    if res is not None: # <--- Remove this identity check; it will always be True.sonarlint(python:S5727)
        return res
    raise ModuleNotFoundError()

Hey there.

What version of SonarLint are you using?

Name: SonarLint
Id: SonarSource.sonarlint-vscode
Description: SonarLint is an IDE extension that helps you detect and fix quality issues as you write code in C, C++, Java, JavaScript, PHP, Python, HTML and TypeScript.
Version: 3.15.1
Publisher: SonarSource
VS Marketplace Link: SonarLint - Visual Studio Marketplace

Hello @smallSwed and sorry for the late reply,

Indeed, the rule raises a false positive in this case. Thank you for reporting it!
I created the following ticket to fix this.

Cheers,
Guillaume

1 Like