From 5699ab190a9415828170cfa0990d648c7011eee0 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Tue, 4 Jan 2022 22:01:55 +0100 Subject: [PATCH] improve code (some clippy::pedantic lints) --- src/ast.rs | 15 +++++++-------- src/compile.rs | 5 ++--- src/gc.rs | 4 ++-- src/lex.rs | 33 ++++++++++++++++----------------- src/lib.rs | 8 ++++---- src/main.rs | 2 +- src/parse.rs | 2 +- 7 files changed, 33 insertions(+), 36 deletions(-) diff --git a/src/ast.rs b/src/ast.rs index 38a916b..c17d4cd 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -76,8 +76,7 @@ pub enum ElsePart<'ast> { impl ElsePart<'_> { pub fn span(&self) -> Span { match self { - ElsePart::Else(_, span) => *span, - ElsePart::ElseIf(_, span) => *span, + ElsePart::Else(_, span) | ElsePart::ElseIf(_, span) => *span, } } } @@ -123,12 +122,12 @@ pub enum Literal<'ast> { impl Literal<'_> { pub fn span(&self) -> Span { match self { - Literal::String(_, span) => *span, - Literal::Number(_, span) => *span, - Literal::Array(_, span) => *span, - Literal::Object(span) => *span, - Literal::Boolean(_, span) => *span, - Literal::Null(span) => *span, + Literal::String(_, span) + | Literal::Number(_, span) + | Literal::Array(_, span) + | Literal::Object(span) + | Literal::Boolean(_, span) + | Literal::Null(span) => *span, } } } diff --git a/src/compile.rs b/src/compile.rs index 9a0166e..63b311a 100644 --- a/src/compile.rs +++ b/src/compile.rs @@ -28,8 +28,7 @@ impl Env<'_> { env.locals.get(&name.sym).copied().or_else(|| { env.outer .as_ref() - .map(|outer| lookup_inner(&outer.borrow(), name)) - .flatten() + .and_then(|outer| lookup_inner(&outer.borrow(), name)) }) } @@ -68,7 +67,7 @@ pub fn compile<'ast, 'bc, 'gc>( blocks: Vec::new_in(bytecode_bump), current_block: 0, bump: bytecode_bump, - env: Rc::new(RefCell::new(Default::default())), + env: Rc::new(RefCell::new(Env::default())), rt, }; diff --git a/src/gc.rs b/src/gc.rs index 9a0a0d8..2176294 100644 --- a/src/gc.rs +++ b/src/gc.rs @@ -156,7 +156,7 @@ impl Drop for RtAlloc { for str in &self.symbols { let raw = str.0.as_ptr(); // SAFETY: No one has free these, see `Gc` - let _ = unsafe { Box::from_raw(raw) }; + drop(unsafe { Box::from_raw(raw) }); } } } @@ -171,7 +171,7 @@ impl Symbol { } pub fn as_str(&self) -> &str { - self.gc.deref() + &*self.gc } } diff --git a/src/lex.rs b/src/lex.rs index 3065253..a22f739 100644 --- a/src/lex.rs +++ b/src/lex.rs @@ -124,8 +124,7 @@ impl<'code, 'gc> Lexer<'code, 'gc> { fn expect(&mut self, expected: char) -> bool { self.code .peek() - .map(|(_, char)| *char == expected) - .unwrap_or(false) + .map_or(false, |(_, char)| *char == expected) } fn maybe_next_char( @@ -372,17 +371,17 @@ mod test { #[test] fn smiley_face() { - lex_test(">>.<<") + lex_test(">>.<<"); } #[test] fn greater_than_less_than_equal() { - lex_test(">= <= == < < >=") + lex_test(">= <= == < < >="); } #[test] fn no_no_no() { - lex_test("!= != = !=") + lex_test("!= != = !="); } #[test] @@ -401,7 +400,7 @@ mod test { #[test] fn fancy_stuff() { - lex_test(". ,- * -, .") + lex_test(". ,- * -, ."); } #[test] @@ -422,7 +421,7 @@ pls :) o(* ̄▽ ̄*)ブ i like the indentation here ngl | sneak for -> ## for ## <- sneak for ## and", - ) + ); } #[test] @@ -437,42 +436,42 @@ pls :) o(* ̄▽ ̄*)ブ # # and ## or ", - ) + ); } #[test] fn greeting() { - lex_test("-.- /%") + lex_test("-.- /%"); } #[test] fn countdown() { - lex_test("3 . . 2 . . 1 . . 0") + lex_test("3 . . 2 . . 1 . . 0"); } #[test] fn larger_numbers() { - lex_test("123456789, 123456789.1234, 64785903") + lex_test("123456789, 123456789.1234, 64785903"); } #[test] fn string() { - lex_test(r#""uwu""#) + lex_test(r#""uwu""#); } #[test] fn strings() { - lex_test(r#"( "hi" "uwu" "\"uwu\"" "no \\ u" )"#) + lex_test(r#"( "hi" "uwu" "\"uwu\"" "no \\ u" )"#); } #[test] fn keywords() { - lex_test("let fn if else loop while break for true false null and not or print") + lex_test("let fn if else loop while break for true false null and not or print"); } #[test] fn keyword_and_ident() { - lex_test("let variable be a loop if false is true") + lex_test("let variable be a loop if false is true"); } #[test] @@ -501,7 +500,7 @@ pls :) o(* ̄▽ ̄*)ブ .map(|word| format!("{} ", word)) .collect::(); - lex_test(&sentences) + lex_test(&sentences); } #[test] @@ -515,6 +514,6 @@ pls :) o(* ̄▽ ̄*)ブ println("Hello \\ World!") } }"#, - ) + ); } } diff --git a/src/lib.rs b/src/lib.rs index 2a61658..2d92be8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -46,19 +46,19 @@ pub fn run_program(program: &str, cfg: &mut Config) { let ast = parse::parse(lexer, &ast_alloc); match ast { - Ok(ast) => process_ast(program, ast, runtime, cfg), + Ok(ast) => process_ast(program, &ast, runtime, cfg), Err(err) => errors::display_error(program, err), } } -fn process_ast(program: &str, ast: Program, mut runtime: RtAlloc, cfg: &mut Config<'_>) { +fn process_ast(program: &str, ast: &Program, mut runtime: RtAlloc, cfg: &mut Config<'_>) { if cfg.debug { println!("AST:\n{:?}\n", ast); } let bytecode_alloc = Bump::new(); - let bytecode = compile::compile(&ast, &bytecode_alloc, &mut runtime); + let bytecode = compile::compile(ast, &bytecode_alloc, &mut runtime); match bytecode { Ok(code) => { @@ -107,5 +107,5 @@ pub fn _fuzz_lex(program: &str) { // SAFETY: Just this scope let mut runtime = unsafe { RtAlloc::new() }; let lexer = lex::Lexer::new(program, &mut runtime); - for _ in lexer {} + for _token in lexer {} } diff --git a/src/main.rs b/src/main.rs index 38f3173..5e58915 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,6 +29,6 @@ fn main() { } } } else { - eprintln!("Usage: ") + eprintln!("Usage: "); } } diff --git a/src/parse.rs b/src/parse.rs index 4324968..89e8c2e 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -217,7 +217,7 @@ where Ok(IfStmt { span: keyword_span .extend(body.span) - .option_extend(else_part.as_ref().map(|part| part.span())), + .option_extend(else_part.as_ref().map(ElsePart::span)), cond, body, else_part: else_part.map(|part| &*self.bump.alloc(part)),