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
This commit is contained in:
Joel Natividad 2022-09-11 17:37:01 -04:00 committed by GitHub
parent 213789b568
commit a64081ac73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 32 additions and 28 deletions

View File

@ -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<Graphemes>) -> 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<Vec<Token>, 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') {

View File

@ -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)
}
}

View File

@ -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 {

View File

@ -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))
}
}
},

View File

@ -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<Number, String> {
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<Number, String> {
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))
}
}