Sitelet https://web.archive.org/web/20240110001409/https://github.com/github/codeql/pull/15255
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python: remove assignments handled by capture library #15255

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

yoff
Copy link
Contributor

@yoff yoff commented Jan 8, 2024

Addresses https://github.com/github/codeql-python-team/issues/764

The following quick-query, run on top 100 Python projects, consistently reported about 1% of assignments can be removed:

import python
import semmle.python.dataflow.new.DataFlow
import semmle.python.dataflow.new.internal.VariableCapture

predicate superflous(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) {
  exists(AssignmentDefinition def |
    nodeFrom.(DataFlow::CfgNode).getNode() = def.getValue() and
    nodeTo.(DataFlow::CfgNode).getNode() = def.getDefiningNode() and
    exists(CapturedVariable v | v.getAStore() = nodeTo.asExpr())
  )
}

predicate allConsidered(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) {
  exists(AssignmentDefinition def |
    nodeFrom.(DataFlow::CfgNode).getNode() = def.getValue() and
    nodeTo.(DataFlow::CfgNode).getNode() = def.getDefiningNode()
  )
}

from int s, int a, int p
where
  s = count(DataFlow::Node nodeFrom, DataFlow::Node nodeTo | superflous(nodeFrom, nodeTo)) and
  a = count(DataFlow::Node nodeFrom, DataFlow::Node nodeTo | allConsidered(nodeFrom, nodeTo)) and
  p = 100 * s / a
select s, a, p

@yoff yoff added the Awaiting evaluation Do not merge yet, this PR is waiting for an evaluation to finish label Jan 8, 2024
@github-actions github-actions bot added the Python label Jan 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting evaluation Do not merge yet, this PR is waiting for an evaluation to finish Python
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant