I've just finished a pretty significant refactoring; byte-compiler warnings were a great way to quickly find errors, without even running tests.

Still, there's one kind of code smell that the byte-compiler doesn't seem to catch: unused variables. And unfortunately, tests are powerless against that one.

For example, my recent refactoring caused a number of internal defconsts to become useless; but I didn't get warnings. Same for defvars and even a few defuns. All of these were private my-package--blah, so I really don't expect them to be used outside of the package. Even then, "unused definition" warnings would be useful (I'd mark the definitions obsolete, for example).

Is there a way to get warnings about unused definitions? I don't mind false positives too much (using intern and eval could hide uses of a definition from the byte-compiler, for example).

  • 1
    I don't think it is in general possible. Anything defined as a variable or a constant is potentially used by, for example by describe-variable. So, they aren't unused per se. It may be possible to do this for function's local variables, but only if you use lexical bindings (otherwise the functions called inside that function can look into the outer function's local variables). But you could probably come up with some grep-based script which would collect all variables declarations and report their usage. It won't be 100% correct, but may catch a lot of cases you are upset about.
    – wvxvw
    Dec 30, 2015 at 6:35
  • @wvxvw: Indeed, and I can also call (symbol-value (intern (some complicated string))), so there's no way to do this fully reliably (more generally, all static analyses are incomplete). Still, I wouldn't mind false positives too much (checkdoc has plenty of them, and it's still a useful tool)
    – Clément
    Dec 31, 2015 at 17:01
  • Unused lexical bindings are already detected, btw.
    – Clément
    Dec 31, 2015 at 17:01

1 Answer 1


While unused lexical bindings are already detected byte the byte compiler there's unfortunately no way to detect defconst and defvars that are unused (except manual use of grep).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.