fix: make #[allow] work on field for pub_underscore_fields

Add test for future regression
This commit is contained in:
Victor Song 2024-02-17 17:19:19 -06:00
parent 5471e0645a
commit d1e8a5956f
2 changed files with 12 additions and 4 deletions

View File

@ -1,6 +1,6 @@
use clippy_config::types::PubUnderscoreFieldsBehaviour; use clippy_config::types::PubUnderscoreFieldsBehaviour;
use clippy_utils::attrs::is_doc_hidden; 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 clippy_utils::is_path_lang_item;
use rustc_hir::{FieldDef, Item, ItemKind, LangItem}; use rustc_hir::{FieldDef, Item, ItemKind, LangItem};
use rustc_lint::{LateContext, LateLintPass}; 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`. // We ignore fields that are `PhantomData`.
&& !is_path_lang_item(cx, field.ty, LangItem::PhantomData) && !is_path_lang_item(cx, field.ty, LangItem::PhantomData)
{ {
span_lint_and_help( span_lint_hir_and_then(
cx, cx,
PUB_UNDERSCORE_FIELDS, PUB_UNDERSCORE_FIELDS,
field.hir_id,
field.vis_span.to(field.ident.span), field.vis_span.to(field.ident.span),
"field marked as public but also inferred as unused because it's prefixed with `_`", "field marked as public but also inferred as unused because it's prefixed with `_`",
None, |diag| {
"consider removing the underscore, or making the field private", diag.help("consider removing the underscore, or making the field private");
},
); );
} }
} }

View File

@ -63,4 +63,10 @@ pub struct NamedPub {
_pub: String, _pub: String,
pub(crate) _mark: PhantomData<u8>, pub(crate) _mark: PhantomData<u8>,
} }
// shouldn't warn when `#[allow]` is used on field level
pub struct AllowedViolations {
#[allow(clippy::pub_underscore_fields)]
pub _first: u32,
}
} }