From b2829a52161bc414f3b361c06b66633a234bba16 Mon Sep 17 00:00:00 2001 From: Leander Tentrup Date: Wed, 22 Apr 2020 10:08:46 +0200 Subject: Apply suggestions from code review Co-Authored-By: bjorn3 --- crates/ra_ide/src/syntax_highlighting.rs | 7 ++-- crates/ra_syntax/src/ast/tokens.rs | 59 ++++++++++++++++---------------- 2 files changed, 32 insertions(+), 34 deletions(-) (limited to 'crates') diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index e342ca9df..8ee3a78c6 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -194,7 +194,7 @@ pub(crate) fn highlight( let token = sema.descend_into_macros(token.clone()); let parent = token.parent(); - // Check if macro takes a format string and remeber it for highlighting later. + // Check if macro takes a format string and remember it for highlighting later. // The macros that accept a format string expand to a compiler builtin macros // `format_args` and `format_args_nl`. if let Some(fmt_macro_call) = parent.parent().and_then(ast::MacroCall::cast) { @@ -233,8 +233,7 @@ pub(crate) fn highlight( } } - let is_format_string = - format_string.as_ref().map(|fs| fs == &element_to_highlight).unwrap_or_default(); + let is_format_string = format_string.as_ref() == Some(&element_to_highlight); if let Some((highlight, binding_hash)) = highlight_element(&sema, &mut bindings_shadow_count, element_to_highlight.clone()) @@ -245,7 +244,7 @@ pub(crate) fn highlight( { stack.push(); if is_format_string { - string.lex_format_specifier(&mut |piece_range, kind| { + string.lex_format_specifier(|piece_range, kind| { let highlight = match kind { FormatSpecifier::Open | FormatSpecifier::Close diff --git a/crates/ra_syntax/src/ast/tokens.rs b/crates/ra_syntax/src/ast/tokens.rs index ec3b4e553..3e5c56b19 100644 --- a/crates/ra_syntax/src/ast/tokens.rs +++ b/crates/ra_syntax/src/ast/tokens.rs @@ -192,7 +192,7 @@ pub enum FormatSpecifier { } pub trait HasFormatSpecifier: AstToken { - fn lex_format_specifier(&self, callback: &mut F) + fn lex_format_specifier(&self, mut callback: F) where F: FnMut(TextRange, FormatSpecifier), { @@ -217,21 +217,15 @@ pub trait HasFormatSpecifier: AstToken { FormatSpecifier::Open, ); - let next_char = if let Some(c) = chars.clone().next() { - c - } else { - break; - }; - // check for integer/identifier - match next_char { + match chars.clone().next().unwrap_or_default() { '0'..='9' => { // integer - read_integer(&mut chars, initial_len, callback); + read_integer(&mut chars, initial_len, &mut callback); } 'a'..='z' | 'A'..='Z' | '_' => { // identifier - read_identifier(&mut chars, initial_len, callback); + read_identifier(&mut chars, initial_len, &mut callback); } _ => {} } @@ -241,7 +235,7 @@ pub trait HasFormatSpecifier: AstToken { &mut chars, initial_len, FormatSpecifier::Colon, - callback, + &mut callback, ); // check for fill/align @@ -255,13 +249,13 @@ pub trait HasFormatSpecifier: AstToken { &mut chars, initial_len, FormatSpecifier::Fill, - callback, + &mut callback, ); skip_char_and_emit( &mut chars, initial_len, FormatSpecifier::Align, - callback, + &mut callback, ); } _ => match first { @@ -270,7 +264,7 @@ pub trait HasFormatSpecifier: AstToken { &mut chars, initial_len, FormatSpecifier::Align, - callback, + &mut callback, ); } _ => {} @@ -284,7 +278,7 @@ pub trait HasFormatSpecifier: AstToken { &mut chars, initial_len, FormatSpecifier::Sign, - callback, + &mut callback, ); } _ => {} @@ -296,7 +290,7 @@ pub trait HasFormatSpecifier: AstToken { &mut chars, initial_len, FormatSpecifier::NumberSign, - callback, + &mut callback, ); } @@ -310,25 +304,25 @@ pub trait HasFormatSpecifier: AstToken { &mut chars, initial_len, FormatSpecifier::Zero, - callback, + &mut callback, ); } // width match chars.clone().next().unwrap_or_default() { '0'..='9' => { - read_integer(&mut chars, initial_len, callback); + read_integer(&mut chars, initial_len, &mut callback); if chars.clone().next() == Some('$') { skip_char_and_emit( &mut chars, initial_len, FormatSpecifier::DollarSign, - callback, + &mut callback, ); } } 'a'..='z' | 'A'..='Z' | '_' => { - read_identifier(&mut chars, initial_len, callback); + read_identifier(&mut chars, initial_len, &mut callback); if chars.clone().next() != Some('$') { continue; } @@ -336,7 +330,7 @@ pub trait HasFormatSpecifier: AstToken { &mut chars, initial_len, FormatSpecifier::DollarSign, - callback, + &mut callback, ); } _ => {} @@ -348,7 +342,7 @@ pub trait HasFormatSpecifier: AstToken { &mut chars, initial_len, FormatSpecifier::Dot, - callback, + &mut callback, ); match chars.clone().next().unwrap_or_default() { @@ -357,22 +351,22 @@ pub trait HasFormatSpecifier: AstToken { &mut chars, initial_len, FormatSpecifier::Asterisk, - callback, + &mut callback, ); } '0'..='9' => { - read_integer(&mut chars, initial_len, callback); + read_integer(&mut chars, initial_len, &mut callback); if chars.clone().next() == Some('$') { skip_char_and_emit( &mut chars, initial_len, FormatSpecifier::DollarSign, - callback, + &mut callback, ); } } 'a'..='z' | 'A'..='Z' | '_' => { - read_identifier(&mut chars, initial_len, callback); + read_identifier(&mut chars, initial_len, &mut callback); if chars.clone().next() != Some('$') { continue; } @@ -380,7 +374,7 @@ pub trait HasFormatSpecifier: AstToken { &mut chars, initial_len, FormatSpecifier::DollarSign, - callback, + &mut callback, ); } _ => { @@ -396,11 +390,11 @@ pub trait HasFormatSpecifier: AstToken { &mut chars, initial_len, FormatSpecifier::QuestionMark, - callback, + &mut callback, ); } 'a'..='z' | 'A'..='Z' | '_' => { - read_identifier(&mut chars, initial_len, callback); + read_identifier(&mut chars, initial_len, &mut callback); } _ => {} } @@ -416,7 +410,12 @@ pub trait HasFormatSpecifier: AstToken { // Escaped format end specifier, `}}` continue; } - skip_char_and_emit(&mut chars, initial_len, FormatSpecifier::Close, callback); + skip_char_and_emit( + &mut chars, + initial_len, + FormatSpecifier::Close, + &mut callback, + ); } _ => { while let Some(next_char) = chars.clone().next() { -- cgit v1.2.3