Suspicious unused loop iteration variable¶
ID: py/unused-loop-variableKind: problemSecurity severity: Severity: errorPrecision: highTags: - quality - reliability - correctnessQuery suites: - 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_ orunused to indicate to readers of the code that it is intentionally unused.
Example¶
In this example, thefor loop iteration variablex is never used. It appears that the originaltest function was used to testTypeA and was subsequently modified to testTypeB as well.
#deftest():fortin[TypeA,TypeB]:x=TypeA()run_test(x)
It is likely that the change fromx=TypeA() tox=t() was forgotten. The fixed version is shown below.
#deftest():fortin[TypeA,TypeB]:x=trun_test(x)
References¶
Python Language Reference:The for statement.
Python Tutorial:For statements.