Skip to content

Commit ca31b1d

Browse files
bors[bot]Veetaha
andauthored
Merge #5105
5105: Simlify with matches!() r=matklad a=Veetaha Co-authored-by: Veetaha <veetaha2@gmail.com>
2 parents 11f31ae + 36128c1 commit ca31b1d

File tree

20 files changed

+44
-119
lines changed

20 files changed

+44
-119
lines changed

crates/ra_assists/src/handlers/change_visibility.rs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
use ra_syntax::{
22
ast::{self, NameOwner, VisibilityOwner},
33
AstNode,
4-
SyntaxKind::{
5-
ATTR, COMMENT, CONST_DEF, ENUM_DEF, FN_DEF, MODULE, STRUCT_DEF, TRAIT_DEF, VISIBILITY,
6-
WHITESPACE,
7-
},
8-
SyntaxNode, TextSize, T,
4+
SyntaxKind::{CONST_DEF, ENUM_DEF, FN_DEF, MODULE, STRUCT_DEF, TRAIT_DEF, VISIBILITY},
5+
T,
96
};
107
use test_utils::mark;
118

12-
use crate::{AssistContext, AssistId, Assists};
9+
use crate::{utils::vis_offset, AssistContext, AssistId, Assists};
1310

1411
// Assist: change_visibility
1512
//
@@ -30,9 +27,8 @@ pub(crate) fn change_visibility(acc: &mut Assists, ctx: &AssistContext) -> Optio
3027
}
3128

3229
fn add_vis(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
33-
let item_keyword = ctx.token_at_offset().find(|leaf| match leaf.kind() {
34-
T![const] | T![fn] | T![mod] | T![struct] | T![enum] | T![trait] => true,
35-
_ => false,
30+
let item_keyword = ctx.token_at_offset().find(|leaf| {
31+
matches!(leaf.kind(), T![const] | T![fn] | T![mod] | T![struct] | T![enum] | T![trait])
3632
});
3733

3834
let (offset, target) = if let Some(keyword) = item_keyword {
@@ -71,17 +67,6 @@ fn add_vis(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
7167
})
7268
}
7369

74-
fn vis_offset(node: &SyntaxNode) -> TextSize {
75-
node.children_with_tokens()
76-
.skip_while(|it| match it.kind() {
77-
WHITESPACE | COMMENT | ATTR => true,
78-
_ => false,
79-
})
80-
.next()
81-
.map(|it| it.text_range().start())
82-
.unwrap_or_else(|| node.text_range().start())
83-
}
84-
8570
fn change_vis(acc: &mut Assists, vis: ast::Visibility) -> Option<()> {
8671
if vis.syntax().text() == "pub" {
8772
let target = vis.syntax().text_range();

crates/ra_assists/src/handlers/fix_visibility.rs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
use hir::{db::HirDatabase, HasSource, HasVisibility, PathResolution};
22
use ra_db::FileId;
3-
use ra_syntax::{
4-
ast, AstNode,
5-
SyntaxKind::{ATTR, COMMENT, WHITESPACE},
6-
SyntaxNode, TextRange, TextSize,
7-
};
3+
use ra_syntax::{ast, AstNode, TextRange, TextSize};
84

9-
use crate::{AssistContext, AssistId, Assists};
5+
use crate::{utils::vis_offset, AssistContext, AssistId, Assists};
106

117
// FIXME: this really should be a fix for diagnostic, rather than an assist.
128

@@ -177,17 +173,6 @@ fn target_data_for_def(
177173
Some((offset, target, target_file, target_name))
178174
}
179175

180-
fn vis_offset(node: &SyntaxNode) -> TextSize {
181-
node.children_with_tokens()
182-
.skip_while(|it| match it.kind() {
183-
WHITESPACE | COMMENT | ATTR => true,
184-
_ => false,
185-
})
186-
.next()
187-
.map(|it| it.text_range().start())
188-
.unwrap_or_else(|| node.text_range().start())
189-
}
190-
191176
#[cfg(test)]
192177
mod tests {
193178
use crate::tests::{check_assist, check_assist_not_applicable};

crates/ra_assists/src/handlers/merge_match_arms.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,7 @@ pub(crate) fn merge_match_arms(acc: &mut Assists, ctx: &AssistContext) -> Option
8181
}
8282

8383
fn contains_placeholder(a: &ast::MatchArm) -> bool {
84-
match a.pat() {
85-
Some(ra_syntax::ast::Pat::PlaceholderPat(..)) => true,
86-
_ => false,
87-
}
84+
matches!(a.pat(), Some(ast::Pat::PlaceholderPat(..)))
8885
}
8986

9087
#[cfg(test)]

crates/ra_assists/src/utils.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use hir::{Adt, Crate, Enum, ScopeDef, Semantics, Trait, Type};
77
use ra_ide_db::RootDatabase;
88
use ra_syntax::{
99
ast::{self, make, NameOwner},
10-
AstNode, SyntaxNode, T,
10+
AstNode,
11+
SyntaxKind::*,
12+
SyntaxNode, TextSize, T,
1113
};
1214
use rustc_hash::FxHashSet;
1315

@@ -120,6 +122,13 @@ pub(crate) fn resolve_target_trait(
120122
}
121123
}
122124

125+
pub(crate) fn vis_offset(node: &SyntaxNode) -> TextSize {
126+
node.children_with_tokens()
127+
.find(|it| !matches!(it.kind(), WHITESPACE | COMMENT | ATTR))
128+
.map(|it| it.text_range().start())
129+
.unwrap_or_else(|| node.text_range().start())
130+
}
131+
123132
pub(crate) fn invert_boolean_expression(expr: ast::Expr) -> ast::Expr {
124133
if let Some(expr) = invert_special_case(&expr) {
125134
return expr;

crates/ra_hir_ty/src/infer/expr.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -785,11 +785,7 @@ impl<'a> InferenceContext<'a> {
785785
for &check_closures in &[false, true] {
786786
let param_iter = param_tys.iter().cloned().chain(repeat(Ty::Unknown));
787787
for (&arg, param_ty) in args.iter().zip(param_iter) {
788-
let is_closure = match &self.body[arg] {
789-
Expr::Lambda { .. } => true,
790-
_ => false,
791-
};
792-
788+
let is_closure = matches!(&self.body[arg], Expr::Lambda { .. });
793789
if is_closure != check_closures {
794790
continue;
795791
}

crates/ra_hir_ty/src/lib.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -620,17 +620,11 @@ pub enum GenericPredicate {
620620

621621
impl GenericPredicate {
622622
pub fn is_error(&self) -> bool {
623-
match self {
624-
GenericPredicate::Error => true,
625-
_ => false,
626-
}
623+
matches!(self, GenericPredicate::Error)
627624
}
628625

629626
pub fn is_implemented(&self) -> bool {
630-
match self {
631-
GenericPredicate::Implemented(_) => true,
632-
_ => false,
633-
}
627+
matches!(self, GenericPredicate::Implemented(_))
634628
}
635629

636630
pub fn trait_ref(&self, db: &dyn HirDatabase) -> Option<TraitRef> {

crates/ra_ide/src/inlay_hints.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,10 +313,8 @@ fn get_string_representation(expr: &ast::Expr) -> Option<String> {
313313
}
314314

315315
fn is_obvious_param(param_name: &str) -> bool {
316-
let is_obvious_param_name = match param_name {
317-
"predicate" | "value" | "pat" | "rhs" | "other" => true,
318-
_ => false,
319-
};
316+
let is_obvious_param_name =
317+
matches!(param_name, "predicate" | "value" | "pat" | "rhs" | "other");
320318
param_name.len() == 1 || is_obvious_param_name
321319
}
322320

crates/ra_ide/src/join_lines.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,7 @@ fn join_single_use_tree(edit: &mut TextEditBuilder, token: &SyntaxToken) -> Opti
165165
}
166166

167167
fn is_trailing_comma(left: SyntaxKind, right: SyntaxKind) -> bool {
168-
match (left, right) {
169-
(T![,], T![')']) | (T![,], T![']']) => true,
170-
_ => false,
171-
}
168+
matches!((left, right), (T![,], T![')']) | (T![,], T![']']))
172169
}
173170

174171
#[cfg(test)]

crates/ra_ide_db/src/symbol_index.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,10 +346,7 @@ impl Query {
346346
}
347347

348348
fn is_type(kind: SyntaxKind) -> bool {
349-
match kind {
350-
STRUCT_DEF | ENUM_DEF | TRAIT_DEF | TYPE_ALIAS_DEF => true,
351-
_ => false,
352-
}
349+
matches!(kind, STRUCT_DEF | ENUM_DEF | TRAIT_DEF | TYPE_ALIAS_DEF)
353350
}
354351

355352
/// The actual data that is stored in the index. It should be as compact as

crates/ra_mbe/src/parser.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,7 @@ fn eat_fragment_kind<'a>(
137137
}
138138

139139
fn is_boolean_literal(lit: &tt::Literal) -> bool {
140-
match lit.text.as_str() {
141-
"true" | "false" => true,
142-
_ => false,
143-
}
140+
matches!(lit.text.as_str(), "true" | "false")
144141
}
145142

146143
fn parse_repeat(src: &mut TtIter) -> Result<(Option<Separator>, RepeatKind), ExpandError> {

0 commit comments

Comments
 (0)