Skip to content

Commit d12a358

Browse files
committed
use matches!() macro in more places
1 parent c34c015 commit d12a358

File tree

34 files changed

+138
-270
lines changed

34 files changed

+138
-270
lines changed

compiler/rustc_ast/src/ast.rs

+9-36
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,7 @@ pub enum GenericArgs {
167167

168168
impl GenericArgs {
169169
pub fn is_angle_bracketed(&self) -> bool {
170-
match *self {
171-
AngleBracketed(..) => true,
172-
_ => false,
173-
}
170+
matches!(self, AngleBracketed(..))
174171
}
175172

176173
pub fn span(&self) -> Span {
@@ -629,10 +626,7 @@ impl Pat {
629626

630627
/// Is this a `..` pattern?
631628
pub fn is_rest(&self) -> bool {
632-
match self.kind {
633-
PatKind::Rest => true,
634-
_ => false,
635-
}
629+
matches!(self.kind, PatKind::Rest)
636630
}
637631
}
638632

@@ -852,10 +846,7 @@ impl BinOpKind {
852846
}
853847
}
854848
pub fn lazy(&self) -> bool {
855-
match *self {
856-
BinOpKind::And | BinOpKind::Or => true,
857-
_ => false,
858-
}
849+
matches!(self, BinOpKind::And | BinOpKind::Or)
859850
}
860851

861852
pub fn is_comparison(&self) -> bool {
@@ -963,17 +954,11 @@ impl Stmt {
963954
}
964955

965956
pub fn is_item(&self) -> bool {
966-
match self.kind {
967-
StmtKind::Item(_) => true,
968-
_ => false,
969-
}
957+
matches!(self.kind, StmtKind::Item(_))
970958
}
971959

972960
pub fn is_expr(&self) -> bool {
973-
match self.kind {
974-
StmtKind::Expr(_) => true,
975-
_ => false,
976-
}
961+
matches!(self.kind, StmtKind::Expr(_))
977962
}
978963
}
979964

@@ -1652,26 +1637,17 @@ pub enum LitKind {
16521637
impl LitKind {
16531638
/// Returns `true` if this literal is a string.
16541639
pub fn is_str(&self) -> bool {
1655-
match *self {
1656-
LitKind::Str(..) => true,
1657-
_ => false,
1658-
}
1640+
matches!(self, LitKind::Str(..))
16591641
}
16601642

16611643
/// Returns `true` if this literal is byte literal string.
16621644
pub fn is_bytestr(&self) -> bool {
1663-
match self {
1664-
LitKind::ByteStr(_) => true,
1665-
_ => false,
1666-
}
1645+
matches!(self, LitKind::ByteStr(_))
16671646
}
16681647

16691648
/// Returns `true` if this is a numeric literal.
16701649
pub fn is_numeric(&self) -> bool {
1671-
match *self {
1672-
LitKind::Int(..) | LitKind::Float(..) => true,
1673-
_ => false,
1674-
}
1650+
matches!(self, LitKind::Int(..) | LitKind::Float(..))
16751651
}
16761652

16771653
/// Returns `true` if this literal has no suffix.
@@ -2237,10 +2213,7 @@ impl FnDecl {
22372213
self.inputs.get(0).map_or(false, Param::is_self)
22382214
}
22392215
pub fn c_variadic(&self) -> bool {
2240-
self.inputs.last().map_or(false, |arg| match arg.ty.kind {
2241-
TyKind::CVarArgs => true,
2242-
_ => false,
2243-
})
2216+
self.inputs.last().map_or(false, |arg| matches!(arg.ty.kind, TyKind::CVarArgs))
22442217
}
22452218
}
22462219

compiler/rustc_ast/src/attr/mod.rs

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

236236
pub fn is_word(&self) -> bool {
237-
match self.kind {
238-
MetaItemKind::Word => true,
239-
_ => false,
240-
}
237+
matches!(self.kind, MetaItemKind::Word)
241238
}
242239

243240
pub fn has_name(&self, name: Symbol) -> bool {

compiler/rustc_ast/src/token.rs

+19-34
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,7 @@ impl LitKind {
130130
}
131131

132132
crate fn may_have_suffix(self) -> bool {
133-
match self {
134-
Integer | Float | Err => true,
135-
_ => false,
136-
}
133+
matches!(self, Integer | Float | Err)
137134
}
138135
}
139136

@@ -305,10 +302,7 @@ impl TokenKind {
305302
}
306303

307304
pub fn should_end_const_arg(&self) -> bool {
308-
match self {
309-
Gt | Ge | BinOp(Shr) | BinOpEq(Shr) => true,
310-
_ => false,
311-
}
305+
matches!(self, Gt | Ge | BinOp(Shr) | BinOpEq(Shr))
312306
}
313307
}
314308

@@ -346,18 +340,21 @@ impl Token {
346340
}
347341

348342
pub fn is_op(&self) -> bool {
349-
match self.kind {
350-
OpenDelim(..) | CloseDelim(..) | Literal(..) | DocComment(..) | Ident(..)
351-
| Lifetime(..) | Interpolated(..) | Eof => false,
352-
_ => true,
353-
}
343+
!matches!(
344+
self.kind,
345+
OpenDelim(..)
346+
| CloseDelim(..)
347+
| Literal(..)
348+
| DocComment(..)
349+
| Ident(..)
350+
| Lifetime(..)
351+
| Interpolated(..)
352+
| Eof
353+
)
354354
}
355355

356356
pub fn is_like_plus(&self) -> bool {
357-
match self.kind {
358-
BinOp(Plus) | BinOpEq(Plus) => true,
359-
_ => false,
360-
}
357+
matches!(self.kind, BinOp(Plus) | BinOpEq(Plus))
361358
}
362359

363360
/// Returns `true` if the token can appear at the start of an expression.
@@ -379,13 +376,10 @@ impl Token {
379376
ModSep | // global path
380377
Lifetime(..) | // labeled loop
381378
Pound => true, // expression attributes
382-
Interpolated(ref nt) => match **nt {
383-
NtLiteral(..) |
379+
Interpolated(ref nt) => matches!(**nt, NtLiteral(..) |
384380
NtExpr(..) |
385381
NtBlock(..) |
386-
NtPath(..) => true,
387-
_ => false,
388-
},
382+
NtPath(..)),
389383
_ => false,
390384
}
391385
}
@@ -405,10 +399,7 @@ impl Token {
405399
Lifetime(..) | // lifetime bound in trait object
406400
Lt | BinOp(Shl) | // associated path
407401
ModSep => true, // global path
408-
Interpolated(ref nt) => match **nt {
409-
NtTy(..) | NtPath(..) => true,
410-
_ => false,
411-
},
402+
Interpolated(ref nt) => matches!(**nt, NtTy(..) | NtPath(..)),
412403
_ => false,
413404
}
414405
}
@@ -417,10 +408,7 @@ impl Token {
417408
pub fn can_begin_const_arg(&self) -> bool {
418409
match self.kind {
419410
OpenDelim(Brace) => true,
420-
Interpolated(ref nt) => match **nt {
421-
NtExpr(..) | NtBlock(..) | NtLiteral(..) => true,
422-
_ => false,
423-
},
411+
Interpolated(ref nt) => matches!(**nt, NtExpr(..) | NtBlock(..) | NtLiteral(..)),
424412
_ => self.can_begin_literal_maybe_minus(),
425413
}
426414
}
@@ -436,10 +424,7 @@ impl Token {
436424

437425
/// Returns `true` if the token is any literal.
438426
pub fn is_lit(&self) -> bool {
439-
match self.kind {
440-
Literal(..) => true,
441-
_ => false,
442-
}
427+
matches!(self.kind, Literal(..))
443428
}
444429

445430
/// Returns `true` if the token is any literal, a minus (which can prefix a literal,

compiler/rustc_ast/src/util/classify.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ use crate::ast;
1212
/// |x| 5
1313
/// isn't parsed as (if true {...} else {...} | x) | 5
1414
pub fn expr_requires_semi_to_be_stmt(e: &ast::Expr) -> bool {
15-
match e.kind {
15+
!matches!(
16+
e.kind,
1617
ast::ExprKind::If(..)
17-
| ast::ExprKind::Match(..)
18-
| ast::ExprKind::Block(..)
19-
| ast::ExprKind::While(..)
20-
| ast::ExprKind::Loop(..)
21-
| ast::ExprKind::ForLoop(..)
22-
| ast::ExprKind::TryBlock(..) => false,
23-
_ => true,
24-
}
18+
| ast::ExprKind::Match(..)
19+
| ast::ExprKind::Block(..)
20+
| ast::ExprKind::While(..)
21+
| ast::ExprKind::Loop(..)
22+
| ast::ExprKind::ForLoop(..)
23+
| ast::ExprKind::TryBlock(..)
24+
)
2525
}

compiler/rustc_ast/src/util/comments.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,8 @@ pub fn gather_comments(sm: &SourceMap, path: FileName, src: String) -> Vec<Comme
178178
}
179179
rustc_lexer::TokenKind::BlockComment { doc_style, .. } => {
180180
if doc_style.is_none() {
181-
let code_to_the_right = match text[pos + token.len..].chars().next() {
182-
Some('\r' | '\n') => false,
183-
_ => true,
184-
};
181+
let code_to_the_right =
182+
!matches!(text[pos + token.len..].chars().next(), Some('\r' | '\n'));
185183
let style = match (code_to_the_left, code_to_the_right) {
186184
(_, true) => CommentStyle::Mixed,
187185
(false, false) => CommentStyle::Isolated,

compiler/rustc_builtin_macros/src/deriving/clone.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,9 @@ pub fn expand_deriving_clone(
3838
| ItemKind::Enum(_, Generics { ref params, .. }) => {
3939
let container_id = cx.current_expansion.id.expn_data().parent;
4040
if cx.resolver.has_derive_copy(container_id)
41-
&& !params.iter().any(|param| match param.kind {
42-
ast::GenericParamKind::Type { .. } => true,
43-
_ => false,
44-
})
41+
&& !params
42+
.iter()
43+
.any(|param| matches!(param.kind, ast::GenericParamKind::Type { .. }))
4544
{
4645
bounds = vec![];
4746
is_shallow = true;

compiler/rustc_builtin_macros/src/deriving/generic/mod.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -404,12 +404,10 @@ impl<'a> TraitDef<'a> {
404404
let has_no_type_params = match item.kind {
405405
ast::ItemKind::Struct(_, ref generics)
406406
| ast::ItemKind::Enum(_, ref generics)
407-
| ast::ItemKind::Union(_, ref generics) => {
408-
!generics.params.iter().any(|param| match param.kind {
409-
ast::GenericParamKind::Type { .. } => true,
410-
_ => false,
411-
})
412-
}
407+
| ast::ItemKind::Union(_, ref generics) => !generics
408+
.params
409+
.iter()
410+
.any(|param| matches!(param.kind, ast::GenericParamKind::Type { .. })),
413411
_ => unreachable!(),
414412
};
415413
let container_id = cx.current_expansion.id.expn_data().parent;
@@ -868,7 +866,7 @@ impl<'a> MethodDef<'a> {
868866
Self_ if nonstatic => {
869867
self_args.push(arg_expr);
870868
}
871-
Ptr(ref ty, _) if (if let Self_ = **ty { true } else { false }) && nonstatic => {
869+
Ptr(ref ty, _) if matches!(**ty, Self_) && nonstatic => {
872870
self_args.push(cx.expr_deref(trait_.span, arg_expr))
873871
}
874872
_ => {

compiler/rustc_builtin_macros/src/format.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1044,10 +1044,7 @@ pub fn expand_preparsed_format_args(
10441044

10451045
let numbered_position_args = pieces.iter().any(|arg: &parse::Piece<'_>| match *arg {
10461046
parse::String(_) => false,
1047-
parse::NextArgument(arg) => match arg.position {
1048-
parse::Position::ArgumentIs(_) => true,
1049-
_ => false,
1050-
},
1047+
parse::NextArgument(arg) => matches!(arg.position, parse::Position::ArgumentIs(_)),
10511048
});
10521049

10531050
cx.build_index_map();

compiler/rustc_builtin_macros/src/format_foreign.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -580,10 +580,7 @@ pub mod printf {
580580
}
581581

582582
fn is_flag(c: &char) -> bool {
583-
match c {
584-
'0' | '-' | '+' | ' ' | '#' | '\'' => true,
585-
_ => false,
586-
}
583+
matches!(c, '0' | '-' | '+' | ' ' | '#' | '\'')
587584
}
588585

589586
#[cfg(test)]

compiler/rustc_builtin_macros/src/llvm_asm.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,11 @@ fn parse_inline_asm<'a>(
8787
// parsed as `llvm_asm!(z)` with `z = "x": y` which is type ascription.
8888
let first_colon = tts
8989
.trees()
90-
.position(|tt| match tt {
91-
tokenstream::TokenTree::Token(Token { kind: token::Colon | token::ModSep, .. }) => true,
92-
_ => false,
90+
.position(|tt| {
91+
matches!(
92+
tt,
93+
tokenstream::TokenTree::Token(Token { kind: token::Colon | token::ModSep, .. })
94+
)
9395
})
9496
.unwrap_or(tts.len());
9597
let mut p = cx.new_parser_from_tts(tts.trees().skip(first_colon).collect());

compiler/rustc_builtin_macros/src/proc_macro_harness.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,7 @@ impl<'a> Visitor<'a> for CollectProcMacros<'a> {
256256
// we're just not interested in this item.
257257
//
258258
// If we find one, try to locate a `#[proc_macro_derive]` attribute on it.
259-
let is_fn = match item.kind {
260-
ast::ItemKind::Fn(..) => true,
261-
_ => false,
262-
};
259+
let is_fn = matches!(item.kind, ast::ItemKind::Fn(..));
263260

264261
let mut found_attr: Option<&'a ast::Attribute> = None;
265262

compiler/rustc_codegen_ssa/src/mir/analyze.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,12 @@ impl<Bx: BuilderMethods<'a, 'tcx>> LocalAnalyzer<'mir, 'a, 'tcx, Bx> {
112112
};
113113

114114
// Allow uses of projections that are ZSTs or from scalar fields.
115-
let is_consume = match context {
115+
let is_consume = matches!(
116+
context,
116117
PlaceContext::NonMutatingUse(
117118
NonMutatingUseContext::Copy | NonMutatingUseContext::Move,
118-
) => true,
119-
_ => false,
120-
};
119+
)
120+
);
121121
if is_consume {
122122
let base_ty =
123123
mir::Place::ty_from(place_ref.local, proj_base, self.fx.mir, cx.tcx());

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

+1-4
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,7 @@ impl Visitor<'tcx> for TypeParamSpanVisitor<'tcx> {
132132
[segment]
133133
if segment
134134
.res
135-
.map(|res| match res {
136-
Res::SelfTy(_, _) | Res::Def(hir::def::DefKind::TyParam, _) => true,
137-
_ => false,
138-
})
135+
.map(|res| matches!(res, Res::SelfTy(_, _) | Res::Def(hir::def::DefKind::TyParam, _)))
139136
.unwrap_or(false) =>
140137
{
141138
self.types.push(path.span);

compiler/rustc_infer/src/infer/free_regions.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,7 @@ impl<'tcx> FreeRegionMap<'tcx> {
9393

9494
/// True for free regions other than `'static`.
9595
pub fn is_free(&self, r: Region<'_>) -> bool {
96-
match *r {
97-
ty::ReEarlyBound(_) | ty::ReFree(_) => true,
98-
_ => false,
99-
}
96+
matches!(r, ty::ReEarlyBound(_) | ty::ReFree(_))
10097
}
10198

10299
/// True if `r` is a free region or static of the sort that this

0 commit comments

Comments
 (0)