|
1 | 1 | use clippy_utils::diagnostics::span_lint_and_then; |
2 | 2 | use clippy_utils::is_ty_alias; |
3 | 3 | use clippy_utils::source::SpanRangeExt as _; |
| 4 | +use clippy_utils::ty::is_unit_struct; |
4 | 5 | use hir::ExprKind; |
5 | 6 | use hir::def::Res; |
6 | 7 | use rustc_errors::Applicability; |
7 | 8 | use rustc_hir as hir; |
8 | 9 | use rustc_lint::{LateContext, LateLintPass}; |
9 | | -use rustc_middle::ty; |
10 | 10 | use rustc_session::declare_lint_pass; |
11 | 11 | use rustc_span::sym; |
12 | 12 |
|
@@ -50,30 +50,20 @@ declare_lint_pass!(DefaultConstructedUnitStructs => [ |
50 | 50 | DEFAULT_CONSTRUCTED_UNIT_STRUCTS, |
51 | 51 | ]); |
52 | 52 |
|
53 | | -fn is_alias(ty: hir::Ty<'_>) -> bool { |
54 | | - if let hir::TyKind::Path(ref qpath) = ty.kind { |
55 | | - is_ty_alias(qpath) |
56 | | - } else { |
57 | | - false |
58 | | - } |
59 | | -} |
60 | | - |
61 | 53 | impl LateLintPass<'_> for DefaultConstructedUnitStructs { |
62 | 54 | fn check_expr<'tcx>(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>) { |
63 | 55 | if let ExprKind::Call(fn_expr, &[]) = expr.kind |
64 | 56 | // make sure we have a call to `Default::default` |
65 | 57 | && let ExprKind::Path(ref qpath @ hir::QPath::TypeRelative(base, _)) = fn_expr.kind |
66 | 58 | // make sure this isn't a type alias: |
67 | 59 | // `<Foo as Bar>::Assoc` cannot be used as a constructor |
68 | | - && !is_alias(*base) |
| 60 | + && !matches!(base.kind, hir::TyKind::Path(ref qpath) if is_ty_alias(qpath)) |
69 | 61 | && let Res::Def(_, def_id) = cx.qpath_res(qpath, fn_expr.hir_id) |
70 | 62 | && cx.tcx.is_diagnostic_item(sym::default_fn, def_id) |
71 | 63 | // make sure we have a struct with no fields (unit struct) |
72 | | - && let ty::Adt(def, ..) = cx.typeck_results().expr_ty(expr).kind() |
73 | | - && def.is_struct() |
74 | | - && let var @ ty::VariantDef { ctor: Some((hir::def::CtorKind::Const, _)), .. } = def.non_enum_variant() |
75 | | - && !var.is_field_list_non_exhaustive() |
76 | | - && !expr.span.from_expansion() && !qpath.span().from_expansion() |
| 64 | + && is_unit_struct(cx.typeck_results().expr_ty(expr)) |
| 65 | + && !expr.span.from_expansion() |
| 66 | + && !qpath.span().from_expansion() |
77 | 67 | // do not suggest replacing an expression by a type name with placeholders |
78 | 68 | && !base.is_suggestable_infer_ty() |
79 | 69 | { |
|
0 commit comments