Fix clippy lints (#2119)

This commit is contained in:
James Lindsay
2024-11-29 22:58:49 +00:00
committed by GitHub
parent 00629571f2
commit e3bb11ec1b
32 changed files with 694 additions and 456 deletions

View File

@@ -9,7 +9,7 @@ macro_rules! generate_benchmarks {
$(
c.bench_function(concat!("parse ", $input), |b| {
b.iter(|| {
let _ = black_box(ast::Node::from_str($input)).unwrap();
let _ = black_box(ast::Node::try_parse_from_str($input)).unwrap();
});
});
)*
@@ -17,7 +17,7 @@ macro_rules! generate_benchmarks {
fn evaluation_bench(c: &mut Criterion) {
$(
let expr = ast::Node::from_str($input).unwrap().0;
let expr = ast::Node::try_parse_from_str($input).unwrap().0;
let context = EvalContext::default();
c.bench_function(concat!("eval ", $input), |b| {

View File

@@ -4,9 +4,10 @@ use lazy_static::lazy_static;
use num_complex::{Complex, ComplexFloat};
use crate::value::{Number, Value};
type FunctionImplementation = Box<dyn Fn(&[Value]) -> Option<Value> + Send + Sync>;
lazy_static! {
pub static ref DEFAULT_FUNCTIONS: HashMap<&'static str, Box<dyn Fn(&[Value]) -> Option<Value> + Send + Sync>> = {
let mut map: HashMap<&'static str, Box<dyn Fn(&[Value]) -> Option<Value> + Send + Sync>> = HashMap::new();
pub static ref DEFAULT_FUNCTIONS: HashMap<&'static str, FunctionImplementation> = {
let mut map: HashMap<&'static str, FunctionImplementation> = HashMap::new();
map.insert(
"sin",

View File

@@ -14,7 +14,7 @@ use parser::ParseError;
use value::Value;
pub fn evaluate(expression: &str) -> Result<(Result<Value, EvalError>, Unit), ParseError> {
let expr = ast::Node::from_str(expression);
let expr = ast::Node::try_parse_from_str(expression);
let context = EvalContext::default();
expr.map(|(node, unit)| (node.eval(&context), unit))
}
@@ -37,7 +37,7 @@ mod tests {
let expected_value = $expected_value;
let expected_unit = $expected_unit;
let expr = ast::Node::from_str($input);
let expr = ast::Node::try_parse_from_str($input);
let context = EvalContext::default();
let (actual_value, actual_unit) = expr.map(|(node, unit)| (node.eval(&context), unit)).unwrap();

View File

@@ -56,7 +56,7 @@ pub enum ParseError {
}
impl Node {
pub fn from_str(s: &str) -> Result<(Node, Unit), ParseError> {
pub fn try_parse_from_str(s: &str) -> Result<(Node, Unit), ParseError> {
let pairs = ExprParser::parse(Rule::program, s).map_err(Box::new)?;
let (node, metadata) = parse_expr(pairs)?;
Ok((node, metadata.unit))
@@ -325,7 +325,7 @@ mod tests {
$(
#[test]
fn $name() {
let result = Node::from_str($input).unwrap();
let result = Node::try_parse_from_str($input).unwrap();
assert_eq!(result.0, $expected);
}
)*
@@ -334,7 +334,7 @@ mod tests {
test_parser! {
test_parse_int_literal: "42" => Node::Lit(Literal::Float(42.0)),
test_parse_float_literal: "3.14" => Node::Lit(Literal::Float(3.14)),
test_parse_float_literal: "3.14" => Node::Lit(Literal::Float(#[allow(clippy::approx_constant)] 3.14)),
test_parse_ident: "x" => Node::Var("x".to_string()),
test_parse_unary_neg: "-42" => Node::UnaryOp {
expr: Box::new(Node::Lit(Literal::Float(42.0))),