Skip to content

Commit 582779b

Browse files
committed
default_constructed_unit_structs -> is_unit_struct
Move is_unit_struct into clippy_utils to use it in the new lint default_mismatches_new
1 parent 377417c commit 582779b

2 files changed

Lines changed: 21 additions & 15 deletions

File tree

clippy_lints/src/default_constructed_unit_structs.rs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::is_ty_alias;
33
use clippy_utils::source::SpanRangeExt as _;
4+
use clippy_utils::ty::is_unit_struct;
45
use hir::ExprKind;
56
use hir::def::Res;
67
use rustc_errors::Applicability;
78
use rustc_hir as hir;
89
use rustc_lint::{LateContext, LateLintPass};
9-
use rustc_middle::ty;
1010
use rustc_session::declare_lint_pass;
1111
use rustc_span::sym;
1212

@@ -50,30 +50,20 @@ declare_lint_pass!(DefaultConstructedUnitStructs => [
5050
DEFAULT_CONSTRUCTED_UNIT_STRUCTS,
5151
]);
5252

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-
6153
impl LateLintPass<'_> for DefaultConstructedUnitStructs {
6254
fn check_expr<'tcx>(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>) {
6355
if let ExprKind::Call(fn_expr, &[]) = expr.kind
6456
// make sure we have a call to `Default::default`
6557
&& let ExprKind::Path(ref qpath @ hir::QPath::TypeRelative(base, _)) = fn_expr.kind
6658
// make sure this isn't a type alias:
6759
// `<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))
6961
&& let Res::Def(_, def_id) = cx.qpath_res(qpath, fn_expr.hir_id)
7062
&& cx.tcx.is_diagnostic_item(sym::default_fn, def_id)
7163
// 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()
7767
// do not suggest replacing an expression by a type name with placeholders
7868
&& !base.is_suggestable_infer_ty()
7969
{

clippy_utils/src/ty/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,22 @@ pub fn is_must_use_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
361361
}
362362
}
363363

364+
/// Returns `true` if the given type is a unit struct (a struct with no fields).
365+
pub fn is_unit_struct(ty: Ty<'_>) -> bool {
366+
if let ty::Adt(def, _) = ty.kind()
367+
&& def.is_struct()
368+
&& let var @ VariantDef {
369+
ctor: Some((CtorKind::Const, _)),
370+
..
371+
} = def.non_enum_variant()
372+
&& !var.is_field_list_non_exhaustive()
373+
{
374+
true
375+
} else {
376+
false
377+
}
378+
}
379+
364380
/// Returns `true` if the given type is a non aggregate primitive (a `bool` or `char`, any
365381
/// integer or floating-point number type).
366382
///

0 commit comments

Comments
 (0)