python.lang.correctness.common-mistakes.string-concat-in-list.string-concat-in-list

Community Favorite
profile photo of returntocorpreturntocorp
Author
34,755
Download Count*

Detected strings that are implicitly concatenated inside a list. Python will implicitly concatenate strings when not explicitly delimited. Was this supposed to be individual elements of the list?

Run Locally

Run in CI

Defintion

rules:
  - id: string-concat-in-list
    patterns:
      - pattern-either:
          - pattern-inside: "[...]"
          - pattern-inside: "{...}"
      - pattern: '"..." "..."'
      - pattern-not-inside: f"..."
      - pattern-not-inside: "{..., $KEY: $VALUE, ...}"
    message: Detected strings that are implicitly concatenated inside a list. Python
      will implicitly concatenate strings when not explicitly delimited. Was
      this supposed to be individual elements of the list?
    severity: WARNING
    languages:
      - python
    metadata:
      category: correctness
      technology:
        - python
      license: Commons Clause License Condition v1.0[LGPL-2.1-only]

Examples

string-concat-in-list.py

# ruleid:string-concat-in-list
bad = ["123" "456" "789"]

# ruleid:string-concat-in-list
bad = ["123" f"{456}" "789"]

bad = [
    # ruleid:string-concat-in-list
    "abc"
    "cde"
    "efg",
    "hijk"
]

bad = [
    "abc",
    # ruleid:string-concat-in-list
    "cde"
    "efg"
    "hijk"
]

bad = [
    "abc",
    # ruleid:string-concat-in-list
    "cde"
    f"efg"
    "hijk"
]

bad = {
    # ruleid:string-concat-in-list
    "abc"
    "cde"
    "efg",
    "hijk"
}

good = {
    "key1": "value1",
    # ok:string-concat-in-list
    "key2": "value2"
    "value2 continuation",
    "key3": "value3",
}

good = {
    "key1": "value1",
    # ok:string-concat-in-list
    "key2": "value2 {}"
    .format("value2 continuation"),
    "key3": "value3",
}

# ok:string-concat-in-list
good = ["123"]

# ok:string-concat-in-list
good = [123, 456]

# ok:string-concat-in-list
good = ["123", "456"]

# ok:string-concat-in-list
good = [f"123"]

# ok:string-concat-in-list
good = [f"{123}"]

# ok:string-concat-in-list
good = ["123", f"{456}"]