mirror of
https://github.com/allankoechke/rust.git
synced 2026-12-04 09:44:40 +00:00
`hir::Item` has an `ident` field.
- It's always non-empty for these item kinds: `ExternCrate`, `Static`,
`Const`, `Fn`, `Macro`, `Mod`, `TyAlias`, `Enum`, `Struct`, `Union`,
Trait`, TraitAalis`.
- It's always empty for these item kinds: `ForeignMod`, `GlobalAsm`,
`Impl`.
- For `Use`, it is non-empty for `UseKind::Single` and empty for
`UseKind::{Glob,ListStem}`.
All of this is quite non-obvious; the only documentation is a single
comment saying "The name might be a dummy name in case of anonymous
items". Some sites that handle items check for an empty ident, some
don't. This is a very C-like way of doing things, but this is Rust, we
have sum types, we can do this properly and never forget to check for
the exceptional case and never YOLO possibly empty identifiers (or
possibly dummy spans) around and hope that things will work out.
The commit is large but it's mostly obvious plumbing work. Some notable
things.
- A similar transformation makes sense for `ast::Item`, but this is
already a big change. That can be done later.
- Lots of assertions are added to item lowering to ensure that
identifiers are empty/non-empty as expected. These will be removable
when `ast::Item` is done later.
- `ItemKind::Use` doesn't get an `Ident`, but `UseKind::Single` does.
- `lower_use_tree` is significantly simpler. No more confusing `&mut
Ident` to deal with.
- `ItemKind::ident` is a new method, it returns an `Option<Ident>`. It's
used with `unwrap` in a few places; sometimes it's hard to tell
exactly which item kinds might occur. None of these unwraps fail on
the test suite. It's conceivable that some might fail on alternative
input. We can deal with those if/when they happen.
- In `trait_path` the `find_map`/`if let` is replaced with a loop, and
things end up much clearer that way.
- `named_span` no longer checks for an empty name; instead the call site
now checks for a missing identifier if necessary.
- `maybe_inline_local` doesn't need the `glob` argument, it can be
computed in-function from the `renamed` argument.
- `arbitrary_source_item_ordering::check_mod` had a big `if` statement
that was just getting the ident from the item kinds that had one. It
could be mostly replaced by a single call to the new `ItemKind::ident`
method.
- `ItemKind` grows from 56 to 64 bytes, but `Item` stays the same size,
and that's what matters, because `ItemKind` only occurs within `Item`.
95 lines
3.2 KiB
Rust
95 lines
3.2 KiB
Rust
use clippy_config::Conf;
|
|
use clippy_config::types::PubUnderscoreFieldsBehaviour;
|
|
use clippy_utils::attrs::is_doc_hidden;
|
|
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};
|
|
use rustc_session::impl_lint_pass;
|
|
|
|
declare_clippy_lint! {
|
|
/// ### What it does
|
|
/// Checks whether any field of the struct is prefixed with an `_` (underscore) and also marked
|
|
/// `pub` (public)
|
|
///
|
|
/// ### Why is this bad?
|
|
/// Fields prefixed with an `_` are inferred as unused, which suggests it should not be marked
|
|
/// as `pub`, because marking it as `pub` infers it will be used.
|
|
///
|
|
/// ### Example
|
|
/// ```rust
|
|
/// struct FileHandle {
|
|
/// pub _descriptor: usize,
|
|
/// }
|
|
/// ```
|
|
/// Use instead:
|
|
/// ```rust
|
|
/// struct FileHandle {
|
|
/// _descriptor: usize,
|
|
/// }
|
|
/// ```
|
|
///
|
|
/// OR
|
|
///
|
|
/// ```rust
|
|
/// struct FileHandle {
|
|
/// pub descriptor: usize,
|
|
/// }
|
|
/// ```
|
|
#[clippy::version = "1.77.0"]
|
|
pub PUB_UNDERSCORE_FIELDS,
|
|
pedantic,
|
|
"struct field prefixed with underscore and marked public"
|
|
}
|
|
|
|
pub struct PubUnderscoreFields {
|
|
behavior: PubUnderscoreFieldsBehaviour,
|
|
}
|
|
impl_lint_pass!(PubUnderscoreFields => [PUB_UNDERSCORE_FIELDS]);
|
|
|
|
impl PubUnderscoreFields {
|
|
pub fn new(conf: &'static Conf) -> Self {
|
|
Self {
|
|
behavior: conf.pub_underscore_fields_behavior,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<'tcx> LateLintPass<'tcx> for PubUnderscoreFields {
|
|
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
|
|
// This lint only pertains to structs.
|
|
let ItemKind::Struct(_, variant_data, _) = &item.kind else {
|
|
return;
|
|
};
|
|
|
|
let is_visible = |field: &FieldDef<'_>| match self.behavior {
|
|
PubUnderscoreFieldsBehaviour::PubliclyExported => cx.effective_visibilities.is_reachable(field.def_id),
|
|
PubUnderscoreFieldsBehaviour::AllPubFields => {
|
|
// If there is a visibility span then the field is marked pub in some way.
|
|
!field.vis_span.is_empty()
|
|
},
|
|
};
|
|
|
|
for field in variant_data.fields() {
|
|
// Only pertains to fields that start with an underscore, and are public.
|
|
if field.ident.as_str().starts_with('_') && is_visible(field)
|
|
// We ignore fields that have `#[doc(hidden)]`.
|
|
&& !is_doc_hidden(cx.tcx.hir_attrs(field.hir_id))
|
|
// We ignore fields that are `PhantomData`.
|
|
&& !is_path_lang_item(cx, field.ty, LangItem::PhantomData)
|
|
{
|
|
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 `_`",
|
|
|diag| {
|
|
diag.help("consider removing the underscore, or making the field private");
|
|
},
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|