From d1e8a5956ffd73e65288b447c68ac11a7ac26577 Mon Sep 17 00:00:00 2001 From: Victor Song Date: Sat, 17 Feb 2024 17:19:19 -0600 Subject: [PATCH] fix: make `#[allow]` work on field for `pub_underscore_fields` Add test for future regression --- clippy_lints/src/pub_underscore_fields.rs | 10 ++++++---- .../pub_underscore_fields/pub_underscore_fields.rs | 6 ++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/clippy_lints/src/pub_underscore_fields.rs b/clippy_lints/src/pub_underscore_fields.rs index 88b5a6cfe2a..d20d4a605a2 100644 --- a/clippy_lints/src/pub_underscore_fields.rs +++ b/clippy_lints/src/pub_underscore_fields.rs @@ -1,6 +1,6 @@ use clippy_config::types::PubUnderscoreFieldsBehaviour; use clippy_utils::attrs::is_doc_hidden; -use clippy_utils::diagnostics::span_lint_and_help; +use clippy_utils::diagnostics::span_lint_hir_and_then; use clippy_utils::is_path_lang_item; use rustc_hir::{FieldDef, Item, ItemKind, LangItem}; use rustc_lint::{LateContext, LateLintPass}; @@ -69,13 +69,15 @@ fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) { // We ignore fields that are `PhantomData`. && !is_path_lang_item(cx, field.ty, LangItem::PhantomData) { - span_lint_and_help( + span_lint_hir_and_then( cx, PUB_UNDERSCORE_FIELDS, + field.hir_id, field.vis_span.to(field.ident.span), "field marked as public but also inferred as unused because it's prefixed with `_`", - None, - "consider removing the underscore, or making the field private", + |diag| { + diag.help("consider removing the underscore, or making the field private"); + }, ); } } diff --git a/tests/ui-toml/pub_underscore_fields/pub_underscore_fields.rs b/tests/ui-toml/pub_underscore_fields/pub_underscore_fields.rs index 1d8fee7daad..4ee8dbb8834 100644 --- a/tests/ui-toml/pub_underscore_fields/pub_underscore_fields.rs +++ b/tests/ui-toml/pub_underscore_fields/pub_underscore_fields.rs @@ -63,4 +63,10 @@ pub struct NamedPub { _pub: String, pub(crate) _mark: PhantomData, } + + // shouldn't warn when `#[allow]` is used on field level + pub struct AllowedViolations { + #[allow(clippy::pub_underscore_fields)] + pub _first: u32, + } }