Skip to content

Commit bcc15bb

Browse files
committed
use matches! macro in more places
1 parent e6e956d commit bcc15bb

File tree

13 files changed

+52
-75
lines changed

13 files changed

+52
-75
lines changed

compiler/rustc_ast/src/ast.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -1298,17 +1298,17 @@ impl Expr {
12981298

12991299
/// To a first-order approximation, is this a pattern?
13001300
pub fn is_approximately_pattern(&self) -> bool {
1301-
match &self.peel_parens().kind {
1301+
matches!(
1302+
&self.peel_parens().kind,
13021303
ExprKind::Array(_)
1303-
| ExprKind::Call(_, _)
1304-
| ExprKind::Tup(_)
1305-
| ExprKind::Lit(_)
1306-
| ExprKind::Range(_, _, _)
1307-
| ExprKind::Underscore
1308-
| ExprKind::Path(_, _)
1309-
| ExprKind::Struct(_) => true,
1310-
_ => false,
1311-
}
1304+
| ExprKind::Call(_, _)
1305+
| ExprKind::Tup(_)
1306+
| ExprKind::Lit(_)
1307+
| ExprKind::Range(_, _, _)
1308+
| ExprKind::Underscore
1309+
| ExprKind::Path(_, _)
1310+
| ExprKind::Struct(_)
1311+
)
13121312
}
13131313
}
13141314

compiler/rustc_ast_lowering/src/lib.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -332,10 +332,7 @@ enum FnDeclKind {
332332

333333
impl FnDeclKind {
334334
fn param_impl_trait_allowed(&self) -> bool {
335-
match self {
336-
FnDeclKind::Fn | FnDeclKind::Inherent | FnDeclKind::Impl | FnDeclKind::Trait => true,
337-
_ => false,
338-
}
335+
matches!(self, FnDeclKind::Fn | FnDeclKind::Inherent | FnDeclKind::Impl | FnDeclKind::Trait)
339336
}
340337

341338
fn return_impl_trait_allowed(&self, tcx: TyCtxt<'_>) -> bool {

compiler/rustc_hir/src/def.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,7 @@ impl DefKind {
234234

235235
#[inline]
236236
pub fn is_fn_like(self) -> bool {
237-
match self {
238-
DefKind::Fn | DefKind::AssocFn | DefKind::Closure | DefKind::Generator => true,
239-
_ => false,
240-
}
237+
matches!(self, DefKind::Fn | DefKind::AssocFn | DefKind::Closure | DefKind::Generator)
241238
}
242239

243240
/// Whether `query get_codegen_attrs` should be used with this definition.

compiler/rustc_hir_analysis/src/collect.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1457,10 +1457,7 @@ fn compute_sig_of_foreign_fn_decl<'tcx>(
14571457
}
14581458

14591459
fn is_foreign_item(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
1460-
match tcx.hir().get_by_def_id(def_id) {
1461-
Node::ForeignItem(..) => true,
1462-
_ => false,
1463-
}
1460+
matches!(tcx.hir().get_by_def_id(def_id), Node::ForeignItem(..))
14641461
}
14651462

14661463
fn generator_kind(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<hir::GeneratorKind> {

compiler/rustc_hir_typeck/src/expr.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1735,10 +1735,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17351735
} else {
17361736
self.check_expr_has_type_or_error(base_expr, adt_ty, |_| {
17371737
let base_ty = self.typeck_results.borrow().expr_ty(*base_expr);
1738-
let same_adt = match (adt_ty.kind(), base_ty.kind()) {
1739-
(ty::Adt(adt, _), ty::Adt(base_adt, _)) if adt == base_adt => true,
1740-
_ => false,
1741-
};
1738+
let same_adt = matches!((adt_ty.kind(), base_ty.kind()),
1739+
(ty::Adt(adt, _), ty::Adt(base_adt, _)) if adt == base_adt);
17421740
if self.tcx.sess.is_nightly_build() && same_adt {
17431741
feature_err(
17441742
&self.tcx.sess.parse_sess,

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -312,13 +312,10 @@ pub fn suggest_new_region_bound(
312312
Applicability::MaybeIncorrect,
313313
);
314314
}
315-
} else if opaque.bounds.iter().any(|arg| match arg {
316-
GenericBound::Outlives(Lifetime { ident, .. })
317-
if ident.name.to_string() == lifetime_name =>
318-
{
319-
true
320-
}
321-
_ => false,
315+
} else if opaque.bounds.iter().any(|arg| {
316+
matches!(arg,
317+
GenericBound::Outlives(Lifetime { ident, .. })
318+
if ident.name.to_string() == lifetime_name )
322319
}) {
323320
} else {
324321
// get a lifetime name of existing named lifetimes if any

compiler/rustc_infer/src/infer/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1577,10 +1577,10 @@ impl<'tcx> InferCtxt<'tcx> {
15771577
(TyOrConstInferVar::Ty(ty_var), Ok(inner)) => {
15781578
use self::type_variable::TypeVariableValue;
15791579

1580-
match inner.try_type_variables_probe_ref(ty_var) {
1581-
Some(TypeVariableValue::Unknown { .. }) => true,
1582-
_ => false,
1583-
}
1580+
matches!(
1581+
inner.try_type_variables_probe_ref(ty_var),
1582+
Some(TypeVariableValue::Unknown { .. })
1583+
)
15841584
}
15851585
_ => false,
15861586
};

compiler/rustc_infer/src/traits/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ impl<'tcx> PredicateObligation<'tcx> {
8989
impl<'tcx> TraitObligation<'tcx> {
9090
/// Returns `true` if the trait predicate is considered `const` in its ParamEnv.
9191
pub fn is_const(&self) -> bool {
92-
match (self.predicate.skip_binder().constness, self.param_env.constness()) {
93-
(ty::BoundConstness::ConstIfConst, hir::Constness::Const) => true,
94-
_ => false,
95-
}
92+
matches!(
93+
(self.predicate.skip_binder().constness, self.param_env.constness()),
94+
(ty::BoundConstness::ConstIfConst, hir::Constness::Const)
95+
)
9696
}
9797

9898
pub fn derived_cause(

compiler/rustc_mir_build/src/errors.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -384,13 +384,8 @@ impl<'a> IntoDiagnostic<'a> for NonExhaustivePatternsTypeNotEmpty<'_, '_, '_> {
384384
diag.span_note(span, fluent::mir_build_def_note);
385385
}
386386

387-
let is_variant_list_non_exhaustive = match self.ty.kind() {
388-
ty::Adt(def, _) if def.is_variant_list_non_exhaustive() && !def.did().is_local() => {
389-
true
390-
}
391-
_ => false,
392-
};
393-
387+
let is_variant_list_non_exhaustive = matches!(self.ty.kind(),
388+
ty::Adt(def, _) if def.is_variant_list_non_exhaustive() && !def.did().is_local());
394389
if is_variant_list_non_exhaustive {
395390
diag.note(fluent::mir_build_non_exhaustive_type_note);
396391
} else {

compiler/rustc_mir_build/src/thir/pattern/check_match.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -671,10 +671,8 @@ fn non_exhaustive_match<'p, 'tcx>(
671671
};
672672
};
673673

674-
let is_variant_list_non_exhaustive = match scrut_ty.kind() {
675-
ty::Adt(def, _) if def.is_variant_list_non_exhaustive() && !def.did().is_local() => true,
676-
_ => false,
677-
};
674+
let is_variant_list_non_exhaustive = matches!(scrut_ty.kind(),
675+
ty::Adt(def, _) if def.is_variant_list_non_exhaustive() && !def.did().is_local());
678676

679677
adt_defined_here(cx, &mut err, scrut_ty, &witnesses);
680678
err.note(&format!(

compiler/rustc_parse/src/parser/item.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -2577,14 +2577,12 @@ impl<'a> Parser<'a> {
25772577
}
25782578

25792579
fn recover_self_param(&mut self) -> bool {
2580-
match self
2581-
.parse_outer_attributes()
2582-
.and_then(|_| self.parse_self_param())
2583-
.map_err(|e| e.cancel())
2584-
{
2585-
Ok(Some(_)) => true,
2586-
_ => false,
2587-
}
2580+
matches!(
2581+
self.parse_outer_attributes()
2582+
.and_then(|_| self.parse_self_param())
2583+
.map_err(|e| e.cancel()),
2584+
Ok(Some(_))
2585+
)
25882586
}
25892587
}
25902588

compiler/rustc_parse/src/parser/nonterminal.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,10 @@ impl<'a> Parser<'a> {
2020
pub fn nonterminal_may_begin_with(kind: NonterminalKind, token: &Token) -> bool {
2121
/// Checks whether the non-terminal may contain a single (non-keyword) identifier.
2222
fn may_be_ident(nt: &token::Nonterminal) -> bool {
23-
match *nt {
24-
token::NtItem(_) | token::NtBlock(_) | token::NtVis(_) | token::NtLifetime(_) => {
25-
false
26-
}
27-
_ => true,
28-
}
23+
!matches!(
24+
*nt,
25+
token::NtItem(_) | token::NtBlock(_) | token::NtVis(_) | token::NtLifetime(_)
26+
)
2927
}
3028

3129
match kind {

compiler/rustc_resolve/src/diagnostics.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -663,15 +663,17 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
663663
Ident::with_dummy_span(name),
664664
Namespace::ValueNS,
665665
&parent_scope,
666-
&|res: Res| match res {
667-
Res::Def(
668-
DefKind::Ctor(CtorOf::Variant, CtorKind::Const)
669-
| DefKind::Ctor(CtorOf::Struct, CtorKind::Const)
670-
| DefKind::Const
671-
| DefKind::AssocConst,
672-
_,
673-
) => true,
674-
_ => false,
666+
&|res: Res| {
667+
matches!(
668+
res,
669+
Res::Def(
670+
DefKind::Ctor(CtorOf::Variant, CtorKind::Const)
671+
| DefKind::Ctor(CtorOf::Struct, CtorKind::Const)
672+
| DefKind::Const
673+
| DefKind::AssocConst,
674+
_,
675+
)
676+
)
675677
},
676678
);
677679

0 commit comments

Comments
 (0)