diff --git a/src/command.rs b/src/command.rs index 0a85f56..d702fb3 100644 --- a/src/command.rs +++ b/src/command.rs @@ -205,7 +205,7 @@ impl Command { }) } else { diag.report(&keyword_loc, Severity::Error, &format!("Applied rule must be a symbol but got {} instead", expr.human_name())); - return None + None } } TokenKind::Equals => { @@ -278,7 +278,7 @@ impl Command { } } _ => { - diag.report(&keyword.loc, Severity::Error, &format!("expected symbol")); + diag.report(&keyword.loc, Severity::Error, "expected symbol"); None } } @@ -352,9 +352,8 @@ fn delete_item_by_key<'a, K, V>(assoc: &'a mut Vec<(K, V)>, needle: &'a K) -> bo impl Context { pub fn new(interactive: bool) -> Self { - let mut rules = Vec::new(); // TODO: you can potentially `delete` the replace rule (you should not be able to do that) - rules.push(( + let rules = vec![( Token { kind: TokenKind::Ident, text: "replace".to_string(), @@ -364,7 +363,7 @@ impl Context { rule: Rule::Replace, history: vec![] } - )); + )]; Self { interactive, rules, @@ -379,7 +378,7 @@ impl Context { match rule { Rule::User{head, body, ..} => { write!(sink, "{name} :: {head}", name = name.text)?; - if history.len() > 0 { + if !history.is_empty() { writeln!(sink, " {{")?; for (_, command) in history { match command { @@ -437,7 +436,7 @@ impl Context { Command::DefineRule{ name, rule } => { if let Some((existing_name, _)) = get_item_by_key(&self.rules, &name) { diag.report(&name.loc, Severity::Error, &format!("redefinition of existing rule {}", name.text)); - diag.report(&existing_name.loc, Severity::Info, &format!("the original definition is located here")); + diag.report(&existing_name.loc, Severity::Info, "the original definition is located here"); return None } if let Rule::User{head, body, ..} = &rule { @@ -463,7 +462,7 @@ impl Context { match rule.clone() { Rule::User {head, body} => Rule::User{head: body, body: head}, Rule::Replace => { - diag.report(&bar.loc, Severity::Error, &format!("irreversible rule")); + diag.report(&bar.loc, Severity::Error, "irreversible rule"); return None; } } @@ -490,8 +489,8 @@ impl Context { println!(" => {}", &frame.expr); frame.history.push((previous_expr, command)); } else { - diag.report(&bar.loc, Severity::Error, &format!("To apply a rule to an expression you need to first start shaping the expression, but no shaping is currently in place")); - diag.report(&bar.loc, Severity::Info, &format!(" {{ - to start shaping")); + diag.report(&bar.loc, Severity::Error, "To apply a rule to an expression you need to first start shaping the expression, but no shaping is currently in place"); + diag.report(&bar.loc, Severity::Info, " { - to start shaping"); return None } } @@ -501,7 +500,7 @@ impl Context { if let Some((name, head)) = frame.rule_via_shaping.take() { if let Some((existing_name, _)) = get_item_by_key(&self.rules, &name) { diag.report(&token.loc, Severity::Error, &format!("redefinition of existing rule {}", &name.text)); - diag.report(&existing_name.loc, Severity::Info, &format!("the original definition is located here")); + diag.report(&existing_name.loc, Severity::Info, "the original definition is located here"); return None } diag.report(&name.loc, Severity::Info, &format!("defined rule {} :: {head} = {body}", &name.text)); @@ -569,7 +568,7 @@ impl Context { match get_item_by_key(&self.rules, &name) { Some((_, RuleDefinition{rule: Rule::User{head, body}, history})) => { print!("{name} :: {head}", name = name.text); - if history.len() > 0 { + if !history.is_empty() { println!(" {{"); for (_, command) in history { match command { diff --git a/src/engine/lexer.rs b/src/engine/lexer.rs index abd00d9..c7fb832 100644 --- a/src/engine/lexer.rs +++ b/src/engine/lexer.rs @@ -179,7 +179,7 @@ impl Lexer { while eol < self.chars.len() && self.chars[eol] != '\n' { eol += 1; } - return self.chars[self.bol..eol].to_vec(); + self.chars[self.bol..eol].to_vec() } pub fn loc(&self) -> Loc { @@ -282,7 +282,7 @@ impl Lexer { '+' => Token {kind: TokenKind::Plus, text, loc}, '-' => Token {kind: TokenKind::Dash, text, loc}, '*' => Token {kind: TokenKind::Asterisk, text, loc}, - '/' => if let Some(_) = self.drop_char_if(|x| x == '/') { + '/' => if self.drop_char_if(|x| x == '/').is_some() { self.drop_line(); continue 'again; } else { diff --git a/src/engine/rule.rs b/src/engine/rule.rs index c8acc13..3dc945b 100644 --- a/src/engine/rule.rs +++ b/src/engine/rule.rs @@ -143,12 +143,12 @@ impl Rule { Some(false) } None => { - diag.report(&apply_command_loc, Severity::Error, &format!("unknown rule application strategy '{}'", meta_strategy_name.report())); + diag.report(apply_command_loc, Severity::Error, &format!("unknown rule application strategy '{}'", meta_strategy_name.report())); None } } } else { - diag.report(&apply_command_loc, Severity::Error, &format!("strategy must be a symbol but got {} {}", meta_strategy.human_name(), &meta_strategy)); + diag.report(apply_command_loc, Severity::Error, &format!("strategy must be a symbol but got {} {}", meta_strategy.human_name(), &meta_strategy)); None } } else { @@ -161,7 +161,7 @@ impl Rule { let mut match_count = 0; apply_impl(self, expr, strategy, apply_command_loc, &mut match_count, diag)?; if match_count == 0 { - diag.report(&apply_command_loc, Severity::Error, &format!("no match found")); + diag.report(apply_command_loc, Severity::Error, "no match found"); return None } Some(()) diff --git a/src/main.rs b/src/main.rs index 80d8754..c379fcc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -65,7 +65,7 @@ fn parse_and_process_command(context: &mut Context, lexer: &mut Lexer, diag: &mu fn interpret_file(file_path: &str) -> Option<()> { let mut context = Context::new(false); - let source = fs::read_to_string(&file_path).unwrap(); + let source = fs::read_to_string(file_path).unwrap(); let mut lexer = Lexer::new(source.chars().collect(), Some(file_path.to_string())); let mut diag = StdoutDiagnoster{}; while !context.quit && lexer.peek_token().kind != TokenKind::End {