Feature: Utilize a control flow graph to detect unused code
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?
I also find this approach very interesting.
Can you share what CFG libraries you have a found so far?
Here's some: https://pypi.org/project/pycfg/ https://pypi.org/project/staticfg/ https://github.com/coetaur0/staticfg