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

Feature: Utilize a control flow graph to detect unused code #385

Open
ericwb opened this issue Feb 4, 2025 · 2 comments
Open

Feature: Utilize a control flow graph to detect unused code #385

ericwb opened this issue Feb 4, 2025 · 2 comments

Comments

@ericwb
Copy link

ericwb commented Feb 4, 2025

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?

@TravisDart
Copy link
Contributor

I also find this approach very interesting.

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

@ericwb
Copy link
Author

ericwb commented Feb 4, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants