From 3c6dc0f89dc9c2902d71e8639b4913917a396f8a Mon Sep 17 00:00:00 2001 From: Clemens Wasser Date: Thu, 3 Jun 2021 11:45:10 +0200 Subject: Apply a few clippy suggestions --- crates/parser/src/grammar/expressions/atom.rs | 8 +- crates/parser/src/syntax_kind/generated.rs | 129 +++++++++++++++++++++----- crates/test_utils/src/lib.rs | 11 +-- 3 files changed, 112 insertions(+), 36 deletions(-) (limited to 'crates') diff --git a/crates/parser/src/grammar/expressions/atom.rs b/crates/parser/src/grammar/expressions/atom.rs index 269f223e6..abdfca1fe 100644 --- a/crates/parser/src/grammar/expressions/atom.rs +++ b/crates/parser/src/grammar/expressions/atom.rs @@ -252,12 +252,10 @@ fn closure_expr(p: &mut Parser) -> CompletedMarker { // test lambda_ret_block // fn main() { || -> i32 { 92 }(); } block_expr(p); + } else if p.at_ts(EXPR_FIRST) { + expr(p); } else { - if p.at_ts(EXPR_FIRST) { - expr(p); - } else { - p.error("expected expression"); - } + p.error("expected expression"); } m.complete(p, CLOSURE_EXPR) } diff --git a/crates/parser/src/syntax_kind/generated.rs b/crates/parser/src/syntax_kind/generated.rs index bcefd183a..be2dfbbea 100644 --- a/crates/parser/src/syntax_kind/generated.rs +++ b/crates/parser/src/syntax_kind/generated.rs @@ -260,33 +260,116 @@ pub enum SyntaxKind { use self::SyntaxKind::*; impl SyntaxKind { pub fn is_keyword(self) -> bool { - match self { - AS_KW | ASYNC_KW | AWAIT_KW | BOX_KW | BREAK_KW | CONST_KW | CONTINUE_KW | CRATE_KW - | DYN_KW | ELSE_KW | ENUM_KW | EXTERN_KW | FALSE_KW | FN_KW | FOR_KW | IF_KW - | IMPL_KW | IN_KW | LET_KW | LOOP_KW | MACRO_KW | MATCH_KW | MOD_KW | MOVE_KW - | MUT_KW | PUB_KW | REF_KW | RETURN_KW | SELF_KW | STATIC_KW | STRUCT_KW | SUPER_KW - | TRAIT_KW | TRUE_KW | TRY_KW | TYPE_KW | UNSAFE_KW | USE_KW | WHERE_KW | WHILE_KW - | YIELD_KW | AUTO_KW | DEFAULT_KW | EXISTENTIAL_KW | UNION_KW | RAW_KW - | MACRO_RULES_KW => true, - _ => false, - } + matches!( + self, + AS_KW + | ASYNC_KW + | AWAIT_KW + | BOX_KW + | BREAK_KW + | CONST_KW + | CONTINUE_KW + | CRATE_KW + | DYN_KW + | ELSE_KW + | ENUM_KW + | EXTERN_KW + | FALSE_KW + | FN_KW + | FOR_KW + | IF_KW + | IMPL_KW + | IN_KW + | LET_KW + | LOOP_KW + | MACRO_KW + | MATCH_KW + | MOD_KW + | MOVE_KW + | MUT_KW + | PUB_KW + | REF_KW + | RETURN_KW + | SELF_KW + | STATIC_KW + | STRUCT_KW + | SUPER_KW + | TRAIT_KW + | TRUE_KW + | TRY_KW + | TYPE_KW + | UNSAFE_KW + | USE_KW + | WHERE_KW + | WHILE_KW + | YIELD_KW + | AUTO_KW + | DEFAULT_KW + | EXISTENTIAL_KW + | UNION_KW + | RAW_KW + | MACRO_RULES_KW + ) } pub fn is_punct(self) -> bool { - match self { - SEMICOLON | COMMA | L_PAREN | R_PAREN | L_CURLY | R_CURLY | L_BRACK | R_BRACK - | L_ANGLE | R_ANGLE | AT | POUND | TILDE | QUESTION | DOLLAR | AMP | PIPE | PLUS - | STAR | SLASH | CARET | PERCENT | UNDERSCORE | DOT | DOT2 | DOT3 | DOT2EQ | COLON - | COLON2 | EQ | EQ2 | FAT_ARROW | BANG | NEQ | MINUS | THIN_ARROW | LTEQ | GTEQ - | PLUSEQ | MINUSEQ | PIPEEQ | AMPEQ | CARETEQ | SLASHEQ | STAREQ | PERCENTEQ | AMP2 - | PIPE2 | SHL | SHR | SHLEQ | SHREQ => true, - _ => false, - } + matches!( + self, + SEMICOLON + | COMMA + | L_PAREN + | R_PAREN + | L_CURLY + | R_CURLY + | L_BRACK + | R_BRACK + | L_ANGLE + | R_ANGLE + | AT + | POUND + | TILDE + | QUESTION + | DOLLAR + | AMP + | PIPE + | PLUS + | STAR + | SLASH + | CARET + | PERCENT + | UNDERSCORE + | DOT + | DOT2 + | DOT3 + | DOT2EQ + | COLON + | COLON2 + | EQ + | EQ2 + | FAT_ARROW + | BANG + | NEQ + | MINUS + | THIN_ARROW + | LTEQ + | GTEQ + | PLUSEQ + | MINUSEQ + | PIPEEQ + | AMPEQ + | CARETEQ + | SLASHEQ + | STAREQ + | PERCENTEQ + | AMP2 + | PIPE2 + | SHL + | SHR + | SHLEQ + | SHREQ + ) } pub fn is_literal(self) -> bool { - match self { - INT_NUMBER | FLOAT_NUMBER | CHAR | BYTE | STRING | BYTE_STRING => true, - _ => false, - } + matches!(self, INT_NUMBER | FLOAT_NUMBER | CHAR | BYTE | STRING | BYTE_STRING) } pub fn from_keyword(ident: &str) -> Option { let kw = match ident { diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index bd017567c..ac5a9509d 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -238,14 +238,9 @@ fn extract_line_annotations(mut line: &str) -> Vec { let mut res = Vec::new(); let mut offset: TextSize = 0.into(); let marker: fn(char) -> bool = if line.contains('^') { |c| c == '^' } else { |c| c == '|' }; - loop { - match line.find(marker) { - Some(idx) => { - offset += TextSize::try_from(idx).unwrap(); - line = &line[idx..]; - } - None => break, - }; + while let Some(idx) = line.find(marker) { + offset += TextSize::try_from(idx).unwrap(); + line = &line[idx..]; let mut len = line.chars().take_while(|&it| it == '^').count(); let mut continuation = false; -- cgit v1.2.3 From 629e8d1ed0aeabf27a1e1b1d3c8c9134716043e3 Mon Sep 17 00:00:00 2001 From: Clemens Wasser Date: Thu, 3 Jun 2021 12:46:56 +0200 Subject: Apply more clippy suggestions and update generated --- crates/mbe/src/benchmark.rs | 2 +- crates/mbe/src/expander/matcher.rs | 21 ++--- crates/mbe/src/lib.rs | 10 +-- crates/mbe/src/parser.rs | 2 +- crates/mbe/src/syntax_bridge.rs | 5 +- crates/mbe/src/tt_iter.rs | 2 +- crates/parser/src/syntax_kind/generated.rs | 129 +++++------------------------ crates/syntax/src/parsing/reparsing.rs | 8 +- crates/syntax/src/tests.rs | 2 +- 9 files changed, 45 insertions(+), 136 deletions(-) (limited to 'crates') diff --git a/crates/mbe/src/benchmark.rs b/crates/mbe/src/benchmark.rs index 38707ffa5..18eb97f0d 100644 --- a/crates/mbe/src/benchmark.rs +++ b/crates/mbe/src/benchmark.rs @@ -187,7 +187,7 @@ fn invocation_fixtures(rules: &FxHashMap) -> Vec<(String, tt let a = 1664525; let c = 1013904223; *seed = usize::wrapping_add(usize::wrapping_mul(*seed, a), c); - return *seed; + *seed } fn make_ident(ident: &str) -> tt::TokenTree { tt::Leaf::Ident(tt::Ident { id: tt::TokenId::unspecified(), text: SmolStr::new(ident) }) diff --git a/crates/mbe/src/expander/matcher.rs b/crates/mbe/src/expander/matcher.rs index 84ca3ff87..c982eb58f 100644 --- a/crates/mbe/src/expander/matcher.rs +++ b/crates/mbe/src/expander/matcher.rs @@ -219,7 +219,7 @@ impl BindingsBuilder { bindings } - fn build_inner(&self, bindings: &mut Bindings, link_nodes: &Vec>>) { + fn build_inner(&self, bindings: &mut Bindings, link_nodes: &[LinkNode>]) { let mut nodes = Vec::new(); self.collect_nodes(&link_nodes, &mut nodes); @@ -301,7 +301,7 @@ impl BindingsBuilder { fn collect_nodes<'a>( &'a self, - link_nodes: &'a Vec>>, + link_nodes: &'a [LinkNode>], nodes: &mut Vec<&'a Rc>, ) { link_nodes.iter().for_each(|it| match it { @@ -494,15 +494,8 @@ fn match_loop_inner<'t>( } Some(err) => { res.add_err(err); - match match_res.value { - Some(fragment) => { - bindings_builder.push_fragment( - &mut item.bindings, - &name, - fragment, - ); - } - _ => {} + if let Some(fragment) = match_res.value { + bindings_builder.push_fragment(&mut item.bindings, &name, fragment); } item.is_error = true; error_items.push(item); @@ -578,9 +571,9 @@ fn match_loop(pattern: &MetaTemplate, src: &tt::Subtree) -> Match { ); stdx::always!(cur_items.is_empty()); - if error_items.len() > 0 { + if !error_items.is_empty() { error_recover_item = error_items.pop().map(|it| it.bindings); - } else if eof_items.len() > 0 { + } else if !eof_items.is_empty() { error_recover_item = Some(eof_items[0].bindings.clone()); } @@ -793,7 +786,7 @@ impl<'a> TtIter<'a> { _ => (), } - let tt = self.next().ok_or_else(|| ())?.clone(); + let tt = self.next().ok_or(())?.clone(); let punct = match tt { tt::TokenTree::Leaf(tt::Leaf::Punct(punct)) if punct.spacing == tt::Spacing::Joint => { punct diff --git a/crates/mbe/src/lib.rs b/crates/mbe/src/lib.rs index b95374b76..380a50744 100644 --- a/crates/mbe/src/lib.rs +++ b/crates/mbe/src/lib.rs @@ -295,8 +295,8 @@ fn validate(pattern: &MetaTemplate) -> Result<(), ParseError> { // Checks that no repetition which could match an empty token // https://github.com/rust-lang/rust/blob/a58b1ed44f5e06976de2bdc4d7dc81c36a96934f/src/librustc_expand/mbe/macro_rules.rs#L558 - if separator.is_none() { - if subtree.iter().all(|child_op| { + if separator.is_none() + && subtree.iter().all(|child_op| { match child_op { Op::Var { kind, .. } => { // vis is optional @@ -314,9 +314,9 @@ fn validate(pattern: &MetaTemplate) -> Result<(), ParseError> { Op::Subtree { .. } => {} } false - }) { - return Err(ParseError::RepetitionEmptyTokenTree); - } + }) + { + return Err(ParseError::RepetitionEmptyTokenTree); } validate(subtree)? } diff --git a/crates/mbe/src/parser.rs b/crates/mbe/src/parser.rs index 61b2a4955..04c0d3e75 100644 --- a/crates/mbe/src/parser.rs +++ b/crates/mbe/src/parser.rs @@ -213,7 +213,7 @@ fn next_op<'a>(first: &tt::TokenTree, src: &mut TtIter<'a>, mode: Mode) -> Resul Ok(res) } -fn eat_fragment_kind<'a>(src: &mut TtIter<'a>, mode: Mode) -> Result, ParseError> { +fn eat_fragment_kind(src: &mut TtIter<'_>, mode: Mode) -> Result, ParseError> { if let Mode::Pattern = mode { src.expect_char(':').map_err(|()| err!("bad fragment specifier 1"))?; let ident = src.expect_ident().map_err(|()| err!("bad fragment specifier 1"))?; diff --git a/crates/mbe/src/syntax_bridge.rs b/crates/mbe/src/syntax_bridge.rs index b11172caf..978c75747 100644 --- a/crates/mbe/src/syntax_bridge.rs +++ b/crates/mbe/src/syntax_bridge.rs @@ -243,8 +243,7 @@ trait TokenConvertor { type Token: SrcToken; fn go(&mut self) -> tt::Subtree { - let mut subtree = tt::Subtree::default(); - subtree.delimiter = None; + let mut subtree = tt::Subtree { delimiter: None, ..Default::default() }; while self.peek().is_some() { self.collect_leaf(&mut subtree.token_trees); } @@ -506,7 +505,7 @@ impl TokenConvertor for Convertor { fn peek(&self) -> Option { if let Some((punct, mut offset)) = self.punct_offset.clone() { - offset = offset + TextSize::of('.'); + offset += TextSize::of('.'); if usize::from(offset) < punct.text().len() { return Some(SynToken::Punch(punct, offset)); } diff --git a/crates/mbe/src/tt_iter.rs b/crates/mbe/src/tt_iter.rs index 99a8d250b..bd54f2442 100644 --- a/crates/mbe/src/tt_iter.rs +++ b/crates/mbe/src/tt_iter.rs @@ -138,7 +138,7 @@ impl<'a> TtIter<'a> { } } self.inner = self.inner.as_slice()[res.len()..].iter(); - if res.len() == 0 && err.is_none() { + if res.is_empty() && err.is_none() { err = Some(err!("no tokens consumed")); } let res = match res.len() { diff --git a/crates/parser/src/syntax_kind/generated.rs b/crates/parser/src/syntax_kind/generated.rs index be2dfbbea..bcefd183a 100644 --- a/crates/parser/src/syntax_kind/generated.rs +++ b/crates/parser/src/syntax_kind/generated.rs @@ -260,116 +260,33 @@ pub enum SyntaxKind { use self::SyntaxKind::*; impl SyntaxKind { pub fn is_keyword(self) -> bool { - matches!( - self, - AS_KW - | ASYNC_KW - | AWAIT_KW - | BOX_KW - | BREAK_KW - | CONST_KW - | CONTINUE_KW - | CRATE_KW - | DYN_KW - | ELSE_KW - | ENUM_KW - | EXTERN_KW - | FALSE_KW - | FN_KW - | FOR_KW - | IF_KW - | IMPL_KW - | IN_KW - | LET_KW - | LOOP_KW - | MACRO_KW - | MATCH_KW - | MOD_KW - | MOVE_KW - | MUT_KW - | PUB_KW - | REF_KW - | RETURN_KW - | SELF_KW - | STATIC_KW - | STRUCT_KW - | SUPER_KW - | TRAIT_KW - | TRUE_KW - | TRY_KW - | TYPE_KW - | UNSAFE_KW - | USE_KW - | WHERE_KW - | WHILE_KW - | YIELD_KW - | AUTO_KW - | DEFAULT_KW - | EXISTENTIAL_KW - | UNION_KW - | RAW_KW - | MACRO_RULES_KW - ) + match self { + AS_KW | ASYNC_KW | AWAIT_KW | BOX_KW | BREAK_KW | CONST_KW | CONTINUE_KW | CRATE_KW + | DYN_KW | ELSE_KW | ENUM_KW | EXTERN_KW | FALSE_KW | FN_KW | FOR_KW | IF_KW + | IMPL_KW | IN_KW | LET_KW | LOOP_KW | MACRO_KW | MATCH_KW | MOD_KW | MOVE_KW + | MUT_KW | PUB_KW | REF_KW | RETURN_KW | SELF_KW | STATIC_KW | STRUCT_KW | SUPER_KW + | TRAIT_KW | TRUE_KW | TRY_KW | TYPE_KW | UNSAFE_KW | USE_KW | WHERE_KW | WHILE_KW + | YIELD_KW | AUTO_KW | DEFAULT_KW | EXISTENTIAL_KW | UNION_KW | RAW_KW + | MACRO_RULES_KW => true, + _ => false, + } } pub fn is_punct(self) -> bool { - matches!( - self, - SEMICOLON - | COMMA - | L_PAREN - | R_PAREN - | L_CURLY - | R_CURLY - | L_BRACK - | R_BRACK - | L_ANGLE - | R_ANGLE - | AT - | POUND - | TILDE - | QUESTION - | DOLLAR - | AMP - | PIPE - | PLUS - | STAR - | SLASH - | CARET - | PERCENT - | UNDERSCORE - | DOT - | DOT2 - | DOT3 - | DOT2EQ - | COLON - | COLON2 - | EQ - | EQ2 - | FAT_ARROW - | BANG - | NEQ - | MINUS - | THIN_ARROW - | LTEQ - | GTEQ - | PLUSEQ - | MINUSEQ - | PIPEEQ - | AMPEQ - | CARETEQ - | SLASHEQ - | STAREQ - | PERCENTEQ - | AMP2 - | PIPE2 - | SHL - | SHR - | SHLEQ - | SHREQ - ) + match self { + SEMICOLON | COMMA | L_PAREN | R_PAREN | L_CURLY | R_CURLY | L_BRACK | R_BRACK + | L_ANGLE | R_ANGLE | AT | POUND | TILDE | QUESTION | DOLLAR | AMP | PIPE | PLUS + | STAR | SLASH | CARET | PERCENT | UNDERSCORE | DOT | DOT2 | DOT3 | DOT2EQ | COLON + | COLON2 | EQ | EQ2 | FAT_ARROW | BANG | NEQ | MINUS | THIN_ARROW | LTEQ | GTEQ + | PLUSEQ | MINUSEQ | PIPEEQ | AMPEQ | CARETEQ | SLASHEQ | STAREQ | PERCENTEQ | AMP2 + | PIPE2 | SHL | SHR | SHLEQ | SHREQ => true, + _ => false, + } } pub fn is_literal(self) -> bool { - matches!(self, INT_NUMBER | FLOAT_NUMBER | CHAR | BYTE | STRING | BYTE_STRING) + match self { + INT_NUMBER | FLOAT_NUMBER | CHAR | BYTE | STRING | BYTE_STRING => true, + _ => false, + } } pub fn from_keyword(ident: &str) -> Option { let kw = match ident { diff --git a/crates/syntax/src/parsing/reparsing.rs b/crates/syntax/src/parsing/reparsing.rs index 4ad50ab72..304f47b3d 100644 --- a/crates/syntax/src/parsing/reparsing.rs +++ b/crates/syntax/src/parsing/reparsing.rs @@ -36,8 +36,8 @@ pub(crate) fn incremental_reparse( None } -fn reparse_token<'node>( - root: &'node SyntaxNode, +fn reparse_token( + root: &SyntaxNode, edit: &Indel, ) -> Option<(GreenNode, Vec, TextRange)> { let prev_token = root.covering_element(edit.delete).as_token()?.clone(); @@ -84,8 +84,8 @@ fn reparse_token<'node>( } } -fn reparse_block<'node>( - root: &'node SyntaxNode, +fn reparse_block( + root: &SyntaxNode, edit: &Indel, ) -> Option<(GreenNode, Vec, TextRange)> { let (node, reparser) = find_reparsable_node(root, edit.delete)?; diff --git a/crates/syntax/src/tests.rs b/crates/syntax/src/tests.rs index 45f3c800f..9f2426171 100644 --- a/crates/syntax/src/tests.rs +++ b/crates/syntax/src/tests.rs @@ -236,7 +236,7 @@ where } }); dir_tests(&test_data_dir(), err_paths, "rast", |text, path| { - if let Ok(_) = f(text) { + if f(text).is_ok() { panic!("'{:?}' successfully parsed when it should have errored", path); } else { "ERROR\n".to_owned() -- cgit v1.2.3