Skip to content

empty_line_after_outer_attr: some attrs like repr can't be crate-level attrs #14980

Closed
@matthiaskrgr

Description

@matthiaskrgr

Using the following flags

--force-warn clippy::empty_line_after_outer_attr

this code:

#[repr(align(536870912))]

enum Aligned {
    Zero = 0,
    One = 1,
}

fn main() {
    let aligned = Aligned::Zero;

    assert_eq!(tou8(Aligned::Zero), 0);
}

fn tou8(al: Aligned) -> u8 {
    al as u8
}

caused the following diagnostics:

    Checking _cranelift1434 v0.1.0 (/tmp/icemaker_global_tempdir.t8dYaH8PqaqW/icemaker_clippyfix_tempdir.GiHeMACJH7hN/_cranelift1434)
warning: empty line after outer attribute
 --> src/main.rs:1:1
  |
1 | / #[repr(align(536870912))]
2 | |
  | |_^
3 |   enum Aligned {
  |   ------------ the attribute applies to this enum
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#empty_line_after_outer_attr
  = note: requested on the command line with `--force-warn clippy::empty-line-after-outer-attr`
  = help: if the empty line is unintentional, remove it
help: if the attribute should apply to the crate use an inner attribute
  |
1 | #![repr(align(536870912))]
  |  +

warning: `_cranelift1434` (bin "_cranelift1434") generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.11s

However after applying these diagnostics, the resulting code:

#![repr(align(536870912))]
enum Aligned {
    Zero = 0,
    One = 1,
}

fn main() {
    let aligned = Aligned::Zero;

    assert_eq!(tou8(Aligned::Zero), 0);
}

fn tou8(al: Aligned) -> u8 {
    al as u8
}

no longer compiled:

    Checking _cranelift1434 v0.1.0 (/tmp/icemaker_global_tempdir.t8dYaH8PqaqW/icemaker_clippyfix_tempdir.GiHeMACJH7hN/_cranelift1434)
error[E0517]: attribute should be applied to a struct, enum, function, associated function, or union
 --> src/main.rs:1:9
  |
1 | #![repr(align(536870912))]
  |         ^^^^^^^^^^^^^^^^ not a struct, enum, function, associated function, or union

error: `repr` attribute cannot be used at crate level
 --> src/main.rs:1:9
  |
1 | #![repr(align(536870912))]
  |         ^^^^^^^^^^^^^^^^
2 | enum Aligned {
  |      ------- the inner attribute doesn't annotate this enum

For more information about this error, try `rustc --explain E0517`.
error: could not compile `_cranelift1434` (bin "_cranelift1434" test) due to 2 previous errors
warning: build failed, waiting for other jobs to finish...
error: could not compile `_cranelift1434` (bin "_cranelift1434") due to 2 previous errors

Maybe we could drop the help for attrs that only apply to particular items?

Version:

rustc 1.89.0-nightly (076ec59ff 2025-06-05)
binary: rustc
commit-hash: 076ec59ff1dcf538b9d3a0b8e0d7f4edd0559959
commit-date: 2025-06-05
host: x86_64-unknown-linux-gnu
release: 1.89.0-nightly
LLVM version: 20.1.5

Metadata

Metadata

Assignees

Labels

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