]> git.lizzy.rs Git - rust.git/commit
Use ident instead of def_span in dead-code pass
authorPi Lanningham <pi.lanningham@gmail.com>
Fri, 25 Oct 2019 18:46:07 +0000 (14:46 -0400)
committerPi Lanningham <pi.lanningham@gmail.com>
Sun, 27 Oct 2019 01:42:52 +0000 (21:42 -0400)
commit7985510e37b91cab66a808ef6b3e4110e369dc9e
tree8d082e07fe63f593109cc305d3097f6d2494043c
parente063ddb12e1e1d2ca0ab05b9976ee1d0b20a34e1
Use ident instead of def_span in dead-code pass

According to @estebank, def_span scans forward on the line until it finds a {,
and if it can't find one, fallse back to the span for the whole item.  This
was apparently written before the identifier span was explicitly tracked on
each node.

This means that if an unused function signature spans multiple lines, the
entire function (potentially hundreds of lines) gets flagged as dead code.
This could, for example, cause IDEs to add error squiggly's to the whole
function.

By using the span from the ident instead, we narrow the scope of this in
most cases.  In a wider sense, it's probably safe to use ident.span
instead of def_span in most locations throughout the whole code base,
but since this is my first contribution, I kept it small.

Some interesting points that came up while I was working on this:
 - I reorganized the tests a bit to bring some of the dead code ones all
   into the same location
 - A few tests were for things unrelated to dead code (like the
   path-lookahead for parens), so I added #![allow(dead_code)] and
   cleaned up the stderr file to reduce noise in the future
 - The same fix doesn't apply to const and static declarations.  I tried
   adding these cases to the match expression, but that created a much
   wider change to tests and error messages, so I left it off until I
   could get some code review to validate the approach.
23 files changed:
src/librustc_passes/dead.rs
src/test/ui-fulldeps/lint-plugin-cmdline-allow.stderr
src/test/ui-fulldeps/lint-tool-cmdline-allow.stderr
src/test/ui/lint/dead-code/basic.stderr
src/test/ui/lint/dead-code/empty-unused-enum.stderr
src/test/ui/lint/dead-code/lint-dead-code-1.stderr
src/test/ui/lint/dead-code/lint-dead-code-2.stderr
src/test/ui/lint/dead-code/lint-dead-code-3.stderr
src/test/ui/lint/dead-code/lint-dead-code-4.stderr
src/test/ui/lint/dead-code/lint-dead-code-5.stderr
src/test/ui/lint/dead-code/newline-span.rs [new file with mode: 0644]
src/test/ui/lint/dead-code/newline-span.stderr [new file with mode: 0644]
src/test/ui/lint/dead-code/unused-enum.stderr
src/test/ui/lint/dead-code/with-core-crate.stderr
src/test/ui/path-lookahead.rs
src/test/ui/path-lookahead.stderr
src/test/ui/span/macro-span-replacement.stderr
src/test/ui/span/unused-warning-point-at-identifier.rs [new file with mode: 0644]
src/test/ui/span/unused-warning-point-at-identifier.stderr [new file with mode: 0644]
src/test/ui/span/unused-warning-point-at-signature.rs [deleted file]
src/test/ui/span/unused-warning-point-at-signature.stderr [deleted file]
src/test/ui/test-attrs/test-warns-dead-code.stderr
src/test/ui/thread-local-not-in-prelude.rs