python.lang.maintainability.useless-assign-keyed.useless-assignment-keyed

profile photo of semgrepsemgrep
Author
6,393
Download Count*

key $Y in $X is assigned twice; the first assignment is useless

Run Locally

Run in CI

Defintion

rules:
  - id: useless-assignment-keyed
    message: key `$Y` in `$X` is assigned twice; the first assignment is useless
    languages:
      - python
    severity: INFO
    pattern-either:
      - pattern: |
          $X[$Y] = ...
          $X[$Y] = ...
      - pattern: |
          $X[$Y][$Z] = ...
          $X[$Y][$Z] = ...
    metadata:
      category: maintainability
      technology:
        - python
      license: Commons Clause License Condition v1.0[LGPL-2.1-only]

Examples

useless-assign-keyed.py

d = {}
z = {}
a = {}
for i in xrange(100):
    # ruleid: useless-assignment-keyed
    d[i] = z[i]
    d[i] = z[i]
    d[i+1] = z[i]

    for i in xrange(100):
        # ruleid: useless-assignment-keyed
        da[i*1][j] = z[i]
        da[i*1][j] = z[i]
        da[i*4] = z[i]

# ok for this rule
x = 5
x = 5

x = y
x = y()

y() = y()