Fix clippy lints

This commit is contained in:
Juhani Krekelä 2023-06-01 19:55:09 +03:00
parent 7191d5a378
commit 18ef6c8787
4 changed files with 26 additions and 26 deletions

View File

@ -51,7 +51,7 @@ fn format_number(number: f64) -> String {
// Remove trailing zeroes
let decimal = decimal.trim_end_matches('0');
if decimal.len() == 0 {
if decimal.is_empty() {
format!("{sign}{grouped}")
} else {
format!("{sign}{grouped}.{decimal}")
@ -67,54 +67,54 @@ fn prefixed_unit(quantity: MetricQuantity) -> PrefixedUnit {
match quantity.unit {
Metric::Metre => {
if absolute >= 1000.0 {
return PrefixedUnit(1000.0, "km");
PrefixedUnit(1000.0, "km")
} else if absolute >= 1.0 {
return PrefixedUnit(1.0, "m");
PrefixedUnit(1.0, "m")
} else if absolute >= 0.01 {
return PrefixedUnit(0.01, "cm");
PrefixedUnit(0.01, "cm")
} else {
return PrefixedUnit(0.001, "mm");
PrefixedUnit(0.001, "mm")
}
}
Metric::Gram => {
if absolute >= 1000.0 {
return PrefixedUnit(1000.0, "kg");
PrefixedUnit(1000.0, "kg")
} else {
return PrefixedUnit(1.0, "g");
PrefixedUnit(1.0, "g")
}
}
Metric::Celsius => PrefixedUnit(1.0, "°C"),
Metric::SquareMetre => {
if absolute >= 1000.0 * 1000.0 {
return PrefixedUnit(1000.0 * 1000.0, "km²");
PrefixedUnit(1000.0 * 1000.0, "km²")
} else if absolute >= 1.0 {
return PrefixedUnit(1.0, "");
PrefixedUnit(1.0, "")
} else if absolute >= 0.01 * 0.01 {
return PrefixedUnit(0.01 * 0.01, "cm²");
PrefixedUnit(0.01 * 0.01, "cm²")
} else {
return PrefixedUnit(0.001 * 0.001, "mm²");
PrefixedUnit(0.001 * 0.001, "mm²")
}
}
Metric::CubicMetre => {
if absolute >= 1000.0 * 1000.0 * 1000.0 {
return PrefixedUnit(1000.0 * 1000.0 * 1000.0, "km³");
PrefixedUnit(1000.0 * 1000.0 * 1000.0, "km³")
} else if absolute >= 1.0 {
return PrefixedUnit(1.0, "");
PrefixedUnit(1.0, "")
} else if absolute >= 0.000_001 { // 0.01 * 0.01 * 0.01 rounds wrong
return PrefixedUnit(0.000_001, "cm³");
PrefixedUnit(0.000_001, "cm³")
} else {
return PrefixedUnit(0.001 * 0.001 * 0.001, "mm³");
PrefixedUnit(0.001 * 0.001 * 0.001, "mm³")
}
}
Metric::Litre => {
if absolute >= 1.0 {
return PrefixedUnit(1.0, "l");
PrefixedUnit(1.0, "l")
} else if absolute >= 0.1 {
return PrefixedUnit(0.1, "dl");
PrefixedUnit(0.1, "dl")
} else if absolute >= 0.01 {
return PrefixedUnit(0.01, "cl");
PrefixedUnit(0.01, "cl")
} else {
return PrefixedUnit(0.001, "ml");
PrefixedUnit(0.001, "ml")
}
}
}

View File

@ -28,7 +28,7 @@ pub fn run(input: &str) -> Result<String, String> {
}
};
if non_metric.len() == 0 {
if non_metric.is_empty() {
return Err("Expected quantity or quantities to convert".to_string());
}
@ -59,7 +59,7 @@ pub fn run(input: &str) -> Result<String, String> {
let amount = metric.into_iter().map(|quantity| { quantity.amount }).sum();
let quantity = MetricQuantity {
amount: amount,
amount,
unit: metric_unit.expect("we must have at least one quantity by this point"),
};

View File

@ -10,7 +10,7 @@ fn main() {
let args = args[1..].join(" ");
if args.len() == 0 {
if args.is_empty() {
loop {
print!("> ");
match io::stdout().flush() {
@ -28,7 +28,7 @@ fn main() {
process::exit(1);
}
}
if input.len() == 0 {
if input.is_empty() {
println!(); // Add a newline if user pressed ^D
break;
}

View File

@ -36,7 +36,7 @@ pub fn parse(input: &str) -> Result<Vec<NonMetricQuantity>, ParseError> {
let unit = parse_unit(unit)?;
let quantity = NonMetricQuantity {
amount: amount.take().expect("must have read a number to be in this state"),
unit: unit,
unit,
};
quantities.push(quantity);
state = Expect::Number;
@ -56,7 +56,7 @@ pub fn parse(input: &str) -> Result<Vec<NonMetricQuantity>, ParseError> {
fn parse_number(input: String) -> Result<f64, ParseError> {
let no_whitespace: String = input.chars().filter(|c| !c.is_whitespace()).collect();
no_whitespace.parse().or_else(|_| Err(ParseError::NotValidNumber(input)))
no_whitespace.parse().map_err(|_| ParseError::NotValidNumber(input))
}
fn parse_unit(input: String) -> Result<NonMetric, ParseError> {
@ -411,7 +411,7 @@ fn tokenize(input: &str) -> Vec<Token> {
}
match state {
TokState::Neutral => { assert!(token.len() == 0); }
TokState::Neutral => { assert!(token.is_empty()); }
TokState::Number => { tokens.push(Token::Number(token.trim().to_string())); }
TokState::Unit(_) => { tokens.push(Token::Unit(token.trim().to_string())); }
}