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

Open
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
@yoff yoff marked this pull request as ready for review January 10, 2024 12:43
@yoff yoff requested a review from a team as a code owner January 10, 2024 12:43
@yoff
Copy link
Contributor Author

yoff commented Jan 10, 2024

Evaluation did not show any improvements, so this is a clean-up more than an optimisation...

@yoff yoff added the no-change-note-required This PR does not need a change note label Jan 10, 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 no-change-note-required This PR does not need a change note Python
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant