vulture icon indicating copy to clipboard operation
vulture copied to clipboard

Feature: Utilize a control flow graph to detect unused code

Open ericwb opened this issue 1 year ago • 2 comments

I want to suggest a different way of detecting dead code. While the AST is useful, I think it can result in a number of false positives.

If you take the AST parsed output and convert into a control flow graph (CFG), you get a directed graph of the code blocks. If a block of code exists with no edges, you know the code is dead and unused.

Unfortunately, converting AST to CFG is not easy, but there are some existing libraries that do it, although I don't think they are well maintained to current versions of Python. Also, to be most useful and accurate, the CFG needs to be generated for the entire code based being analyzed, not just a single file.

Thoughts?

ericwb avatar Feb 04 '25 00:02 ericwb

I also find this approach very interesting.

Can you share what CFG libraries you have a found so far?

TravisDart avatar Feb 04 '25 19:02 TravisDart

Here's some: https://pypi.org/project/pycfg/ https://pypi.org/project/staticfg/ https://github.com/coetaur0/staticfg

ericwb avatar Feb 04 '25 21:02 ericwb