Skip to content

pub_underscore_fields: #[allow] doesn't work on the field that is linted #12286

@xFrednet

Description

@xFrednet

Summary

#[allow(clippy::pub_underscore_fields)] doesn't seem to work on the field itself, but only on the struct. The reason is most likely, that the lint is emitted at the struct and not the field.

This should be simple to fix, by using clippy_utils::diagnostics::span_lint_hir to emit the lint at the field directly.

The fix described above should work for other lint attributes as well, like #[expect]

Lint Name

clippy::pub_underscore_fields

Reproducer

I tried this code:

#![feature(lint_reasons)]
#![warn(clippy::pub_underscore_fields)]

pub struct Cat {
    #[allow(clippy::pub_underscore_fields)]
    pub _secret_1: u32,
    #[expect(clippy::pub_underscore_fields)]
    pub _secret_2: u32,
}

I saw this happen:

warning: field marked as public but also inferred as unused because it's prefixed with `_`
 --> src/lib.rs:6:5
  |
6 |     pub _secret: u32,
  |     ^^^^^^^^^^^
  |
  = help: consider removing the underscore, or making the field private
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#pub_underscore_fields
note: the lint level is defined here
 --> src/lib.rs:2:9
  |
2 | #![warn(clippy::pub_underscore_fields)]
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  
// [Same for expect]

I expected to see this happen:

Nothing as the lint was allowed

Version

rustc 1.78.0-nightly (8ace7ea1f 2024-02-07)
binary: rustc
commit-hash: 8ace7ea1f7cbba7b4f031e66c54ca237a0d65de6
commit-date: 2024-02-07
host: x86_64-unknown-linux-gnu
release: 1.78.0-nightly
LLVM version: 17.0.6

Additional Labels

No response

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't havegood first issueThese issues are a good way to get started with Clippy

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions