Files
codeql/python/ql/src/Variables/MultiplyDefined.ql
Copilot 717ff62d70 Python: deprecate AstNode.getAFlowNode() and rewrite internal callers
Preparatory refactor for the shared-CFG dataflow migration.

Deprecates the AstNode.getAFlowNode() cached predicate on the public
Python QL API and rewrites all ~140 internal callers across lib/, src/,
test/, and tools/ from `expr.getAFlowNode() = cfgNode` to
`cfgNode.getNode() = expr`, using ControlFlowNode.getNode() which
already exists in Flow.qll.

The predicate itself is preserved (with a deprecation note pointing at
the new pattern) so external users do not experience churn — they can
migrate at their own pace and the AST/CFG hierarchies still get the
intended untangling once the deprecation eventually elapses.

Semantic noop verified by:
- All 361 lib/ + src/ queries compile clean.
- All 122 ControlFlow + PointsTo library-tests pass.
- All 64 dataflow library-tests pass.
- All 113 Variables/Exceptions/Expressions/Statements/Functions/Imports/
  Security/CWE-798/ModificationOfParameterWithDefault query-tests pass.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-06-22 14:55:19 +02:00

73 lines
1.9 KiB
Plaintext

/**
* @name Variable defined multiple times
* @description Assignment to a variable occurs multiple times without any intermediate use of that variable
* @kind problem
* @tags quality
* maintainability
* useless-code
* external/cwe/cwe-563
* @problem.severity warning
* @sub-severity low
* @precision very-high
* @id py/multiple-definition
*/
import python
import Definition
predicate multiply_defined(AstNode asgn1, AstNode asgn2, Variable v) {
/*
* Must be redefined on all possible paths in the CFG corresponding to the original source.
* For example, splitting may create a path where `def` is unconditionally redefined, even though
* it is not in the original source.
*/
forex(Definition def, Definition redef |
def.getVariable() = v and
def.getNode() = asgn1 and
redef.getNode() = asgn2
|
def.isUnused() and
def.getARedef() = redef and
def.isRelevant()
)
}
predicate simple_literal(Expr e) {
e.(Num).getN() = "0"
or
e instanceof NameConstant
or
e instanceof List and not exists(e.(List).getAnElt())
or
e instanceof Tuple and not exists(e.(Tuple).getAnElt())
or
e instanceof Dict and not exists(e.(Dict).getAKey())
or
e.(StringLiteral).getText() = ""
}
/**
* Holds if the redefinition is uninteresting.
*
* A multiple definition is 'uninteresting' if it sets a variable to a
* simple literal before reassigning it.
* x = None
* if cond:
* x = value1
* else:
* x = value2
*/
predicate uninteresting_definition(AstNode asgn1) {
exists(AssignStmt a | a.getATarget() = asgn1 | simple_literal(a.getValue()))
}
from AstNode asgn1, AstNode asgn2, Variable v
where
multiply_defined(asgn1, asgn2, v) and
forall(Name el | el = asgn1.getParentNode().(Tuple).getAnElt() | multiply_defined(el, _, _)) and
not uninteresting_definition(asgn1)
select asgn1,
"This assignment to '" + v.getId() + "' is unnecessary as it is $@ before this value is used.",
asgn2, "redefined"