mirror of
https://github.com/github/codeql.git
synced 2026-04-24 08:15:14 +02:00
Initial commit of Python queries and QL libraries.
This commit is contained in:
committed by
Mark Shannon
parent
90c75cd362
commit
5f58824d1b
29
python/ql/src/Statements/NestedLoopsSameVariable.ql
Normal file
29
python/ql/src/Statements/NestedLoopsSameVariable.ql
Normal file
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
* @name Nested loops with same variable
|
||||
* @description Nested loops in which the target variable is the same for each loop make
|
||||
* the behavior of the loops difficult to understand.
|
||||
* @kind problem
|
||||
* @tags maintainability
|
||||
* correctness
|
||||
* @problem.severity recommendation
|
||||
* @sub-severity high
|
||||
* @precision very-high
|
||||
* @id py/nested-loops-with-same-variable
|
||||
*/
|
||||
import python
|
||||
|
||||
predicate loop_variable(For f, Variable v) {
|
||||
f.getTarget().defines(v)
|
||||
}
|
||||
|
||||
predicate variableUsedInNestedLoops(For inner, For outer, Variable v) {
|
||||
/* Only treat loops in body as inner loops. Loops in the else clause are ignored. */
|
||||
outer.getBody().contains(inner) and loop_variable(inner, v) and loop_variable(outer, v)
|
||||
/* Ignore cases where there is no use of the variable or the only use is in the inner loop */
|
||||
and exists(Name n | n.uses(v) and outer.contains(n) and not inner.contains(n))
|
||||
}
|
||||
|
||||
from For inner, For outer, Variable v
|
||||
where variableUsedInNestedLoops(inner, outer, v)
|
||||
select inner, "Nested for statement uses loop variable '" + v.getId() + "' of enclosing $@.",
|
||||
outer, "for statement"
|
||||
Reference in New Issue
Block a user