Skip to content

useless-expression (B018)#

Derived from the flake8-bugbear linter.

What it does#

Checks for useless expressions.

Why is this bad?#

Useless expressions have no effect on the program, and are often included by mistake. Assign a useless expression to a variable, or remove it entirely.


1 + 1

Use instead:

foo = 1 + 1

Known problems#

This rule ignores expression types that are commonly used for their side effects, such as function calls.

However, if a seemingly useless expression (like an attribute access) is needed to trigger a side effect, consider assigning it to an anonymous variable, to indicate that the return value is intentionally ignored.

For example, given:

with errors.ExceptionRaisedContext():

Use instead:

with errors.ExceptionRaisedContext():
    _ = obj.attribute