Skip to content

Incorrect suggestion in useless_attribute lint #4467

Closed
@phansch

Description

@phansch

The useless_attribute lint is meant to warn about extern crate and use items with outer lint attributes (for example #[allow(dead_code)]).

However, when this lint triggers on items that are preceded by other items, applying the suggestion will fail to compile.

Minimal example (Playground Link):

#![allow(unused_imports)]

fn main() {}

#[allow(dead_code)]
use aho_corasick;

Produces:

error: useless lint attribute
 --> src/main.rs:6:1
  |
6 | #[allow(dead_code)]
  | ^^^^^^^^^^^^^^^^^^^ help: if you just forgot a `!`, use: `#![allow(dead_code)]`

But applying the suggestion results in:

error: an inner attribute is not permitted in this context
 --> src/main.rs:6:3
  |
6 | #![allow(dead_code)]
  |   ^

To fix this, we could

a) Try to find a way to detect if the use/extern crate is preceded by any other items
b) Remove the suggestion completely if a) turns out impossible

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedL-suggestionLint: Improving, adding or fixing lint suggestions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions