rspec/rules/S5727/python/rule.adoc
2023-08-03 09:44:48 +02:00

67 lines
1.6 KiB
Plaintext

This rule raises an issue when a comparison to `None` is invariant.
== Why is this an issue?
Checking if a variable or parameter is `None` should only be done when you expect that it can be `None`. Doing so when the variable is always `None` or never `None` is confusing at best. At worse, there is a bug and the variable is not updated properly.
This rule raises an issue when expressions `X is None`, `X is not None`, `X == None` or `X != None` are constant, i.e. `X` is always None or never None.
=== Code examples
==== Noncompliant code example
[source,python,diff-id=1,diff-type=noncompliant]
----
def foo():
my_var = None
if my_var == None: # Noncompliant: always True.
...
----
==== Compliant solution
[source,python,diff-id=1,diff-type=compliant]
----
def foo(my_var):
if my_var == None:
...
----
:link-with-uscores1: https://docs.python.org/3/reference/datamodel.html#object.__eq__
== Resources
=== Documentation
* Python documentation - https://docs.python.org/3/reference/expressions.html#is-not[Identity comparisons]
* Python documentation - {link-with-uscores1}[``++__eq__++`` operator]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
* Remove this identity check; it will always be True/False
* Remove this == comparison; it will always be True/False
* Remove this != comparison; it will always be True/False
=== Highlighting
the "is", "is not", "==" or "!=" operator
'''
== Comments And Links
(visible only on this page)
=== is related to: S5914
endif::env-github,rspecator-view[]