Skip to content

Commit 55fe4db

Browse files
matthiaskrgrcalebcartwright
authored andcommitted
fix clippy::match_like_matches_macro, use matches!() to compact code
1 parent 218593f commit 55fe4db

16 files changed

+97
-169
lines changed

Diff for: config_proc_macro/src/attrs.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@ pub fn is_config_value(attr: &syn::Attribute) -> bool {
3939
}
4040

4141
fn is_attr_name_value(attr: &syn::Attribute, name: &str) -> bool {
42-
attr.parse_meta().ok().map_or(false, |meta| match meta {
43-
syn::Meta::NameValue(syn::MetaNameValue { ref path, .. }) if path.is_ident(name) => true,
44-
_ => false,
45-
})
42+
attr.parse_meta().ok().map_or(false, |meta| matches!(meta, syn::Meta::NameValue(syn::MetaNameValue { ref path, .. }) if path.is_ident(name)))
4643
}
4744

4845
fn get_name_value_str_lit(attr: &syn::Attribute, name: &str) -> Option<String> {

Diff for: config_proc_macro/src/utils.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ where
1313
}
1414

1515
pub fn is_unit(v: &syn::Variant) -> bool {
16-
match v.fields {
17-
syn::Fields::Unit => true,
18-
_ => false,
19-
}
16+
matches!(v.fields, syn::Fields::Unit)
2017
}
2118

2219
/// Pretty-print the output of proc macro using rustfmt.

Diff for: src/emitter/rustfmt_diff.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,7 @@ impl From<Vec<Mismatch>> for ModifiedLines {
5656
let chunks = mismatches.into_iter().map(|mismatch| {
5757
let lines = mismatch.lines.iter();
5858
let num_removed = lines
59-
.filter(|line| match line {
60-
DiffLine::Resulting(_) => true,
61-
_ => false,
62-
})
59+
.filter(|line| matches!(line, DiffLine::Resulting(_)))
6360
.count();
6461

6562
let new_lines = mismatch.lines.into_iter().filter_map(|line| match line {

Diff for: src/formatting/chains.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,7 @@ impl ChainItem {
229229
}
230230

231231
fn is_comment(&self) -> bool {
232-
match self.kind {
233-
ChainItemKind::Comment(..) => true,
234-
_ => false,
235-
}
232+
matches!(self.kind, ChainItemKind::Comment(..))
236233
}
237234

238235
fn rewrite_method_call(

Diff for: src/formatting/closures.rs

+22-25
Original file line numberDiff line numberDiff line change
@@ -124,19 +124,19 @@ fn needs_block(block: &ast::Block, prefix: &str, context: &RewriteContext<'_>) -
124124
// TODO: The expressions "veto"ed here should align with expressions that are permitted on multiple
125125
// lines in rewrite_closure_expr#allow_multi_line. Consider refactoring to avoid this disparity.
126126
fn veto_block(e: &ast::Expr) -> bool {
127-
match e.kind {
127+
matches!(
128+
e.kind,
128129
ast::ExprKind::Call(..)
129-
| ast::ExprKind::Binary(..)
130-
| ast::ExprKind::Cast(..)
131-
| ast::ExprKind::Type(..)
132-
| ast::ExprKind::Assign(..)
133-
| ast::ExprKind::AssignOp(..)
134-
| ast::ExprKind::Field(..)
135-
| ast::ExprKind::Index(..)
136-
| ast::ExprKind::Range(..)
137-
| ast::ExprKind::Try(..) => true,
138-
_ => false,
139-
}
130+
| ast::ExprKind::Binary(..)
131+
| ast::ExprKind::Cast(..)
132+
| ast::ExprKind::Type(..)
133+
| ast::ExprKind::Assign(..)
134+
| ast::ExprKind::AssignOp(..)
135+
| ast::ExprKind::Field(..)
136+
| ast::ExprKind::Index(..)
137+
| ast::ExprKind::Range(..)
138+
| ast::ExprKind::Try(..)
139+
)
140140
}
141141

142142
// Rewrite closure with a single expression wrapping its body with block.
@@ -402,10 +402,7 @@ pub(crate) fn rewrite_last_closure(
402402
pub(crate) fn args_have_many_closure(args: &[OverflowableItem<'_>]) -> bool {
403403
args.iter()
404404
.filter_map(OverflowableItem::to_expr)
405-
.filter(|expr| match expr.kind {
406-
ast::ExprKind::Closure(..) => true,
407-
_ => false,
408-
})
405+
.filter(|expr| matches!(expr.kind, ast::ExprKind::Closure(..)))
409406
.count()
410407
> 1
411408
}
@@ -454,14 +451,14 @@ fn is_block_closure_forced_inner(expr: &ast::Expr) -> bool {
454451
/// isn't parsed as (if true {...} else {...} | x) | 5
455452
// From https://github.com/rust-lang/rust/blob/master/src/libsyntax/parse/classify.rs.
456453
fn expr_requires_semi_to_be_stmt(e: &ast::Expr) -> bool {
457-
match e.kind {
454+
!matches!(
455+
e.kind,
458456
ast::ExprKind::If(..)
459-
| ast::ExprKind::Match(..)
460-
| ast::ExprKind::Block(..)
461-
| ast::ExprKind::While(..)
462-
| ast::ExprKind::Loop(..)
463-
| ast::ExprKind::ForLoop(..)
464-
| ast::ExprKind::TryBlock(..) => false,
465-
_ => true,
466-
}
457+
| ast::ExprKind::Match(..)
458+
| ast::ExprKind::Block(..)
459+
| ast::ExprKind::While(..)
460+
| ast::ExprKind::Loop(..)
461+
| ast::ExprKind::ForLoop(..)
462+
| ast::ExprKind::TryBlock(..)
463+
)
467464
}

Diff for: src/formatting/comment.rs

+23-30
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,10 @@ fn custom_opener(s: &str) -> &str {
4848
impl<'a> CommentStyle<'a> {
4949
/// Returns `true` if the commenting style covers a line only.
5050
pub(crate) fn is_line_comment(&self) -> bool {
51-
match *self {
52-
CommentStyle::DoubleSlash
51+
matches!(*self, CommentStyle::DoubleSlash
5352
| CommentStyle::TripleSlash
5453
| CommentStyle::Doc
55-
| CommentStyle::Custom(_) => true,
56-
_ => false,
57-
}
54+
| CommentStyle::Custom(_))
5855
}
5956

6057
/// Returns `true` if the commenting style can span over multiple lines.
@@ -70,13 +67,13 @@ impl<'a> CommentStyle<'a> {
7067
/// Returns `true` if the commenting style is for documentation.
7168
/// https://doc.rust-lang.org/reference/comments.html
7269
pub(crate) fn is_doc_comment(&self) -> bool {
73-
match *self {
70+
matches!(
71+
*self,
7472
CommentStyle::TripleSlash
75-
| CommentStyle::Doc
76-
| CommentStyle::DoubleBullet
77-
| CommentStyle::Exclamation => true,
78-
_ => false,
79-
}
73+
| CommentStyle::Doc
74+
| CommentStyle::DoubleBullet
75+
| CommentStyle::Exclamation
76+
)
8077
}
8178

8279
pub(crate) fn opener(&self) -> &'a str {
@@ -888,11 +885,7 @@ fn is_table_item(mut s: &str) -> bool {
888885
// This function may return false positive, but should get its job done in most cases (i.e.
889886
// markdown tables with two column delimiters).
890887
s = s.trim_start();
891-
return s.starts_with('|')
892-
&& match s.rfind('|') {
893-
Some(0) | None => false,
894-
_ => true,
895-
};
888+
return s.starts_with('|') && !matches!(s.rfind('|'), Some(0) | None);
896889
}
897890

898891
/// Given the span, rewrite the missing comment inside it if available.
@@ -1166,26 +1159,26 @@ pub(crate) enum FullCodeCharKind {
11661159

11671160
impl FullCodeCharKind {
11681161
pub(crate) fn is_comment(self) -> bool {
1169-
match self {
1162+
matches!(
1163+
self,
11701164
FullCodeCharKind::StartComment
1171-
| FullCodeCharKind::InComment
1172-
| FullCodeCharKind::EndComment
1173-
| FullCodeCharKind::StartStringCommented
1174-
| FullCodeCharKind::InStringCommented
1175-
| FullCodeCharKind::EndStringCommented => true,
1176-
_ => false,
1177-
}
1165+
| FullCodeCharKind::InComment
1166+
| FullCodeCharKind::EndComment
1167+
| FullCodeCharKind::StartStringCommented
1168+
| FullCodeCharKind::InStringCommented
1169+
| FullCodeCharKind::EndStringCommented
1170+
)
11781171
}
11791172

11801173
/// Returns true if the character is inside a comment
11811174
pub(crate) fn inside_comment(self) -> bool {
1182-
match self {
1175+
matches!(
1176+
self,
11831177
FullCodeCharKind::InComment
1184-
| FullCodeCharKind::StartStringCommented
1185-
| FullCodeCharKind::InStringCommented
1186-
| FullCodeCharKind::EndStringCommented => true,
1187-
_ => false,
1188-
}
1178+
| FullCodeCharKind::StartStringCommented
1179+
| FullCodeCharKind::InStringCommented
1180+
| FullCodeCharKind::EndStringCommented
1181+
)
11891182
}
11901183

11911184
pub(crate) fn is_string(self) -> bool {

Diff for: src/formatting/expr.rs

+8-18
Original file line numberDiff line numberDiff line change
@@ -263,15 +263,12 @@ pub(crate) fn format_expr(
263263
}
264264

265265
fn needs_space_after_range(rhs: &ast::Expr) -> bool {
266-
match rhs.kind {
267-
// Don't format `.. ..` into `....`, which is invalid.
268-
//
269-
// This check is unnecessary for `lhs`, because a range
270-
// starting from another range needs parentheses as `(x ..) ..`
271-
// (`x .. ..` is a range from `x` to `..`).
272-
ast::ExprKind::Range(None, _, _) => true,
273-
_ => false,
274-
}
266+
// Don't format `.. ..` into `....`, which is invalid.
267+
//
268+
// This check is unnecessary for `lhs`, because a range
269+
// starting from another range needs parentheses as `(x ..) ..`
270+
// (`x .. ..` is a range from `x` to `..`).
271+
matches!(rhs.kind, ast::ExprKind::Range(None, _, _))
275272
}
276273

277274
let default_sp_delim = |lhs: Option<&ast::Expr>, rhs: Option<&ast::Expr>| {
@@ -1203,18 +1200,11 @@ pub(crate) fn is_empty_block(
12031200
}
12041201

12051202
pub(crate) fn stmt_is_expr(stmt: &ast::Stmt) -> bool {
1206-
match stmt.kind {
1207-
ast::StmtKind::Expr(..) => true,
1208-
_ => false,
1209-
}
1203+
matches!(stmt.kind, ast::StmtKind::Expr(..))
12101204
}
12111205

12121206
pub(crate) fn is_unsafe_block(block: &ast::Block) -> bool {
1213-
if let ast::BlockCheckMode::Unsafe(..) = block.rules {
1214-
true
1215-
} else {
1216-
false
1217-
}
1207+
matches!(block.rules, ast::BlockCheckMode::Unsafe(..))
12181208
}
12191209

12201210
pub(crate) fn rewrite_literal(

Diff for: src/formatting/imports.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -743,13 +743,9 @@ fn rewrite_nested_use_tree(
743743
}
744744
}
745745
let has_nested_list = use_tree_list.iter().any(|use_segment| {
746-
use_segment
747-
.path
748-
.last()
749-
.map_or(false, |last_segment| match last_segment {
750-
UseSegment::List(..) => true,
751-
_ => false,
752-
})
746+
use_segment.path.last().map_or(false, |last_segment| {
747+
matches!(last_segment, UseSegment::List(..))
748+
})
753749
});
754750

755751
let remaining_width = if has_nested_list {

Diff for: src/formatting/items.rs

+3-12
Original file line numberDiff line numberDiff line change
@@ -471,10 +471,7 @@ impl<'a> FmtVisitor<'a> {
471471
}
472472

473473
pub(crate) fn visit_struct(&mut self, struct_parts: &StructParts<'_>) {
474-
let is_tuple = match struct_parts.def {
475-
ast::VariantData::Tuple(..) => true,
476-
_ => false,
477-
};
474+
let is_tuple = matches!(struct_parts.def, ast::VariantData::Tuple(..));
478475
let rewrite = format_struct(&self.get_context(), struct_parts, self.block_indent, None)
479476
.map(|s| if is_tuple { s + ";" } else { s });
480477
self.push_rewrite(struct_parts.span, rewrite);
@@ -3318,15 +3315,9 @@ pub(crate) fn is_mod_decl(item: &ast::Item) -> bool {
33183315
}
33193316

33203317
pub(crate) fn is_use_item(item: &ast::Item) -> bool {
3321-
match item.kind {
3322-
ast::ItemKind::Use(_) => true,
3323-
_ => false,
3324-
}
3318+
matches!(item.kind, ast::ItemKind::Use(_))
33253319
}
33263320

33273321
pub(crate) fn is_extern_crate(item: &ast::Item) -> bool {
3328-
match item.kind {
3329-
ast::ItemKind::ExternCrate(..) => true,
3330-
_ => false,
3331-
}
3322+
matches!(item.kind, ast::ItemKind::ExternCrate(..))
33323323
}

Diff for: src/formatting/lists.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,7 @@ impl ListItem {
196196
// Returns `true` if the item causes something to be written.
197197
fn is_substantial(&self) -> bool {
198198
fn empty(s: &Option<String>) -> bool {
199-
match *s {
200-
Some(ref s) if !s.is_empty() => false,
201-
_ => true,
202-
}
199+
!matches!(*s, Some(ref s) if !s.is_empty())
203200
}
204201

205202
!(empty(&self.pre_comment) && empty(&self.item) && empty(&self.post_comment))

0 commit comments

Comments
 (0)