2023-07-17 03:19:29 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
2023-09-26 22:56:38 -05:00
|
|
|
use clippy_utils::is_ty_alias;
|
2023-07-17 03:19:29 -05:00
|
|
|
use hir::def::Res;
|
|
|
|
use hir::ExprKind;
|
2023-05-05 10:45:49 -05:00
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
|
|
|
use rustc_middle::ty;
|
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2023-09-26 22:56:38 -05:00
|
|
|
use rustc_span::sym;
|
2023-05-05 10:45:49 -05:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
/// ### What it does
|
2023-07-02 07:35:19 -05:00
|
|
|
/// Checks for construction on unit struct using `default`.
|
2023-05-05 10:45:49 -05:00
|
|
|
///
|
|
|
|
/// ### Why is this bad?
|
|
|
|
/// This adds code complexity and an unnecessary function call.
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
/// ```rust
|
|
|
|
/// # use std::marker::PhantomData;
|
|
|
|
/// #[derive(Default)]
|
|
|
|
/// struct S<T> {
|
|
|
|
/// _marker: PhantomData<T>
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// let _: S<i32> = S {
|
|
|
|
/// _marker: PhantomData::default()
|
|
|
|
/// };
|
|
|
|
/// ```
|
|
|
|
/// Use instead:
|
|
|
|
/// ```rust
|
|
|
|
/// # use std::marker::PhantomData;
|
|
|
|
/// struct S<T> {
|
|
|
|
/// _marker: PhantomData<T>
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// let _: S<i32> = S {
|
|
|
|
/// _marker: PhantomData
|
|
|
|
/// };
|
|
|
|
/// ```
|
|
|
|
#[clippy::version = "1.71.0"]
|
|
|
|
pub DEFAULT_CONSTRUCTED_UNIT_STRUCTS,
|
|
|
|
complexity,
|
|
|
|
"unit structs can be contructed without calling `default`"
|
|
|
|
}
|
|
|
|
declare_lint_pass!(DefaultConstructedUnitStructs => [DEFAULT_CONSTRUCTED_UNIT_STRUCTS]);
|
|
|
|
|
2023-06-02 04:41:57 -05:00
|
|
|
fn is_alias(ty: hir::Ty<'_>) -> bool {
|
|
|
|
if let hir::TyKind::Path(ref qpath) = ty.kind {
|
|
|
|
is_ty_alias(qpath)
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-05 10:45:49 -05:00
|
|
|
impl LateLintPass<'_> for DefaultConstructedUnitStructs {
|
|
|
|
fn check_expr<'tcx>(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>) {
|
|
|
|
if_chain!(
|
|
|
|
// make sure we have a call to `Default::default`
|
|
|
|
if let hir::ExprKind::Call(fn_expr, &[]) = expr.kind;
|
2023-06-02 04:41:57 -05:00
|
|
|
if let ExprKind::Path(ref qpath @ hir::QPath::TypeRelative(base, _)) = fn_expr.kind;
|
|
|
|
// make sure this isn't a type alias:
|
|
|
|
// `<Foo as Bar>::Assoc` cannot be used as a constructor
|
|
|
|
if !is_alias(*base);
|
2023-05-05 10:45:49 -05:00
|
|
|
if let Res::Def(_, def_id) = cx.qpath_res(qpath, fn_expr.hir_id);
|
2023-09-26 22:56:38 -05:00
|
|
|
if cx.tcx.is_diagnostic_item(sym::default_fn, def_id);
|
2023-05-05 10:45:49 -05:00
|
|
|
// make sure we have a struct with no fields (unit struct)
|
|
|
|
if let ty::Adt(def, ..) = cx.typeck_results().expr_ty(expr).kind();
|
|
|
|
if def.is_struct();
|
|
|
|
if let var @ ty::VariantDef { ctor: Some((hir::def::CtorKind::Const, _)), .. } = def.non_enum_variant();
|
2023-05-20 08:39:26 -05:00
|
|
|
if !var.is_field_list_non_exhaustive();
|
|
|
|
if !expr.span.from_expansion() && !qpath.span().from_expansion();
|
2023-05-05 10:45:49 -05:00
|
|
|
then {
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
DEFAULT_CONSTRUCTED_UNIT_STRUCTS,
|
|
|
|
expr.span.with_lo(qpath.qself_span().hi()),
|
|
|
|
"use of `default` to create a unit struct",
|
|
|
|
"remove this call to `default`",
|
|
|
|
String::new(),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|