`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`.
89 lines
2.8 KiB
Rust
89 lines
2.8 KiB
Rust
use clippy_utils::diagnostics::span_lint;
|
|
use clippy_utils::return_ty;
|
|
use clippy_utils::ty::contains_adt_constructor;
|
|
use rustc_hir::{Impl, ImplItem, ImplItemKind, ItemKind, Node};
|
|
use rustc_lint::{LateContext, LateLintPass};
|
|
use rustc_session::declare_lint_pass;
|
|
|
|
declare_clippy_lint! {
|
|
/// ### What it does
|
|
/// Warns when constructors have the same name as their types.
|
|
///
|
|
/// ### Why is this bad?
|
|
/// Repeating the name of the type is redundant.
|
|
///
|
|
/// ### Example
|
|
/// ```rust,ignore
|
|
/// struct Foo {}
|
|
///
|
|
/// impl Foo {
|
|
/// pub fn foo() -> Foo {
|
|
/// Foo {}
|
|
/// }
|
|
/// }
|
|
/// ```
|
|
/// Use instead:
|
|
/// ```rust,ignore
|
|
/// struct Foo {}
|
|
///
|
|
/// impl Foo {
|
|
/// pub fn new() -> Foo {
|
|
/// Foo {}
|
|
/// }
|
|
/// }
|
|
/// ```
|
|
#[clippy::version = "1.55.0"]
|
|
pub SELF_NAMED_CONSTRUCTORS,
|
|
style,
|
|
"method should not have the same name as the type it is implemented for"
|
|
}
|
|
|
|
declare_lint_pass!(SelfNamedConstructors => [SELF_NAMED_CONSTRUCTORS]);
|
|
|
|
impl<'tcx> LateLintPass<'tcx> for SelfNamedConstructors {
|
|
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx ImplItem<'_>) {
|
|
match impl_item.kind {
|
|
ImplItemKind::Fn(ref sig, _) => {
|
|
if sig.decl.implicit_self.has_implicit_self() {
|
|
return;
|
|
}
|
|
},
|
|
_ => return,
|
|
}
|
|
|
|
let parent = cx.tcx.hir_get_parent_item(impl_item.hir_id()).def_id;
|
|
let item = cx.tcx.hir_expect_item(parent);
|
|
let self_ty = cx.tcx.type_of(item.owner_id).instantiate_identity();
|
|
let ret_ty = return_ty(cx, impl_item.owner_id);
|
|
|
|
// Do not check trait impls
|
|
if matches!(item.kind, ItemKind::Impl(Impl { of_trait: Some(_), .. })) {
|
|
return;
|
|
}
|
|
|
|
// Ensure method is constructor-like
|
|
if let Some(self_adt) = self_ty.ty_adt_def() {
|
|
if !contains_adt_constructor(ret_ty, self_adt) {
|
|
return;
|
|
}
|
|
} else if !ret_ty.contains(self_ty) {
|
|
return;
|
|
}
|
|
|
|
if let Some(self_def) = self_ty.ty_adt_def()
|
|
&& let Some(self_local_did) = self_def.did().as_local()
|
|
&& let Node::Item(x) = cx.tcx.hir_node_by_def_id(self_local_did)
|
|
&& let Some(type_ident) = x.kind.ident()
|
|
&& let type_name = type_ident.name.as_str().to_lowercase()
|
|
&& (impl_item.ident.name.as_str() == type_name
|
|
|| impl_item.ident.name.as_str().replace('_', "") == type_name)
|
|
{
|
|
span_lint(
|
|
cx,
|
|
SELF_NAMED_CONSTRUCTORS,
|
|
impl_item.span,
|
|
format!("constructor `{}` has the same name as the type", impl_item.ident.name),
|
|
);
|
|
}
|
|
}
|
|
}
|