Why this matters
Creating an exception without raising it does nothing and is likely an oversight. It should be removed or properly raised.
Identify instances where an exception object is created but never raised. This is likely an oversight and should either be removed or properly raised.
Creating an exception without raising it does nothing and is likely an oversight. It should be removed or properly raised.
Side-by-side examples engineers can pattern-match during review.
def func(x):
if not isinstance(x, int):
TypeError("Wrong type for parameter 'x'. func expects an integer") # Noncompliant
if x < 0:
ValueError # Noncompliant
return x + 42def func(x):
if not isinstance(x, int):
raise TypeError("Wrong type for parameter 'x'. func expects an integer")
if x < 0:
raise ValueError
return x + 42def func(x):
if not isinstance(x, int):
TypeError("Wrong type for parameter 'x'. func expects an integer") # Noncompliant
if x < 0:
ValueError # Noncompliant
return x + 42def func(x):
if not isinstance(x, int):
raise TypeError("Wrong type for parameter 'x'. func expects an integer")
if x < 0:
raise ValueError
return x + 42From the same buckets as this rule.
Check if loops use equality operators (== or !=) in termination conditions. These can lead to infinite loops if the condition is never met exactly. Instead, use relational operators like < or > for safer loop termination.