Skip to content

Commit

Permalink
Auto merge of rust-lang#12309 - Tyrubias:fix-allow-pub-under, r=y21
Browse files Browse the repository at this point in the history
fix: make `#[allow]` work on field for `pub_underscore_fields`

Closes rust-lang#12286

changelog: `#[allow(clippy::pub_underscore_fields)]` now works on linted field
  • Loading branch information
bors committed Feb 18, 2024
2 parents 5471e06 + d1e8a59 commit 5a50481
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
10 changes: 6 additions & 4 deletions clippy_lints/src/pub_underscore_fields.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -69,13 +69,15 @@ impl<'tcx> LateLintPass<'tcx> for PubUnderscoreFields {
// 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");
},
);
}
}
Expand Down
6 changes: 6 additions & 0 deletions tests/ui-toml/pub_underscore_fields/pub_underscore_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,10 @@ fn main() {
_pub: String,
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,
}
}

0 comments on commit 5a50481

Please sign in to comment.