GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • CodeQL documentation
  • CodeQL overview
  • Writing CodeQL queries
  • CodeQL language guides
  • QL language reference
  • CodeQL standard-libraries
  • CodeQL query help
  • CodeQL repository
  • QL publications
  • CodeQL query help for C and C++
  • CodeQL query help for C#
  • CodeQL query help for GitHub Actions
  • CodeQL query help for Go
  • CodeQL query help for Java and Kotlin

This view

  • Original page
  • New address

GeezerVison is showing codeql.github.com — Suspicious unused loop iteration variable — CodeQL query help documentation

Suspicious unused loop iteration variable¶

ID: py/unused-loop-variable
Kind: problem
Security severity: 
Severity: error
Precision: high
Tags:
   - quality
   - reliability
   - correctness
Query suites:
   - python-code-quality.qls
   - python-security-and-quality.qls

Click to see the query in the CodeQL repository

A for loop iteration variable is not used in the body of the loop, and the loop does not count the number of items in the sequence. This is suspicious as there is rarely any reason to iterate over a sequence and not use the contents. Not using the loop variable can often indicate a logical error or typo.

Recommendation¶

Carefully check that the loop variable should not be used. If the variable is genuinely not being used and the code is correct, then rename the variable to _ or unused to indicate to readers of the code that it is intentionally unused.

Example¶

In this example, the for loop iteration variable x is never used. It appears that the original test function was used to test TypeA and was subsequently modified to test TypeB as well.


#
def test():
    for t in [TypeA, TypeB]:
        x = TypeA()
        run_test(x)

It is likely that the change from x = TypeA() to x = t() was forgotten. The fixed version is shown below.


#
def test():
    for t in [TypeA, TypeB]:
        x = t
        run_test(x)

References¶

  • Python Language Reference: The for statement.

  • Python Tutorial: For statements.

  • © GitHub, Inc.
  • Terms
  • Privacy
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.