Uploaded image for project: 'SonarPython'
  1. SonarPython
  2. SONARPY-615

Rule S5727: Comparison to None should not be constant

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.6
    • Fix Version/s: 2.7
    • Component/s: Rules
    • Labels:

      Description

      After implementing RSPEC-2159 and RSPEC-3403 for SonarPython we noticed that some issues are Code Smells rather than bugs. Example:

      x = ValueError()
      if x is None:  # Noncompliant but this is a code smell as it has no effect
          ...
      

      Thus we created a separate rule RSPEC-5727 "Comparison to None should not be constant". Every issue comparing another type to None values should be moved from RSPEC-2159 and RSPEC-3403 to RSPEC-5727.

      x = ValueError()
      if x is None:  # Noncompliant
          ...
      
      if x is not None:  # Noncompliant
          ...
      
      if x == None:  # Noncompliant
          ...
      
      if x != None:  # Noncompliant
          ...
      

      In addition, this rule will also raise issues when two values which are always None are compared.

      x = None
      if x is None:  # Noncompliant
          ...
      
      if x is not None:  # Noncompliant
          ...
      
      x = None
      if x == None:  # Noncompliant
          ...
      
      x = None
      if x != None:  # Noncompliant
          ...
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                pierre-yves.nicolas Pierre-Yves Nicolas
                Reporter:
                nicolas.harraudeau Nicolas Harraudeau
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Due:
                  Created:
                  Updated:
                  Resolved: