python.lang.maintainability.useless-ifelse.useless-if-body

profile photo of semgrepsemgrep
Author
6,393
Download Count*

Useless if statement; both blocks have the same body

Run Locally

Run in CI

Defintion

rules:
  - id: useless-if-body
    pattern: |
      if $X:
          $S
      else:
          $S
    message: Useless if statement; both blocks have the same body
    languages:
      - python
    severity: WARNING
    metadata:
      references:
        - https://docs.python.org/3/tutorial/controlflow.html
      category: maintainability
      technology:
        - python
      license: Commons Clause License Condition v1.0[LGPL-2.1-only]

Examples

useless-ifelse.py

a, b, c = 1

# ruleid: useless-if-conditional
if a:
    print('1')
elif a:
    print('2')

# ruleid: useless-if-body
if a:
    print('1')
else:
    print('1')

# a and b are different cases -- ok
if a:
    print('1')
elif b:
    print('1')


# don't report on cases like this
if a:
    print('this is a')
elif b:
    print('this is b')
elif c:
    print('this is c')
elif d:
    print('this is d')


# don't report on cases like this
if a:
    print('this is a')
elif b:
    print('this is b')
elif c:
    print('this is b')