5105: Simlify with matches!() r=matklad a=Veetaha



Co-authored-by: Veetaha <veetaha2@gmail.com>
This commit is contained in:
bors[bot] 2020-06-28 22:37:25 +00:00 committed by GitHub
commit ca31b1d63a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 44 additions and 119 deletions

View file

@ -1,15 +1,12 @@
use ra_syntax::{
ast::{self, NameOwner, VisibilityOwner},
AstNode,
SyntaxKind::{
ATTR, COMMENT, CONST_DEF, ENUM_DEF, FN_DEF, MODULE, STRUCT_DEF, TRAIT_DEF, VISIBILITY,
WHITESPACE,
},
SyntaxNode, TextSize, T,
SyntaxKind::{CONST_DEF, ENUM_DEF, FN_DEF, MODULE, STRUCT_DEF, TRAIT_DEF, VISIBILITY},
T,
};
use test_utils::mark;
use crate::{AssistContext, AssistId, Assists};
use crate::{utils::vis_offset, AssistContext, AssistId, Assists};
// Assist: change_visibility
//
@ -30,9 +27,8 @@ pub(crate) fn change_visibility(acc: &mut Assists, ctx: &AssistContext) -> Optio
}
fn add_vis(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let item_keyword = ctx.token_at_offset().find(|leaf| match leaf.kind() {
T![const] | T![fn] | T![mod] | T![struct] | T![enum] | T![trait] => true,
_ => false,
let item_keyword = ctx.token_at_offset().find(|leaf| {
matches!(leaf.kind(), T![const] | T![fn] | T![mod] | T![struct] | T![enum] | T![trait])
});
let (offset, target) = if let Some(keyword) = item_keyword {
@ -71,17 +67,6 @@ fn add_vis(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
})
}
fn vis_offset(node: &SyntaxNode) -> TextSize {
node.children_with_tokens()
.skip_while(|it| match it.kind() {
WHITESPACE | COMMENT | ATTR => true,
_ => false,
})
.next()
.map(|it| it.text_range().start())
.unwrap_or_else(|| node.text_range().start())
}
fn change_vis(acc: &mut Assists, vis: ast::Visibility) -> Option<()> {
if vis.syntax().text() == "pub" {
let target = vis.syntax().text_range();

View file

@ -1,12 +1,8 @@
use hir::{db::HirDatabase, HasSource, HasVisibility, PathResolution};
use ra_db::FileId;
use ra_syntax::{
ast, AstNode,
SyntaxKind::{ATTR, COMMENT, WHITESPACE},
SyntaxNode, TextRange, TextSize,
};
use ra_syntax::{ast, AstNode, TextRange, TextSize};
use crate::{AssistContext, AssistId, Assists};
use crate::{utils::vis_offset, AssistContext, AssistId, Assists};
// FIXME: this really should be a fix for diagnostic, rather than an assist.
@ -177,17 +173,6 @@ fn target_data_for_def(
Some((offset, target, target_file, target_name))
}
fn vis_offset(node: &SyntaxNode) -> TextSize {
node.children_with_tokens()
.skip_while(|it| match it.kind() {
WHITESPACE | COMMENT | ATTR => true,
_ => false,
})
.next()
.map(|it| it.text_range().start())
.unwrap_or_else(|| node.text_range().start())
}
#[cfg(test)]
mod tests {
use crate::tests::{check_assist, check_assist_not_applicable};

View file

@ -81,10 +81,7 @@ pub(crate) fn merge_match_arms(acc: &mut Assists, ctx: &AssistContext) -> Option
}
fn contains_placeholder(a: &ast::MatchArm) -> bool {
match a.pat() {
Some(ra_syntax::ast::Pat::PlaceholderPat(..)) => true,
_ => false,
}
matches!(a.pat(), Some(ast::Pat::PlaceholderPat(..)))
}
#[cfg(test)]

View file

@ -7,7 +7,9 @@ use hir::{Adt, Crate, Enum, ScopeDef, Semantics, Trait, Type};
use ra_ide_db::RootDatabase;
use ra_syntax::{
ast::{self, make, NameOwner},
AstNode, SyntaxNode, T,
AstNode,
SyntaxKind::*,
SyntaxNode, TextSize, T,
};
use rustc_hash::FxHashSet;
@ -120,6 +122,13 @@ pub(crate) fn resolve_target_trait(
}
}
pub(crate) fn vis_offset(node: &SyntaxNode) -> TextSize {
node.children_with_tokens()
.find(|it| !matches!(it.kind(), WHITESPACE | COMMENT | ATTR))
.map(|it| it.text_range().start())
.unwrap_or_else(|| node.text_range().start())
}
pub(crate) fn invert_boolean_expression(expr: ast::Expr) -> ast::Expr {
if let Some(expr) = invert_special_case(&expr) {
return expr;

View file

@ -785,11 +785,7 @@ impl<'a> InferenceContext<'a> {
for &check_closures in &[false, true] {
let param_iter = param_tys.iter().cloned().chain(repeat(Ty::Unknown));
for (&arg, param_ty) in args.iter().zip(param_iter) {
let is_closure = match &self.body[arg] {
Expr::Lambda { .. } => true,
_ => false,
};
let is_closure = matches!(&self.body[arg], Expr::Lambda { .. });
if is_closure != check_closures {
continue;
}

View file

@ -620,17 +620,11 @@ pub enum GenericPredicate {
impl GenericPredicate {
pub fn is_error(&self) -> bool {
match self {
GenericPredicate::Error => true,
_ => false,
}
matches!(self, GenericPredicate::Error)
}
pub fn is_implemented(&self) -> bool {
match self {
GenericPredicate::Implemented(_) => true,
_ => false,
}
matches!(self, GenericPredicate::Implemented(_))
}
pub fn trait_ref(&self, db: &dyn HirDatabase) -> Option<TraitRef> {

View file

@ -313,10 +313,8 @@ fn get_string_representation(expr: &ast::Expr) -> Option<String> {
}
fn is_obvious_param(param_name: &str) -> bool {
let is_obvious_param_name = match param_name {
"predicate" | "value" | "pat" | "rhs" | "other" => true,
_ => false,
};
let is_obvious_param_name =
matches!(param_name, "predicate" | "value" | "pat" | "rhs" | "other");
param_name.len() == 1 || is_obvious_param_name
}

View file

@ -165,10 +165,7 @@ fn join_single_use_tree(edit: &mut TextEditBuilder, token: &SyntaxToken) -> Opti
}
fn is_trailing_comma(left: SyntaxKind, right: SyntaxKind) -> bool {
match (left, right) {
(T![,], T![')']) | (T![,], T![']']) => true,
_ => false,
}
matches!((left, right), (T![,], T![')']) | (T![,], T![']']))
}
#[cfg(test)]

View file

@ -346,10 +346,7 @@ impl Query {
}
fn is_type(kind: SyntaxKind) -> bool {
match kind {
STRUCT_DEF | ENUM_DEF | TRAIT_DEF | TYPE_ALIAS_DEF => true,
_ => false,
}
matches!(kind, STRUCT_DEF | ENUM_DEF | TRAIT_DEF | TYPE_ALIAS_DEF)
}
/// The actual data that is stored in the index. It should be as compact as

View file

@ -137,10 +137,7 @@ fn eat_fragment_kind<'a>(
}
fn is_boolean_literal(lit: &tt::Literal) -> bool {
match lit.text.as_str() {
"true" | "false" => true,
_ => false,
}
matches!(lit.text.as_str(), "true" | "false")
}
fn parse_repeat(src: &mut TtIter) -> Result<(Option<Separator>, RepeatKind), ExpandError> {

View file

@ -73,10 +73,7 @@ pub(crate) mod fragments {
// Parse a meta item , which excluded [], e.g : #[ MetaItem ]
pub(crate) fn meta_item(p: &mut Parser) {
fn is_delimiter(p: &mut Parser) -> bool {
match p.current() {
T!['{'] | T!['('] | T!['['] => true,
_ => false,
}
matches!(p.current(), T!['{'] | T!['('] | T!['['])
}
if is_delimiter(p) {

View file

@ -41,10 +41,7 @@ fn path(p: &mut Parser, mode: Mode) {
path_segment(p, mode, true);
let mut qual = path.complete(p, PATH);
loop {
let use_tree = match p.nth(2) {
T![*] | T!['{'] => true,
_ => false,
};
let use_tree = matches!(p.nth(2), T![*] | T!['{']);
if p.at(T![::]) && !use_tree {
let path = qual.precede(p);
p.bump(T![::]);

View file

@ -169,10 +169,7 @@ fn is_where_predicate(p: &mut Parser) -> bool {
}
fn is_where_clause_end(p: &mut Parser) -> bool {
match p.current() {
T!['{'] | T![;] | T![=] => true,
_ => false,
}
matches!(p.current(), T!['{'] | T![;] | T![=])
}
fn where_predicate(p: &mut Parser) {

View file

@ -20,9 +20,6 @@ impl From<SyntaxKind> for u16 {
impl SyntaxKind {
pub fn is_trivia(self) -> bool {
match self {
SyntaxKind::WHITESPACE | SyntaxKind::COMMENT => true,
_ => false,
}
matches!(self, SyntaxKind::WHITESPACE | SyntaxKind::COMMENT)
}
}

View file

@ -399,10 +399,7 @@ impl ast::BlockExpr {
Some(it) => it,
None => return true,
};
match parent.kind() {
FN_DEF | IF_EXPR | WHILE_EXPR | LOOP_EXPR | EFFECT_EXPR => false,
_ => true,
}
!matches!(parent.kind(), FN_DEF | IF_EXPR | WHILE_EXPR | LOOP_EXPR | EFFECT_EXPR)
}
}

View file

@ -459,16 +459,16 @@ impl ast::RangePat {
impl ast::TokenTree {
pub fn left_delimiter_token(&self) -> Option<SyntaxToken> {
self.syntax().first_child_or_token()?.into_token().filter(|it| match it.kind() {
T!['{'] | T!['('] | T!['['] => true,
_ => false,
})
self.syntax()
.first_child_or_token()?
.into_token()
.filter(|it| matches!(it.kind(), T!['{'] | T!['('] | T!['[']))
}
pub fn right_delimiter_token(&self) -> Option<SyntaxToken> {
self.syntax().last_child_or_token()?.into_token().filter(|it| match it.kind() {
T!['}'] | T![')'] | T![']'] => true,
_ => false,
})
self.syntax()
.last_child_or_token()?
.into_token()
.filter(|it| matches!(it.kind(), T!['}'] | T![')'] | T![']']))
}
}

View file

@ -120,10 +120,7 @@ fn get_text_after_edit(element: SyntaxElement, edit: &Indel) -> String {
}
fn is_contextual_kw(text: &str) -> bool {
match text {
"auto" | "default" | "union" => true,
_ => false,
}
matches!(text, "auto" | "default" | "union")
}
fn find_reparsable_node(node: &SyntaxNode, range: TextRange) -> Option<(SyntaxNode, Reparser)> {

View file

@ -105,10 +105,7 @@ impl<'a> Eq for Cursor<'a> {}
impl<'a> Cursor<'a> {
/// Check whether it is eof
pub fn eof(self) -> bool {
match self.buffer.entry(&self.ptr) {
None | Some(Entry::End(None)) => true,
_ => false,
}
matches!(self.buffer.entry(&self.ptr), None | Some(Entry::End(None)))
}
/// If the cursor is pointing at the end of a subtree, returns

View file

@ -28,16 +28,10 @@ pub enum Verbosity {
impl Verbosity {
pub fn is_verbose(self) -> bool {
match self {
Verbosity::Verbose | Verbosity::Spammy => true,
_ => false,
}
matches!(self, Verbosity::Verbose | Verbosity::Spammy)
}
pub fn is_spammy(self) -> bool {
match self {
Verbosity::Spammy => true,
_ => false,
}
matches!(self, Verbosity::Spammy)
}
}

View file

@ -78,10 +78,7 @@ pub fn analysis_bench(
}
}
BenchWhat::Complete(pos) | BenchWhat::GotoDef(pos) => {
let is_completion = match what {
BenchWhat::Complete(..) => true,
_ => false,
};
let is_completion = matches!(what, BenchWhat::Complete(..));
let offset = host
.analysis()