|
1 | | -use clippy_utils::{diagnostics::span_lint_and_sugg, match_def_path, paths}; |
| 1 | +use clippy_utils::{diagnostics::span_lint_and_sugg, is_ty_alias, match_def_path, paths}; |
2 | 2 | use hir::{def::Res, ExprKind}; |
3 | 3 | use rustc_errors::Applicability; |
4 | 4 | use rustc_hir as hir; |
@@ -43,12 +43,23 @@ declare_clippy_lint! { |
43 | 43 | } |
44 | 44 | declare_lint_pass!(DefaultConstructedUnitStructs => [DEFAULT_CONSTRUCTED_UNIT_STRUCTS]); |
45 | 45 |
|
| 46 | +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 | + |
46 | 54 | impl LateLintPass<'_> for DefaultConstructedUnitStructs { |
47 | 55 | fn check_expr<'tcx>(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>) { |
48 | 56 | if_chain!( |
49 | 57 | // make sure we have a call to `Default::default` |
50 | 58 | if let hir::ExprKind::Call(fn_expr, &[]) = expr.kind; |
51 | | - if let ExprKind::Path(ref qpath@ hir::QPath::TypeRelative(_,_)) = fn_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); |
52 | 63 | if let Res::Def(_, def_id) = cx.qpath_res(qpath, fn_expr.hir_id); |
53 | 64 | if match_def_path(cx, def_id, &paths::DEFAULT_TRAIT_METHOD); |
54 | 65 | // make sure we have a struct with no fields (unit struct) |
|
0 commit comments