1 use clippy_utils::{diagnostics::span_lint_and_sugg, is_ty_alias, match_def_path, paths};
2 use hir::{def::Res, ExprKind};
3 use rustc_errors::Applicability;
4 use rustc_hir as hir;
5 use rustc_lint::{LateContext, LateLintPass};
6 use rustc_middle::ty;
7 use rustc_session::{declare_lint_pass, declare_tool_lint};
8
9 declare_clippy_lint! {
10 /// ### What it does
11 /// Checks for construction on unit struct using `default`.
12 ///
13 /// ### Why is this bad?
14 /// This adds code complexity and an unnecessary function call.
15 ///
16 /// ### Example
17 /// ```rust
18 /// # use std::marker::PhantomData;
19 /// #[derive(Default)]
20 /// struct S<T> {
21 /// _marker: PhantomData<T>
22 /// }
23 ///
24 /// let _: S<i32> = S {
25 /// _marker: PhantomData::default()
26 /// };
27 /// ```
28 /// Use instead:
29 /// ```rust
30 /// # use std::marker::PhantomData;
31 /// struct S<T> {
32 /// _marker: PhantomData<T>
33 /// }
34 ///
35 /// let _: S<i32> = S {
36 /// _marker: PhantomData
37 /// };
38 /// ```
39 #[clippy::version = "1.71.0"]
40 pub DEFAULT_CONSTRUCTED_UNIT_STRUCTS,
41 complexity,
42 "unit structs can be contructed without calling `default`"
43 }
44 declare_lint_pass!(DefaultConstructedUnitStructs => [DEFAULT_CONSTRUCTED_UNIT_STRUCTS]);
45
is_alias(ty: hir::Ty<'_>) -> bool46 fn is_alias(ty: hir::Ty<'_>) -> bool {
47 if let hir::TyKind::Path(ref qpath) = ty.kind {
48 is_ty_alias(qpath)
49 } else {
50 false
51 }
52 }
53
54 impl LateLintPass<'_> for DefaultConstructedUnitStructs {
check_expr<'tcx>(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>)55 fn check_expr<'tcx>(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>) {
56 if_chain!(
57 // make sure we have a call to `Default::default`
58 if let hir::ExprKind::Call(fn_expr, &[]) = expr.kind;
59 if let ExprKind::Path(ref qpath @ hir::QPath::TypeRelative(base, _)) = fn_expr.kind;
60 // make sure this isn't a type alias:
61 // `<Foo as Bar>::Assoc` cannot be used as a constructor
62 if !is_alias(*base);
63 if let Res::Def(_, def_id) = cx.qpath_res(qpath, fn_expr.hir_id);
64 if match_def_path(cx, def_id, &paths::DEFAULT_TRAIT_METHOD);
65 // make sure we have a struct with no fields (unit struct)
66 if let ty::Adt(def, ..) = cx.typeck_results().expr_ty(expr).kind();
67 if def.is_struct();
68 if let var @ ty::VariantDef { ctor: Some((hir::def::CtorKind::Const, _)), .. } = def.non_enum_variant();
69 if !var.is_field_list_non_exhaustive();
70 if !expr.span.from_expansion() && !qpath.span().from_expansion();
71 then {
72 span_lint_and_sugg(
73 cx,
74 DEFAULT_CONSTRUCTED_UNIT_STRUCTS,
75 expr.span.with_lo(qpath.qself_span().hi()),
76 "use of `default` to create a unit struct",
77 "remove this call to `default`",
78 String::new(),
79 Applicability::MachineApplicable,
80 )
81 }
82 );
83 }
84 }
85