ruff icon indicating copy to clipboard operation
ruff copied to clipboard

Add support for `vulture`-like dead code detection

Open charliermarsh opened this issue 2 years ago • 12 comments

As a maintainer of a large Python codebase, I found vulture to be really useful, especially for detecting unused functions and unused modules.

This would require us to introduce some new patterns and capabilities that don't yet exist in Ruff, but should! For example, we need to be able to run some analysis over every file (list the used members), then reconcile that analysis post-hoc. We also need to be able to map from references to local modules.

charliermarsh avatar Nov 22 '22 14:11 charliermarsh

By the way there is a subset of vulture (and pylint) that would be useful even without mapping out the entire codebase, namely this sort of dead-code detection:

def oops():
    return 1
    return 2  # <- guaranteed never to run

olliemath avatar May 12 '23 12:05 olliemath

Yeah I'm interested in doing this. @MichaReiser shared some links with me around building these kinds of control-flow graphs. I may write it up as a more detailed issue.

charliermarsh avatar May 12 '23 12:05 charliermarsh

+1 we really miss this kind of linter in ruff

The most interesting thing is to find those functions and classes that are forgotten and do not run

saippuakauppias avatar Jun 08 '23 06:06 saippuakauppias

Also +1 on linting these issues in ruff.

eronning avatar Aug 30 '23 14:08 eronning

+1 – would be nice.

Being 100% certain about deadness of names (even locals) can be tricky – e.g. PyCharm generally highlights (well, lowlights) unused locals, unless it notices vars(), locals(), etc. being used in the same scope, in which case it just throws its paws up and doesn't do that highlighting anymore.

For globals, it gets even harder because a tricky user could be getattring (or vars().geting) from any given module.

akx avatar Sep 07 '23 13:09 akx

This would be extremely useful. I understand full implementation requires a lot of work, but checking if methods or variables are unused is very helpful.

FilipeBento avatar Oct 11 '23 16:10 FilipeBento

This vulture-like tool is great: https://github.com/asottile/dead

choweth avatar Dec 19 '23 17:12 choweth

Since this was opened a while ago, have there been any changes internally that would make such an implementation easier?

ofek avatar Feb 20 '24 23:02 ofek

Since this was opened a while ago, have there been any changes internally that would make such an implementation easier?

Not yet. Adding multi-file analysis is a feature that's on our roadmap which will allow us to support this.

dhruvmanila avatar Feb 21 '24 05:02 dhruvmanila