From 3ec781d4f2e2e3087a99b7ace21f97a84b7c7b84 Mon Sep 17 00:00:00 2001 From: Veetaha Date: Wed, 22 Jan 2020 02:11:38 +0200 Subject: ra_syntax: remove code duplication and token reevaluation from ast::Literal::kind() --- crates/ra_syntax/src/ast/expr_extensions.rs | 50 ++++++++++++----------------- 1 file changed, 20 insertions(+), 30 deletions(-) (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/ast/expr_extensions.rs b/crates/ra_syntax/src/ast/expr_extensions.rs index 6da4b1309..e609e02dc 100644 --- a/crates/ra_syntax/src/ast/expr_extensions.rs +++ b/crates/ra_syntax/src/ast/expr_extensions.rs @@ -305,44 +305,34 @@ impl ast::Literal { .unwrap() } - pub fn kind(&self) -> LiteralKind { - match self.token().kind() { - INT_NUMBER => { - let int_suffix_list = [ - "isize", "i128", "i64", "i32", "i16", "i8", "usize", "u128", "u64", "u32", - "u16", "u8", - ]; + fn find_suffix(text: &str, possible_suffixes: &[&str]) -> Option { + possible_suffixes + .iter() + .find(|&suffix| text.ends_with(suffix)) + .map(|&suffix| SmolStr::new(suffix)) + } - // The lexer treats e.g. `1f64` as an integer literal. See - // https://github.com/rust-analyzer/rust-analyzer/issues/1592 - // and the comments on the linked PR. - let float_suffix_list = ["f32", "f64"]; + pub fn kind(&self) -> LiteralKind { + const INT_SUFFIXES: [&'static str; 12] = [ + "u64", "u32", "u16", "u8", "usize", "isize", "i64", "i32", "i16", "i8", "u128", "i128", + ]; + const FLOAT_SUFFIXES: [&'static str; 2] = ["f32", "f64"]; - let text = self.token().text().to_string(); + let token = self.token(); - let float_suffix = float_suffix_list - .iter() - .find(|&s| text.ends_with(s)) - .map(|&suf| SmolStr::new(suf)); + match token.kind() { + INT_NUMBER => { + let text = token.text(); - if float_suffix.is_some() { - LiteralKind::FloatNumber { suffix: float_suffix } + if let suffix @ Some(_) = Self::find_suffix(&text, &FLOAT_SUFFIXES) { + LiteralKind::FloatNumber { suffix } } else { - let suffix = int_suffix_list - .iter() - .find(|&s| text.ends_with(s)) - .map(|&suf| SmolStr::new(suf)); - LiteralKind::IntNumber { suffix } + LiteralKind::IntNumber { suffix: Self::find_suffix(&text, &INT_SUFFIXES) } } } FLOAT_NUMBER => { - let allowed_suffix_list = ["f64", "f32"]; - let text = self.token().text().to_string(); - let suffix = allowed_suffix_list - .iter() - .find(|&s| text.ends_with(s)) - .map(|&suf| SmolStr::new(suf)); - LiteralKind::FloatNumber { suffix } + let text = token.text(); + LiteralKind::FloatNumber { suffix: Self::find_suffix(&text, &FLOAT_SUFFIXES) } } STRING | RAW_STRING => LiteralKind::String, T![true] | T![false] => LiteralKind::Bool, -- cgit v1.2.3