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

Implement RFC#2: Unused attribute lint #14373

Merged
merged 6 commits into from
May 25, 2014
Merged

Conversation

sfackler
Copy link
Member

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.
      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

@huonw
Copy link
Member

huonw commented May 23, 2014

(Needs a rebase already ;P )

@sfackler
Copy link
Member Author

Yeah, #14320 stomped all over everything :(

@sfackler
Copy link
Member Author

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
Copy link
Member

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(...);
    }
}

@sfackler
Copy link
Member Author

@huonw updated

@huonw
Copy link
Member

huonw commented May 24, 2014

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.

@huonw
Copy link
Member

huonw commented May 24, 2014

Oh, also, does this make linting noticably slower (-Z time-passes)?

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.

@sfackler
Copy link
Member Author

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.
bors added a commit that referenced this pull request May 25, 2014
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
@bors bors closed this May 25, 2014
@bors bors merged commit 3347993 into rust-lang:master May 25, 2014
@sfackler sfackler deleted the unused-attr branch November 26, 2016 05:54
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

Successfully merging this pull request may close these issues.

None yet

3 participants