From a64081ac7348d73bb83a5a4dca636fdbf6842350 Mon Sep 17 00:00:00 2001 From: Joel Natividad <1980690+jqnatividad@users.noreply.github.com> Date: Sun, 11 Sep 2022 17:37:01 -0400 Subject: [PATCH] Apply select clippy recommendations (#27) * Bump dependencies to minor version; 2021 edition * apply clippy recommendations * go back to match statement * consolidate clippy allows * pare down lints - remove nursery level lints - sort lints * pared down lint allows --- src/lexer.rs | 21 ++++++++------------- src/lib.rs | 12 ++++++++++-- src/main.rs | 9 +++++---- src/parser.rs | 4 ++-- src/units.rs | 14 +++++++------- 5 files changed, 32 insertions(+), 28 deletions(-) diff --git a/src/lexer.rs b/src/lexer.rs index 7626258..e6d4d61 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -14,7 +14,7 @@ use crate::units::Unit::*; use unicode_segmentation::{Graphemes, UnicodeSegmentation}; fn is_word_char_str(input: &str) -> bool { - let x = match input { + match input { "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z" => true, @@ -23,16 +23,11 @@ fn is_word_char_str(input: &str) -> bool { | "y" | "z" => true, "Ω" | "Ω" | "µ" | "μ" => true, _ => false, - }; - return x; + } } fn is_numeric_str(input: &str) -> bool { - match input { - "." => true, - "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" => true, - _ => false, - } + matches!(input, "." | "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9") } /// Read next characters as a word, otherwise return empty string. @@ -40,7 +35,7 @@ fn is_numeric_str(input: &str) -> bool { fn read_word_plain(chars: &mut Peekable) -> String { let mut word = String::new(); while let Some(next_char) = chars.peek() { - if is_word_char_str(&next_char) { + if is_word_char_str(next_char) { word += chars.next().unwrap(); } else { break; @@ -65,7 +60,7 @@ fn read_word(first_c: &str, lexer: &mut Lexer) -> String { } } while let Some(next_char) = chars.peek() { - if is_word_char_str(&next_char) { + if is_word_char_str(next_char) { word += chars.next().unwrap(); } else { break; @@ -91,7 +86,7 @@ fn parse_token(c: &str, lexer: &mut Lexer) -> Result<(), String> { let tokens = &mut lexer.tokens; match c { value if value.trim().is_empty() => {}, - value if is_word_char_str(&value) => { + value if is_word_char_str(value) => { parse_word(read_word(c, lexer).as_str(), lexer)?; }, value if is_numeric_str(value) => { @@ -361,7 +356,7 @@ fn parse_word(word: &str, lexer: &mut Lexer) -> Result<(), String> { other => { lexer.tokens.push(Token::Unit(Pound)); lexer.tokens.push(Token::Operator(Minus)); - parse_word_if_non_empty(&other, lexer)?; + parse_word_if_non_empty(other, lexer)?; return Ok(()); } } @@ -619,7 +614,7 @@ struct Lexer<'a> { /// Lex an input string and returns [`Token`]s pub fn lex(input: &str, remove_trailing_operator: bool, default_degree: Unit) -> Result, String> { - let mut input = input.replace(",", "").to_ascii_lowercase(); + let mut input = input.replace(',', "").to_ascii_lowercase(); if remove_trailing_operator { match &input.chars().last().unwrap_or('x') { diff --git a/src/lib.rs b/src/lib.rs index 5963822..bf9b61c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,11 @@ +#![cfg_attr( + feature = "cargo-clippy", + allow( + clippy::comparison_chain, + clippy::if_same_then_else, + clippy::match_like_matches_macro, + ) +)] //! calculation + conversion //! //! cpc parses and evaluates strings of math, with support for units and conversion. 128-bit decimal floating points are used for high accuracy. @@ -59,7 +67,7 @@ pub struct Number { } impl Number { - pub fn new(value: d128, unit: Unit) -> Number { + pub const fn new(value: d128, unit: Unit) -> Number { Number { value, unit } } } @@ -71,7 +79,7 @@ impl Display for Number { Unit::NoUnit => format!("{}", fixed_value), unit => format!("{} {:?}", fixed_value, unit), }; - return write!(f, "{}", output); + write!(f, "{}", output) } } diff --git a/src/main.rs b/src/main.rs index eaa12a2..bd5dd84 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ use cpc::units::Unit; use std::process::exit; use std::env; -const VERSION: &'static str = env!("CARGO_PKG_VERSION"); +const VERSION: &str = env!("CARGO_PKG_VERSION"); fn print_help() { println!(concat!( @@ -17,9 +17,9 @@ fn print_help() { } fn get_args() -> env::Args { - let mut args = env::args().into_iter(); + let mut args = env::args(); args.next(); // skip binary name - return args; + args } /// CLI interface @@ -44,7 +44,7 @@ fn main() { match arg.as_str() { "-v" | "--verbose" => verbose = true, _ => { - if expression_opt == None { + if expression_opt.is_none() { expression_opt = Some(arg); } else { eprintln!("Unexpected argument: {}", arg); @@ -60,6 +60,7 @@ fn main() { exit(0); }, }; + match eval(&expression, true, Unit::Celsius, verbose) { Ok(answer) => { if !verbose { diff --git a/src/parser.rs b/src/parser.rs index 1aa215f..63eae2d 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -14,7 +14,7 @@ pub struct AstNode { } impl AstNode { - pub fn new(token: Token) -> AstNode { + pub const fn new(token: Token) -> AstNode { AstNode { children: Vec::new(), token, @@ -331,7 +331,7 @@ pub fn parse_level_7(tokens: &[Token], pos: usize) -> Result<(AstNode, usize), S }) }, _ => { - return Err(format!("Expected ( after {} at {:?} but found {:?}", left_paren_pos, token, left_paren_token)); + Err(format!("Expected ( after {} at {:?} but found {:?}", left_paren_pos, token, left_paren_token)) } } }, diff --git a/src/units.rs b/src/units.rs index 5ed1819..6540590 100644 --- a/src/units.rs +++ b/src/units.rs @@ -302,19 +302,19 @@ create_units!( // These functions are here to avoid dividing by small numbers like 0.01, // because d128 gives numbers in E notation in that case. fn get_inverted_millijoule_weight() -> d128 { - return d128!(1000); + d128!(1000) } fn get_inverted_milliwatt_weight() -> d128 { - return d128!(1000); + d128!(1000) } fn get_inverted_milliohm_weight() -> d128 { - return d128!(1000); + d128!(1000) } fn get_inverted_milliampere_weight() -> d128 { - return d128!(1000); + d128!(1000) } fn get_inverted_millivolt_weight() -> d128 { - return d128!(1000); + d128!(1000) } /// Returns the conversion factor between two units. @@ -378,7 +378,7 @@ pub fn add(left: Number, right: Number) -> Result { let (left, right) = convert_to_lowest(left, right)?; Ok(Number::new(left.value + right.value, left.unit)) } else { - return Err(format!("Cannot add {:?} and {:?}", left.unit, right.unit)) + Err(format!("Cannot add {:?} and {:?}", left.unit, right.unit)) } } @@ -390,7 +390,7 @@ pub fn subtract(left: Number, right: Number) -> Result { let (left, right) = convert_to_lowest(left, right)?; Ok(Number::new(left.value - right.value, left.unit)) } else { - return Err(format!("Cannot subtract {:?} by {:?}", left.unit, right.unit)) + Err(format!("Cannot subtract {:?} by {:?}", left.unit, right.unit)) } }