From 3003429f1db4e1aeff17a878f873c55c81cdf8d9 Mon Sep 17 00:00:00 2001 From: XFFXFF <1247714429@qq.com> Date: Mon, 18 Sep 2023 18:12:16 +0800 Subject: [PATCH] make clippy happy --- components/lox-compile/src/compile.rs | 4 +- components/lox-ir/src/bytecode.rs | 4 ++ components/lox-ir/src/span.rs | 4 ++ components/lox-ir/src/word.rs | 2 +- components/lox-lex/src/lex.rs | 2 +- components/lox-parse/src/parser.rs | 15 ++--- components/lox-parse/src/tests.rs | 79 +++++++++++++-------------- components/lox-parse/src/tokens.rs | 6 -- 8 files changed, 55 insertions(+), 61 deletions(-) diff --git a/components/lox-compile/src/compile.rs b/components/lox-compile/src/compile.rs index 2e5fbef..242a43b 100644 --- a/components/lox-compile/src/compile.rs +++ b/components/lox-compile/src/compile.rs @@ -25,8 +25,8 @@ fn compile_expr(db: &dyn crate::Db, expr: &syntax::Expr, chunk: &mut Chunk) { syntax::Expr::BooleanLiteral(_) => todo!(), syntax::Expr::NilLiteral => todo!(), syntax::Expr::BinaryOp(left, op, right) => { - compile_expr(db, &left, chunk); - compile_expr(db, &right, chunk); + compile_expr(db, left, chunk); + compile_expr(db, right, chunk); match op { syntax::Op::Plus => chunk.emit_byte(Code::Add), syntax::Op::Minus => chunk.emit_byte(Code::Subtract), diff --git a/components/lox-ir/src/bytecode.rs b/components/lox-ir/src/bytecode.rs index d750b58..57a36b9 100644 --- a/components/lox-ir/src/bytecode.rs +++ b/components/lox-ir/src/bytecode.rs @@ -25,4 +25,8 @@ impl Chunk { pub fn len(&self) -> usize { self.code.len() } + + pub fn is_empty(&self) -> bool { + self.code.is_empty() + } } diff --git a/components/lox-ir/src/span.rs b/components/lox-ir/src/span.rs index 28d9d94..31f142c 100644 --- a/components/lox-ir/src/span.rs +++ b/components/lox-ir/src/span.rs @@ -35,6 +35,10 @@ impl Span { self.end.0 - self.start.0 } + pub fn is_empty(&self) -> bool { + self.start == self.end + } + pub fn to(self, other: Span) -> Span { assert!(self.start <= other.start && other.end >= self.end); Span { diff --git a/components/lox-ir/src/word.rs b/components/lox-ir/src/word.rs index c3fea1f..46a00fe 100644 --- a/components/lox-ir/src/word.rs +++ b/components/lox-ir/src/word.rs @@ -13,6 +13,6 @@ impl Word { } pub fn as_str(self, db: &dyn Db) -> &str { - &self.string(db) + self.string(db) } } diff --git a/components/lox-lex/src/lex.rs b/components/lox-lex/src/lex.rs index 6d0f1a5..be86d5d 100644 --- a/components/lox-lex/src/lex.rs +++ b/components/lox-lex/src/lex.rs @@ -75,7 +75,7 @@ where push_token(Token::Alphabetic(text)); } '0'..='9' => { - let text = self.accumulate(ch, |c| matches!(c, '0'..='9')); + let text = self.accumulate(ch, |c| c.is_ascii_digit()); push_token(Token::Number(text)); } '+' | '-' | '*' | '/' | '!' | '<' | '>' | '=' => { diff --git a/components/lox-parse/src/parser.rs b/components/lox-parse/src/parser.rs index 2f9a8ad..f5e027a 100644 --- a/components/lox-parse/src/parser.rs +++ b/components/lox-parse/src/parser.rs @@ -121,7 +121,7 @@ impl<'me> Parser<'me> { parse_rhs: impl Fn(&mut Self) -> Option, ) -> Option { for op in ops { - if let Some(_) = self.eat_op(*op) { + if self.eat_op(*op).is_some() { let right = parse_rhs(self)?; let left = Expr::BinaryOp(Box::new(left), *op, Box::new(right)); return Some(left); @@ -132,7 +132,7 @@ impl<'me> Parser<'me> { fn unary(&mut self) -> Option { for op in &[Op::Minus, Op::Bang] { - if let Some(_) = self.eat_op(*op) { + if self.eat_op(*op).is_some() { let expr = self.unary()?; return Some(Expr::UnaryOp(*op, Box::new(expr))); } @@ -141,11 +141,11 @@ impl<'me> Parser<'me> { } fn primary(&mut self) -> Option { - if let Some(_) = self.eat(Keyword::True) { + if self.eat(Keyword::True).is_some() { Some(Expr::BooleanLiteral(true)) - } else if let Some(_) = self.eat(Keyword::False) { + } else if self.eat(Keyword::False).is_some() { Some(Expr::BooleanLiteral(false)) - } else if let Some(_) = self.eat(Keyword::Nil) { + } else if self.eat(Keyword::Nil).is_some() { Some(Expr::NilLiteral) } else if let Some((_, word)) = self.eat(Number) { Some(Expr::NumberLiteral(word)) @@ -165,11 +165,6 @@ impl<'me> Parser<'me> { test.test(self.db, self.tokens.peek()?, span) } - /// Span of the next pending token, or the span of EOF if there is no next token. - fn peek_span(&mut self) -> Span { - self.tokens.peek_span() - } - /// If the next pending token matches `test`, consumes it and /// returns the span + narrowed view. Otherwise returns None /// and has no effect. Returns None if there is no pending token. diff --git a/components/lox-parse/src/tests.rs b/components/lox-parse/src/tests.rs index 41ca4fe..bc274da 100644 --- a/components/lox-parse/src/tests.rs +++ b/components/lox-parse/src/tests.rs @@ -1,52 +1,49 @@ -use std::{ - fs, - path::{Path, PathBuf}, -}; - -#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)] -struct TestCase { - lox: PathBuf, - syntax: PathBuf, - text: String, -} - -impl TestCase { - fn list(path: &'static str) -> Vec { - let crate_root_dir = Path::new(env!("CARGO_MANIFEST_DIR")); - let test_data_dir = crate_root_dir.join("test_data"); - let dir = test_data_dir.join(path); - - let mut res = Vec::new(); - let read_dir = fs::read_dir(&dir) - .unwrap_or_else(|err| panic!("can't `read_dir` {}: {err}", dir.display())); - for file in read_dir { - let file = file.unwrap(); - let path = file.path(); - if path.extension().unwrap_or_default() == "lox" { - let rs = path; - let rast = rs.with_extension("syntax"); - let text = fs::read_to_string(&rs).unwrap(); - res.push(TestCase { - lox: rs, - syntax: rast, - text, - }); - } - } - res.sort(); - res - } -} - #[cfg(test)] mod tests { + use std::{path::{PathBuf, Path}, fs}; + use expect_test::expect_file; use lox_ir::{diagnostic::Diagnostics, input_file::InputFile, word::Word}; use salsa::DebugWithDb; use crate::file_parser::parse_file; - use super::TestCase; + #[derive(PartialEq, Eq, PartialOrd, Ord, Debug)] + struct TestCase { + lox: PathBuf, + syntax: PathBuf, + text: String, + } + + impl TestCase { + fn list(path: &'static str) -> Vec { + let crate_root_dir = Path::new(env!("CARGO_MANIFEST_DIR")); + let test_data_dir = crate_root_dir.join("test_data"); + let dir = test_data_dir.join(path); + + let mut res = Vec::new(); + let read_dir = fs::read_dir(&dir) + .unwrap_or_else(|err| panic!("can't `read_dir` {}: {err}", dir.display())); + for file in read_dir { + let file = file.unwrap(); + let path = file.path(); + if path.extension().unwrap_or_default() == "lox" { + let rs = path; + let rast = rs.with_extension("syntax"); + let text = fs::read_to_string(&rs).unwrap(); + res.push(TestCase { + lox: rs, + syntax: rast, + text, + }); + } + } + res.sort(); + res + } + } + + #[salsa::db(crate::Jar, lox_ir::Jar, lox_lex::Jar)] #[derive(Default)] diff --git a/components/lox-parse/src/tokens.rs b/components/lox-parse/src/tokens.rs index d5f2d3c..5a90923 100644 --- a/components/lox-parse/src/tokens.rs +++ b/components/lox-parse/src/tokens.rs @@ -52,12 +52,6 @@ impl<'me> Tokens<'me> { } } - /// True if we skipped a newline after consuming - /// the last token. - pub(crate) fn skipped_newline(&self) -> bool { - self.skipped >= Skipped::Newline - } - /// True if we skipped whitespace after consuming /// the last token. pub(crate) fn skipped_any(&self) -> bool {