-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Implement RFC#2: Unused attribute lint #14373
Conversation
(Needs a rebase already ;P ) |
Yeah, #14320 stomped all over everything :( |
Rebased |
@@ -1137,6 +1145,47 @@ fn check_attrs_usage(cx: &Context, attrs: &[ast::Attribute]) { | |||
} | |||
} | |||
|
|||
fn check_unused_attribute(cx: &Context, attrs: &[ast::Attribute]) { | |||
for attr in attrs.iter() { | |||
// whitelist docs since rustdoc looks at them |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this could be
static ATTR_WHITELIST: &'static [&'static str] = [
// rustdoc
"automatically_derived", "doc",
// trans
"address_insignificant",
// ...
];
// ...
for attr in attrs.iter() {
for name in ATTR_WHITELIST.iter() {
if attr.check_name(*name) {
break // can't have two different names at once.
}
}
if !attr:is_used(attr) {
cx.span_lint(...);
}
}
@huonw updated |
r=me with comments, a FIXME & issue for reducing the whitelist of attributes (especially trans ones) (just to reduce the bus-factor), and a rebase. |
Oh, also, does this make linting noticably slower ( I imagine doing the whitelist loop is a little slow, as is doing a TLD look up for each attribute, but I don't know how slow. |
There's no significant difference in the speed of the lint pass. |
Enough attributes are marked to cleanly compile an empty library.
This is a bit overly permissive but should be okay for now.
There's a fair number of attributes that have to be whitelisted since they're either looked for by rustdoc, in trans, or as needed. These can be cleaned up in the future.
The compiler now tracks which attributes were actually looked at during the compilation process and warns for those that were unused. Some things of note: * The tracking is done via thread locals, as it made the implementation more straightforward. Note that this shouldn't hamper any future parallelization as each task can have its own thread local state which can be merged for the lint pass. If there are serious objections to this, I can restructure things to explicitly pass the state around. * There are a number of attributes that have to be special-cased and globally whitelisted. This happens for four reasons: * The `doc` and `automatically_derived` attributes are used by rustdoc, but not by the compiler. * The crate-level attributes `license`, `desc` and `comment` aren't currently used by anything. * Stability attributes as well as `must_use` are checked only when the tagged item is used, so we can't guarantee that the compiler's looked at them. * 12 attributes are used only in trans, which happens after the lint pass. #14300 is adding infrastructure to track lint state through trans, which this lint should also be able to use to handle the last case. For the other attributes, the right solution would probably involve a specific pass to mark uses that occur in the correct context. For example, a `doc` attribute attached to a match arm should generate a warning, but will not currently. RFC: 0002-attribute-usage
The compiler now tracks which attributes were actually looked at during the compilation process and warns for those that were unused.
Some things of note:
doc
andautomatically_derived
attributes are used by rustdoc, but not by the compiler.license
,desc
andcomment
aren't currently used by anything.must_use
are checked only when the tagged item is used, so we can't guarantee that the compiler's looked at them.enum size lint #14300 is adding infrastructure to track lint state through trans, which this lint should also be able to use to handle the last case. For the other attributes, the right solution would probably involve a specific pass to mark uses that occur in the correct context. For example, a
doc
attribute attached to a match arm should generate a warning, but will not currently.RFC: 0002-attribute-usage