Skip to content

Commit

Permalink
Rollup merge of #80344 - matthiaskrgr:matches, r=Dylan-DPC
Browse files Browse the repository at this point in the history
use matches!() macro in more places
  • Loading branch information
Dylan-DPC authored Dec 28, 2020
2 parents 3f8c979 + d12a358 commit c51172f
Show file tree
Hide file tree
Showing 33 changed files with 137 additions and 266 deletions.
45 changes: 9 additions & 36 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,7 @@ pub enum GenericArgs {

impl GenericArgs {
pub fn is_angle_bracketed(&self) -> bool {
match *self {
AngleBracketed(..) => true,
_ => false,
}
matches!(self, AngleBracketed(..))
}

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

/// Is this a `..` pattern?
pub fn is_rest(&self) -> bool {
match self.kind {
PatKind::Rest => true,
_ => false,
}
matches!(self.kind, PatKind::Rest)
}
}

Expand Down Expand Up @@ -852,10 +846,7 @@ impl BinOpKind {
}
}
pub fn lazy(&self) -> bool {
match *self {
BinOpKind::And | BinOpKind::Or => true,
_ => false,
}
matches!(self, BinOpKind::And | BinOpKind::Or)
}

pub fn is_comparison(&self) -> bool {
Expand Down Expand Up @@ -963,17 +954,11 @@ impl Stmt {
}

pub fn is_item(&self) -> bool {
match self.kind {
StmtKind::Item(_) => true,
_ => false,
}
matches!(self.kind, StmtKind::Item(_))
}

pub fn is_expr(&self) -> bool {
match self.kind {
StmtKind::Expr(_) => true,
_ => false,
}
matches!(self.kind, StmtKind::Expr(_))
}
}

Expand Down Expand Up @@ -1652,26 +1637,17 @@ pub enum LitKind {
impl LitKind {
/// Returns `true` if this literal is a string.
pub fn is_str(&self) -> bool {
match *self {
LitKind::Str(..) => true,
_ => false,
}
matches!(self, LitKind::Str(..))
}

/// Returns `true` if this literal is byte literal string.
pub fn is_bytestr(&self) -> bool {
match self {
LitKind::ByteStr(_) => true,
_ => false,
}
matches!(self, LitKind::ByteStr(_))
}

/// Returns `true` if this is a numeric literal.
pub fn is_numeric(&self) -> bool {
match *self {
LitKind::Int(..) | LitKind::Float(..) => true,
_ => false,
}
matches!(self, LitKind::Int(..) | LitKind::Float(..))
}

/// Returns `true` if this literal has no suffix.
Expand Down Expand Up @@ -2237,10 +2213,7 @@ impl FnDecl {
self.inputs.get(0).map_or(false, Param::is_self)
}
pub fn c_variadic(&self) -> bool {
self.inputs.last().map_or(false, |arg| match arg.ty.kind {
TyKind::CVarArgs => true,
_ => false,
})
self.inputs.last().map_or(false, |arg| matches!(arg.ty.kind, TyKind::CVarArgs))
}
}

Expand Down
5 changes: 1 addition & 4 deletions compiler/rustc_ast/src/attr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,7 @@ impl MetaItem {
}

pub fn is_word(&self) -> bool {
match self.kind {
MetaItemKind::Word => true,
_ => false,
}
matches!(self.kind, MetaItemKind::Word)
}

pub fn has_name(&self, name: Symbol) -> bool {
Expand Down
53 changes: 19 additions & 34 deletions compiler/rustc_ast/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,7 @@ impl LitKind {
}

crate fn may_have_suffix(self) -> bool {
match self {
Integer | Float | Err => true,
_ => false,
}
matches!(self, Integer | Float | Err)
}
}

Expand Down Expand Up @@ -305,10 +302,7 @@ impl TokenKind {
}

pub fn should_end_const_arg(&self) -> bool {
match self {
Gt | Ge | BinOp(Shr) | BinOpEq(Shr) => true,
_ => false,
}
matches!(self, Gt | Ge | BinOp(Shr) | BinOpEq(Shr))
}
}

Expand Down Expand Up @@ -346,18 +340,21 @@ impl Token {
}

pub fn is_op(&self) -> bool {
match self.kind {
OpenDelim(..) | CloseDelim(..) | Literal(..) | DocComment(..) | Ident(..)
| Lifetime(..) | Interpolated(..) | Eof => false,
_ => true,
}
!matches!(
self.kind,
OpenDelim(..)
| CloseDelim(..)
| Literal(..)
| DocComment(..)
| Ident(..)
| Lifetime(..)
| Interpolated(..)
| Eof
)
}

pub fn is_like_plus(&self) -> bool {
match self.kind {
BinOp(Plus) | BinOpEq(Plus) => true,
_ => false,
}
matches!(self.kind, BinOp(Plus) | BinOpEq(Plus))
}

/// Returns `true` if the token can appear at the start of an expression.
Expand All @@ -379,13 +376,10 @@ impl Token {
ModSep | // global path
Lifetime(..) | // labeled loop
Pound => true, // expression attributes
Interpolated(ref nt) => match **nt {
NtLiteral(..) |
Interpolated(ref nt) => matches!(**nt, NtLiteral(..) |
NtExpr(..) |
NtBlock(..) |
NtPath(..) => true,
_ => false,
},
NtPath(..)),
_ => false,
}
}
Expand All @@ -405,10 +399,7 @@ impl Token {
Lifetime(..) | // lifetime bound in trait object
Lt | BinOp(Shl) | // associated path
ModSep => true, // global path
Interpolated(ref nt) => match **nt {
NtTy(..) | NtPath(..) => true,
_ => false,
},
Interpolated(ref nt) => matches!(**nt, NtTy(..) | NtPath(..)),
_ => false,
}
}
Expand All @@ -417,10 +408,7 @@ impl Token {
pub fn can_begin_const_arg(&self) -> bool {
match self.kind {
OpenDelim(Brace) => true,
Interpolated(ref nt) => match **nt {
NtExpr(..) | NtBlock(..) | NtLiteral(..) => true,
_ => false,
},
Interpolated(ref nt) => matches!(**nt, NtExpr(..) | NtBlock(..) | NtLiteral(..)),
_ => self.can_begin_literal_maybe_minus(),
}
}
Expand All @@ -436,10 +424,7 @@ impl Token {

/// Returns `true` if the token is any literal.
pub fn is_lit(&self) -> bool {
match self.kind {
Literal(..) => true,
_ => false,
}
matches!(self.kind, Literal(..))
}

/// Returns `true` if the token is any literal, a minus (which can prefix a literal,
Expand Down
18 changes: 9 additions & 9 deletions compiler/rustc_ast/src/util/classify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ use crate::ast;
/// |x| 5
/// isn't parsed as (if true {...} else {...} | x) | 5
pub fn expr_requires_semi_to_be_stmt(e: &ast::Expr) -> bool {
match e.kind {
!matches!(
e.kind,
ast::ExprKind::If(..)
| ast::ExprKind::Match(..)
| ast::ExprKind::Block(..)
| ast::ExprKind::While(..)
| ast::ExprKind::Loop(..)
| ast::ExprKind::ForLoop(..)
| ast::ExprKind::TryBlock(..) => false,
_ => true,
}
| ast::ExprKind::Match(..)
| ast::ExprKind::Block(..)
| ast::ExprKind::While(..)
| ast::ExprKind::Loop(..)
| ast::ExprKind::ForLoop(..)
| ast::ExprKind::TryBlock(..)
)
}
6 changes: 2 additions & 4 deletions compiler/rustc_ast/src/util/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,8 @@ pub fn gather_comments(sm: &SourceMap, path: FileName, src: String) -> Vec<Comme
}
rustc_lexer::TokenKind::BlockComment { doc_style, .. } => {
if doc_style.is_none() {
let code_to_the_right = match text[pos + token.len..].chars().next() {
Some('\r' | '\n') => false,
_ => true,
};
let code_to_the_right =
!matches!(text[pos + token.len..].chars().next(), Some('\r' | '\n'));
let style = match (code_to_the_left, code_to_the_right) {
(_, true) => CommentStyle::Mixed,
(false, false) => CommentStyle::Isolated,
Expand Down
7 changes: 3 additions & 4 deletions compiler/rustc_builtin_macros/src/deriving/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ pub fn expand_deriving_clone(
| ItemKind::Enum(_, Generics { ref params, .. }) => {
let container_id = cx.current_expansion.id.expn_data().parent;
if cx.resolver.has_derive_copy(container_id)
&& !params.iter().any(|param| match param.kind {
ast::GenericParamKind::Type { .. } => true,
_ => false,
})
&& !params
.iter()
.any(|param| matches!(param.kind, ast::GenericParamKind::Type { .. }))
{
bounds = vec![];
is_shallow = true;
Expand Down
12 changes: 5 additions & 7 deletions compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,12 +404,10 @@ impl<'a> TraitDef<'a> {
let has_no_type_params = match item.kind {
ast::ItemKind::Struct(_, ref generics)
| ast::ItemKind::Enum(_, ref generics)
| ast::ItemKind::Union(_, ref generics) => {
!generics.params.iter().any(|param| match param.kind {
ast::GenericParamKind::Type { .. } => true,
_ => false,
})
}
| ast::ItemKind::Union(_, ref generics) => !generics
.params
.iter()
.any(|param| matches!(param.kind, ast::GenericParamKind::Type { .. })),
_ => unreachable!(),
};
let container_id = cx.current_expansion.id.expn_data().parent;
Expand Down Expand Up @@ -868,7 +866,7 @@ impl<'a> MethodDef<'a> {
Self_ if nonstatic => {
self_args.push(arg_expr);
}
Ptr(ref ty, _) if (if let Self_ = **ty { true } else { false }) && nonstatic => {
Ptr(ref ty, _) if matches!(**ty, Self_) && nonstatic => {
self_args.push(cx.expr_deref(trait_.span, arg_expr))
}
_ => {
Expand Down
5 changes: 1 addition & 4 deletions compiler/rustc_builtin_macros/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1044,10 +1044,7 @@ pub fn expand_preparsed_format_args(

let numbered_position_args = pieces.iter().any(|arg: &parse::Piece<'_>| match *arg {
parse::String(_) => false,
parse::NextArgument(arg) => match arg.position {
parse::Position::ArgumentIs(_) => true,
_ => false,
},
parse::NextArgument(arg) => matches!(arg.position, parse::Position::ArgumentIs(_)),
});

cx.build_index_map();
Expand Down
5 changes: 1 addition & 4 deletions compiler/rustc_builtin_macros/src/format_foreign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,10 +580,7 @@ pub mod printf {
}

fn is_flag(c: &char) -> bool {
match c {
'0' | '-' | '+' | ' ' | '#' | '\'' => true,
_ => false,
}
matches!(c, '0' | '-' | '+' | ' ' | '#' | '\'')
}

#[cfg(test)]
Expand Down
8 changes: 5 additions & 3 deletions compiler/rustc_builtin_macros/src/llvm_asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,11 @@ fn parse_inline_asm<'a>(
// parsed as `llvm_asm!(z)` with `z = "x": y` which is type ascription.
let first_colon = tts
.trees()
.position(|tt| match tt {
tokenstream::TokenTree::Token(Token { kind: token::Colon | token::ModSep, .. }) => true,
_ => false,
.position(|tt| {
matches!(
tt,
tokenstream::TokenTree::Token(Token { kind: token::Colon | token::ModSep, .. })
)
})
.unwrap_or(tts.len());
let mut p = cx.new_parser_from_tts(tts.trees().skip(first_colon).collect());
Expand Down
5 changes: 1 addition & 4 deletions compiler/rustc_builtin_macros/src/proc_macro_harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,7 @@ impl<'a> Visitor<'a> for CollectProcMacros<'a> {
// we're just not interested in this item.
//
// If we find one, try to locate a `#[proc_macro_derive]` attribute on it.
let is_fn = match item.kind {
ast::ItemKind::Fn(..) => true,
_ => false,
};
let is_fn = matches!(item.kind, ast::ItemKind::Fn(..));

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

Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_codegen_ssa/src/mir/analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ impl<Bx: BuilderMethods<'a, 'tcx>> LocalAnalyzer<'mir, 'a, 'tcx, Bx> {
};

// Allow uses of projections that are ZSTs or from scalar fields.
let is_consume = match context {
let is_consume = matches!(
context,
PlaceContext::NonMutatingUse(
NonMutatingUseContext::Copy | NonMutatingUseContext::Move,
) => true,
_ => false,
};
)
);
if is_consume {
let base_ty =
mir::Place::ty_from(place_ref.local, proj_base, self.fx.mir, cx.tcx());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,7 @@ impl Visitor<'tcx> for TypeParamSpanVisitor<'tcx> {
[segment]
if segment
.res
.map(|res| match res {
Res::SelfTy(_, _) | Res::Def(hir::def::DefKind::TyParam, _) => true,
_ => false,
})
.map(|res| matches!(res, Res::SelfTy(_, _) | Res::Def(hir::def::DefKind::TyParam, _)))
.unwrap_or(false) =>
{
self.types.push(path.span);
Expand Down
5 changes: 1 addition & 4 deletions compiler/rustc_infer/src/infer/free_regions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,7 @@ impl<'tcx> FreeRegionMap<'tcx> {

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

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

0 comments on commit c51172f

Please sign in to comment.