Merge pull request #23 from Pyther99/fix-clippy-lints

Fix clippy lints
This commit is contained in:
Kasper 2021-08-16 06:15:52 +02:00 committed by GitHub
commit d8af50d991
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 55 additions and 59 deletions

View File

@ -21,7 +21,7 @@ pub fn evaluate(ast: &AstNode) -> Result<Number, String> {
/// ///
/// All return values of this function are hard-coded. /// All return values of this function are hard-coded.
pub fn factorial(input: d128) -> d128 { pub fn factorial(input: d128) -> d128 {
return lookup_factorial(input.into()); lookup_factorial(input.into())
} }
/// Returns the square root of a [`struct@d128`] /// Returns the square root of a [`struct@d128`]
@ -31,7 +31,7 @@ pub fn sqrt(input: d128) -> d128 {
for _ in 0..10 { for _ in 0..10 {
n = (n + input/n) * half; n = (n + input/n) * half;
} }
return n n
} }
/// Returns the cube root of a [`struct@d128`] /// Returns the cube root of a [`struct@d128`]
@ -43,7 +43,7 @@ pub fn cbrt(input: d128) -> d128 {
let z2 = n*n; let z2 = n*n;
n = n - ((n*z2 - input) / (three*z2)); n = n - ((n*z2 - input) / (three*z2));
} }
return n n
} }
/// Returns the sine of a [`struct@d128`] /// Returns the sine of a [`struct@d128`]
@ -72,19 +72,19 @@ pub fn sin(mut input: d128) -> d128 {
result += neg_one.pow(i) * (input.pow(calc_result) / factorial(calc_result)); result += neg_one.pow(i) * (input.pow(calc_result) / factorial(calc_result));
} }
return negative_correction * result; negative_correction * result
} }
/// Returns the cosine of a [`struct@d128`] /// Returns the cosine of a [`struct@d128`]
pub fn cos(input: d128) -> d128 { pub fn cos(input: d128) -> d128 {
let half_pi = d128!(1.570796326794896619231321691639751); let half_pi = d128!(1.570796326794896619231321691639751);
return sin(half_pi - input); sin(half_pi - input)
} }
/// Returns the tangent of a [`struct@d128`] /// Returns the tangent of a [`struct@d128`]
pub fn tan(input: d128) -> d128 { pub fn tan(input: d128) -> d128 {
return sin(input) / cos(input); sin(input) / cos(input)
} }
/// Evaluate an [`AstNode`] into a [`Number`] /// Evaluate an [`AstNode`] into a [`Number`]
@ -93,7 +93,7 @@ fn evaluate_node(ast_node: &AstNode) -> Result<Number, String> {
let children = &ast_node.children; let children = &ast_node.children;
match token { match token {
Token::Number(number) => { Token::Number(number) => {
Ok(Number::new(number.clone(), Unit::NoUnit)) Ok(Number::new(*number, Unit::NoUnit))
}, },
Token::Constant(constant) => { Token::Constant(constant) => {
match constant { match constant {
@ -112,41 +112,41 @@ fn evaluate_node(ast_node: &AstNode) -> Result<Number, String> {
Cbrt => { Cbrt => {
if child_answer.unit.category() == UnitType::NoType { if child_answer.unit.category() == UnitType::NoType {
let result = cbrt(child_answer.value); let result = cbrt(child_answer.value);
return Ok(Number::new(result, child_answer.unit)) Ok(Number::new(result, child_answer.unit))
} else { } else {
return Err("log() only accepts UnitType::NoType".to_string()) Err("log() only accepts UnitType::NoType".to_string())
} }
}, },
Sqrt => { Sqrt => {
if child_answer.unit.category() == UnitType::NoType { if child_answer.unit.category() == UnitType::NoType {
let result = sqrt(child_answer.value); let result = sqrt(child_answer.value);
return Ok(Number::new(result, child_answer.unit)) Ok(Number::new(result, child_answer.unit))
} else { } else {
return Err("log() only accepts UnitType::NoType".to_string()) Err("log() only accepts UnitType::NoType".to_string())
} }
}, },
Log => { Log => {
if child_answer.unit.category() == UnitType::NoType { if child_answer.unit.category() == UnitType::NoType {
let result = child_answer.value.log10(); let result = child_answer.value.log10();
return Ok(Number::new(result, child_answer.unit)) Ok(Number::new(result, child_answer.unit))
} else { } else {
return Err("log() only accepts UnitType::NoType".to_string()) Err("log() only accepts UnitType::NoType".to_string())
} }
}, },
Ln => { Ln => {
if child_answer.unit.category() == UnitType::NoType { if child_answer.unit.category() == UnitType::NoType {
let result = child_answer.value.ln(); let result = child_answer.value.ln();
return Ok(Number::new(result, child_answer.unit)) Ok(Number::new(result, child_answer.unit))
} else { } else {
return Err("ln() only accepts UnitType::NoType".to_string()) Err("ln() only accepts UnitType::NoType".to_string())
} }
}, },
Exp => { Exp => {
if child_answer.unit.category() == UnitType::NoType { if child_answer.unit.category() == UnitType::NoType {
let result = child_answer.value.exp(child_answer.value); let result = child_answer.value.exp(child_answer.value);
return Ok(Number::new(result, child_answer.unit)) Ok(Number::new(result, child_answer.unit))
} else { } else {
return Err("exp() only accepts UnitType::NoType".to_string()) Err("exp() only accepts UnitType::NoType".to_string())
} }
}, },
Round => { Round => {
@ -155,44 +155,44 @@ fn evaluate_node(ast_node: &AstNode) -> Result<Number, String> {
let rounding_change = result - child_answer.value; let rounding_change = result - child_answer.value;
// If the result was rounded down by 0.5, correct by +1 // If the result was rounded down by 0.5, correct by +1
if rounding_change == d128!(-0.5) { result += d128!(1); } if rounding_change == d128!(-0.5) { result += d128!(1); }
return Ok(Number::new(result, child_answer.unit)) Ok(Number::new(result, child_answer.unit))
}, },
Ceil => { Ceil => {
let mut result = child_answer.value.quantize(d128!(1)); let mut result = child_answer.value.quantize(d128!(1));
let rounding_change = result - child_answer.value; let rounding_change = result - child_answer.value;
if rounding_change.is_negative() { result += d128!(1); } if rounding_change.is_negative() { result += d128!(1); }
return Ok(Number::new(result, child_answer.unit)) Ok(Number::new(result, child_answer.unit))
}, },
Floor => { Floor => {
let mut result = child_answer.value.quantize(d128!(1)); let mut result = child_answer.value.quantize(d128!(1));
let rounding_change = result - child_answer.value; let rounding_change = result - child_answer.value;
if !rounding_change.is_negative() { result -= d128!(1); } if !rounding_change.is_negative() { result -= d128!(1); }
return Ok(Number::new(result, child_answer.unit)) Ok(Number::new(result, child_answer.unit))
}, },
Abs => { Abs => {
let mut result = child_answer.value.abs(); let mut result = child_answer.value.abs();
let rounding_change = result - child_answer.value; let rounding_change = result - child_answer.value;
if rounding_change == d128!(-0.5) { result += d128!(1); } if rounding_change == d128!(-0.5) { result += d128!(1); }
return Ok(Number::new(result, child_answer.unit)) Ok(Number::new(result, child_answer.unit))
}, },
Sin => { Sin => {
let result = sin(child_answer.value); let result = sin(child_answer.value);
return Ok(Number::new(result, child_answer.unit)) Ok(Number::new(result, child_answer.unit))
}, },
Cos => { Cos => {
let result = cos(child_answer.value); let result = cos(child_answer.value);
return Ok(Number::new(result, child_answer.unit)) Ok(Number::new(result, child_answer.unit))
}, },
Tan => { Tan => {
let result = tan(child_answer.value); let result = tan(child_answer.value);
return Ok(Number::new(result, child_answer.unit)) Ok(Number::new(result, child_answer.unit))
}, },
} }
} }
Token::Unit(unit) => { Token::Unit(unit) => {
let child_node = children.get(0).ok_or("Unit has no child[0]")?; let child_node = children.get(0).ok_or("Unit has no child[0]")?;
let child_answer = evaluate_node(child_node)?; let child_answer = evaluate_node(child_node)?;
Ok(Number::new(child_answer.value, unit.clone())) Ok(Number::new(child_answer.value, *unit))
}, },
Token::Negative => { Token::Negative => {
let child_node = children.get(0).ok_or("Negative has no child[0]")?; let child_node = children.get(0).ok_or("Negative has no child[0]")?;
@ -201,7 +201,7 @@ fn evaluate_node(ast_node: &AstNode) -> Result<Number, String> {
}, },
Token::Paren => { Token::Paren => {
let child_node = children.get(0).ok_or("Paren has no child[0]")?; let child_node = children.get(0).ok_or("Paren has no child[0]")?;
return evaluate_node(child_node) evaluate_node(child_node)
}, },
Token::UnaryOperator(operator) => { Token::UnaryOperator(operator) => {
let child_node = children.get(0).ok_or(format!("Token {:?} has no child[0]", token))?; let child_node = children.get(0).ok_or(format!("Token {:?} has no child[0]", token))?;
@ -241,18 +241,18 @@ fn evaluate_node(ast_node: &AstNode) -> Result<Number, String> {
if let Token::Unit(right_unit) = right_child.token { if let Token::Unit(right_unit) = right_child.token {
let left = evaluate_node(left_child)?; let left = evaluate_node(left_child)?;
let result = convert(left, right_unit)?; let result = convert(left, right_unit)?;
return Ok(result) Ok(result)
} else { } else {
return Err("Right side of To operator needs to be a unit".to_string()) Err("Right side of To operator needs to be a unit".to_string())
} }
}, },
Of => { Of => {
let left = evaluate_node(left_child)?; let left = evaluate_node(left_child)?;
let right = evaluate_node(right_child)?; let right = evaluate_node(right_child)?;
if left.unit == Unit::NoUnit { if left.unit == Unit::NoUnit {
return Ok(Number::new(left.value * right.value, right.unit)) Ok(Number::new(left.value * right.value, right.unit))
} else { } else {
return Err("Left side of the Of operator must be NoUnit".to_string()) Err("Left side of the Of operator must be NoUnit".to_string())
} }
}, },
} }

View File

@ -46,7 +46,7 @@ fn read_word_plain(chars: &mut Peekable<Graphemes>) -> String {
break; break;
} }
} }
return word; word
} }
/// Read next as a word, otherwise return empty string. /// Read next as a word, otherwise return empty string.
@ -54,7 +54,7 @@ fn read_word_plain(chars: &mut Peekable<Graphemes>) -> String {
fn read_word(first_c: &str, lexer: &mut Lexer) -> String { fn read_word(first_c: &str, lexer: &mut Lexer) -> String {
let chars = &mut lexer.chars; let chars = &mut lexer.chars;
let mut word = first_c.trim().to_owned(); let mut word = first_c.trim().to_owned();
if word == "" { if word.is_empty() {
// skip whitespace // skip whitespace
while let Some(current_char) = chars.peek() { while let Some(current_char) = chars.peek() {
if current_char.trim().is_empty() { if current_char.trim().is_empty() {
@ -71,7 +71,7 @@ fn read_word(first_c: &str, lexer: &mut Lexer) -> String {
break; break;
} }
} }
if word != "" { if !word.is_empty() {
match *chars.peek().unwrap_or(&"") { match *chars.peek().unwrap_or(&"") {
"2" | "²" => { "2" | "²" => {
word += "2"; word += "2";
@ -84,7 +84,7 @@ fn read_word(first_c: &str, lexer: &mut Lexer) -> String {
_ => {}, _ => {},
} }
} }
return word; word
} }
fn parse_token(c: &str, lexer: &mut Lexer) -> Result<(), String> { fn parse_token(c: &str, lexer: &mut Lexer) -> Result<(), String> {
@ -606,7 +606,7 @@ fn parse_word(word: &str, lexer: &mut Lexer) -> Result<(), String> {
} }
}; };
lexer.tokens.push(token); lexer.tokens.push(token);
return Ok(()); Ok(())
} }
struct Lexer<'a> { struct Lexer<'a> {
@ -655,8 +655,8 @@ pub fn lex(input: &str, remove_trailing_operator: bool, default_degree: Unit) ->
} }
} }
if tokens.len() == 0 { if tokens.is_empty() {
return Err(format!("Input was empty")) return Err("Input was empty".to_string());
} }
let mut token_index = 0; let mut token_index = 0;

View File

@ -232,27 +232,27 @@ pub fn eval(input: &str, allow_trailing_operators: bool, default_degree: Unit, v
match lexer::lex(input, allow_trailing_operators, default_degree) { match lexer::lex(input, allow_trailing_operators, default_degree) {
Ok(tokens) => { Ok(tokens) => {
let lex_time = Instant::now().duration_since(lex_start).as_nanos() as f32; let lex_time = Instant::now().duration_since(lex_start).as_nanos() as f32;
if verbose == true { println!("Lexed TokenVector: {:?}", tokens); } if verbose { println!("Lexed TokenVector: {:?}", tokens); }
let parse_start = Instant::now(); let parse_start = Instant::now();
match parser::parse(&tokens) { match parser::parse(&tokens) {
Ok(ast) => { Ok(ast) => {
let parse_time = Instant::now().duration_since(parse_start).as_nanos() as f32; let parse_time = Instant::now().duration_since(parse_start).as_nanos() as f32;
if verbose == true { println!("Parsed AstNode: {:#?}", ast); } if verbose { println!("Parsed AstNode: {:#?}", ast); }
let eval_start = Instant::now(); let eval_start = Instant::now();
match evaluator::evaluate(&ast) { match evaluator::evaluate(&ast) {
Ok(answer) => { Ok(answer) => {
let eval_time = Instant::now().duration_since(eval_start).as_nanos() as f32; let eval_time = Instant::now().duration_since(eval_start).as_nanos() as f32;
if verbose == true { if verbose {
println!("Evaluated value: {} {:?}", answer.value, answer.unit); println!("Evaluated value: {} {:?}", answer.value, answer.unit);
println!("\u{23f1} {:.3}ms lexing", lex_time/1000.0/1000.0); println!("\u{23f1} {:.3}ms lexing", lex_time/1000.0/1000.0);
println!("\u{23f1} {:.3}ms parsing", parse_time/1000.0/1000.0); println!("\u{23f1} {:.3}ms parsing", parse_time/1000.0/1000.0);
println!("\u{23f1} {:.3}ms evaluation", eval_time/1000.0/1000.0); println!("\u{23f1} {:.3}ms evaluation", eval_time/1000.0/1000.0);
} }
return Ok(answer) Ok(answer)
}, },
Err(e) => Err(format!("Eval error: {}", e)), Err(e) => Err(format!("Eval error: {}", e)),
} }

View File

@ -4,7 +4,7 @@ use decimal::d128;
/// Returns the corresponding [`d128`] of a [`NamedNumber`] /// Returns the corresponding [`d128`] of a [`NamedNumber`]
pub fn lookup_named_number(named_number: &NamedNumber) -> d128 { pub fn lookup_named_number(named_number: &NamedNumber) -> d128 {
return match named_number { match named_number {
Hundred => d128!(100), Hundred => d128!(100),
Thousand => d128!(1000), Thousand => d128!(1000),
Million => d128!(1000000), Million => d128!(1000000),
@ -34,7 +34,7 @@ pub fn lookup_named_number(named_number: &NamedNumber) -> d128 {
/// Returns the factorial of an `i32` as a [`struct@d128`] /// Returns the factorial of an `i32` as a [`struct@d128`]
pub fn lookup_factorial(n: i32) -> d128 { pub fn lookup_factorial(n: i32) -> d128 {
return match n { match n {
0 => d128!(1), 0 => d128!(1),
1 => d128!(1), 1 => d128!(1),
2 => d128!(2), 2 => d128!(2),
@ -1037,5 +1037,5 @@ pub fn lookup_factorial(n: i32) -> d128 {
999 => d128!(4.023872600770937735437024339230041E+2564), 999 => d128!(4.023872600770937735437024339230041E+2564),
1000 => d128!(4.023872600770937735437024339230041E+2567), 1000 => d128!(4.023872600770937735437024339230041E+2567),
_ => d128!("NaN"), _ => d128!("NaN"),
}; }
} }

View File

@ -17,7 +17,7 @@ impl AstNode {
pub fn new(token: Token) -> AstNode { pub fn new(token: Token) -> AstNode {
AstNode { AstNode {
children: Vec::new(), children: Vec::new(),
token: token, token,
} }
} }
} }
@ -253,11 +253,9 @@ pub fn parse_level_5(tokens: &[Token], pos: usize) -> Result<(AstNode, usize), S
let (right_node, next_pos) = parse_level_6(tokens, pos + 1)?; let (right_node, next_pos) = parse_level_6(tokens, pos + 1)?;
let mut new_node = AstNode::new(Token::Negative); let mut new_node = AstNode::new(Token::Negative);
new_node.children.push(right_node); new_node.children.push(right_node);
return Ok((new_node, next_pos)); Ok((new_node, next_pos))
}, },
_ => { _ => parse_level_6(tokens, pos)
return Ok(parse_level_6(tokens, pos)?);
}
} }
} }

View File

@ -325,7 +325,7 @@ fn get_inverted_millivolt_weight() -> d128 {
/// ///
/// This is not sufficient for [`Temperature`] units. /// This is not sufficient for [`Temperature`] units.
pub fn get_conversion_factor(unit: Unit, to_unit: Unit) -> d128 { pub fn get_conversion_factor(unit: Unit, to_unit: Unit) -> d128 {
return unit.weight() / to_unit.weight(); unit.weight() / to_unit.weight()
} }
/// Convert a [`Number`] to a specified [`Unit`]. /// Convert a [`Number`] to a specified [`Unit`].
@ -550,7 +550,7 @@ pub fn to_ideal_joule_unit(number: Number) -> Number {
/// - If you multiply [`ElectricCurrent`] with [`Resistance`], the result has a unit of [`Voltage`] /// - If you multiply [`ElectricCurrent`] with [`Resistance`], the result has a unit of [`Voltage`]
/// - If you multiply [`Power`] with [`Time`], the result has a unit of [`Energy`] /// - If you multiply [`Power`] with [`Time`], the result has a unit of [`Energy`]
pub fn multiply(left: Number, right: Number) -> Result<Number, String> { pub fn multiply(left: Number, right: Number) -> Result<Number, String> {
Ok(actual_multiply(left, right, false)?) actual_multiply(left, right, false)
} }
fn actual_multiply(left: Number, right: Number, swapped: bool) -> Result<Number, String> { fn actual_multiply(left: Number, right: Number, swapped: bool) -> Result<Number, String> {
@ -644,17 +644,15 @@ fn actual_multiply(left: Number, right: Number, swapped: bool) -> Result<Number,
// 1 watt * 1 second = 1 joule // 1 watt * 1 second = 1 joule
let result = (left.value * left.unit.weight()) * (right.value * right.unit.weight() / Unit::Second.weight()); let result = (left.value * left.unit.weight()) * (right.value * right.unit.weight() / Unit::Second.weight());
match right.unit { match right.unit {
Second => return Ok(to_ideal_joule_unit(Number::new(result, Joule))), Second => Ok(to_ideal_joule_unit(Number::new(result, Joule))),
_ => return Ok(to_ideal_unit(Number::new(result, Joule))), _ => Ok(to_ideal_unit(Number::new(result, Joule))),
}; }
} else { } else if swapped {
if swapped == true {
Err(format!("Cannot multiply {:?} and {:?}", right.unit, left.unit)) Err(format!("Cannot multiply {:?} and {:?}", right.unit, left.unit))
} else { } else {
actual_multiply(right, left, true) actual_multiply(right, left, true)
} }
} }
}
/// Divide a [`Number`] by another [`Number`] /// Divide a [`Number`] by another [`Number`]
/// ///