Rust: add a warning on attributes that can be confused with global ones

Created on 29 Oct 2020  路  3Comments  路  Source: rust-lang/rust

I was asked by a person new to Rust why #[allow(unused_variables)] does not work in the following code.

#[allow(dead_code)]
#[allow(unused_variables)]

fn trip() {
}

fn main() {
 let a =1;
// println!("{}",a);
}

It would be nice to have a warning here that these attributes are for the next item, not for the whole file as one could think from their position in file. Probably a warning when there's a blank line between item body and its attributes would work.

A-lint C-feature-request

All 3 comments

I think Clippy's empty_line_after_outer_attr is what you're looking for? https://rust-lang.github.io/rust-clippy/master/index.html#empty_line_after_outer_attr

Looks right, yes. I'd argue that it's a good idea to have it in rustc because this looks like a mistake a new user can accidentally do and they likely would not know about clippy yet. However, the note about possible false positives is not exactly optimistic.

And even in Clippy it's allow-by-default, so you cannot really use it when you're a newcomer and have no idea about this specific problem.

Was this page helpful?
0 / 5 - 0 ratings