improve code (some clippy::pedantic lints)

This commit is contained in:
nora 2022-01-04 22:01:55 +01:00
parent a42cec3075
commit 5699ab190a
7 changed files with 33 additions and 36 deletions

View file

@ -76,8 +76,7 @@ pub enum ElsePart<'ast> {
impl ElsePart<'_> { impl ElsePart<'_> {
pub fn span(&self) -> Span { pub fn span(&self) -> Span {
match self { match self {
ElsePart::Else(_, span) => *span, ElsePart::Else(_, span) | ElsePart::ElseIf(_, span) => *span,
ElsePart::ElseIf(_, span) => *span,
} }
} }
} }
@ -123,12 +122,12 @@ pub enum Literal<'ast> {
impl Literal<'_> { impl Literal<'_> {
pub fn span(&self) -> Span { pub fn span(&self) -> Span {
match self { match self {
Literal::String(_, span) => *span, Literal::String(_, span)
Literal::Number(_, span) => *span, | Literal::Number(_, span)
Literal::Array(_, span) => *span, | Literal::Array(_, span)
Literal::Object(span) => *span, | Literal::Object(span)
Literal::Boolean(_, span) => *span, | Literal::Boolean(_, span)
Literal::Null(span) => *span, | Literal::Null(span) => *span,
} }
} }
} }

View file

@ -28,8 +28,7 @@ impl Env<'_> {
env.locals.get(&name.sym).copied().or_else(|| { env.locals.get(&name.sym).copied().or_else(|| {
env.outer env.outer
.as_ref() .as_ref()
.map(|outer| lookup_inner(&outer.borrow(), name)) .and_then(|outer| lookup_inner(&outer.borrow(), name))
.flatten()
}) })
} }
@ -68,7 +67,7 @@ pub fn compile<'ast, 'bc, 'gc>(
blocks: Vec::new_in(bytecode_bump), blocks: Vec::new_in(bytecode_bump),
current_block: 0, current_block: 0,
bump: bytecode_bump, bump: bytecode_bump,
env: Rc::new(RefCell::new(Default::default())), env: Rc::new(RefCell::new(Env::default())),
rt, rt,
}; };

View file

@ -156,7 +156,7 @@ impl Drop for RtAlloc {
for str in &self.symbols { for str in &self.symbols {
let raw = str.0.as_ptr(); let raw = str.0.as_ptr();
// SAFETY: No one has free these, see `Gc<T>` // SAFETY: No one has free these, see `Gc<T>`
let _ = unsafe { Box::from_raw(raw) }; drop(unsafe { Box::from_raw(raw) });
} }
} }
} }
@ -171,7 +171,7 @@ impl Symbol {
} }
pub fn as_str(&self) -> &str { pub fn as_str(&self) -> &str {
self.gc.deref() &*self.gc
} }
} }

View file

@ -124,8 +124,7 @@ impl<'code, 'gc> Lexer<'code, 'gc> {
fn expect(&mut self, expected: char) -> bool { fn expect(&mut self, expected: char) -> bool {
self.code self.code
.peek() .peek()
.map(|(_, char)| *char == expected) .map_or(false, |(_, char)| *char == expected)
.unwrap_or(false)
} }
fn maybe_next_char( fn maybe_next_char(
@ -372,17 +371,17 @@ mod test {
#[test] #[test]
fn smiley_face() { fn smiley_face() {
lex_test(">>.<<") lex_test(">>.<<");
} }
#[test] #[test]
fn greater_than_less_than_equal() { fn greater_than_less_than_equal() {
lex_test(">= <= == < < >=") lex_test(">= <= == < < >=");
} }
#[test] #[test]
fn no_no_no() { fn no_no_no() {
lex_test("!= != = !=") lex_test("!= != = !=");
} }
#[test] #[test]
@ -401,7 +400,7 @@ mod test {
#[test] #[test]
fn fancy_stuff() { fn fancy_stuff() {
lex_test(". ,- * -, .") lex_test(". ,- * -, .");
} }
#[test] #[test]
@ -422,7 +421,7 @@ pls :) o(* ̄▽ ̄*)ブ
i like the indentation here ngl | sneak for -> ## for ## <- sneak for i like the indentation here ngl | sneak for -> ## for ## <- sneak for
## and", ## and",
) );
} }
#[test] #[test]
@ -437,42 +436,42 @@ pls :) o(* ̄▽ ̄*)ブ
# # and # # and
## or ## or
", ",
) );
} }
#[test] #[test]
fn greeting() { fn greeting() {
lex_test("-.- /%") lex_test("-.- /%");
} }
#[test] #[test]
fn countdown() { fn countdown() {
lex_test("3 . . 2 . . 1 . . 0") lex_test("3 . . 2 . . 1 . . 0");
} }
#[test] #[test]
fn larger_numbers() { fn larger_numbers() {
lex_test("123456789, 123456789.1234, 64785903") lex_test("123456789, 123456789.1234, 64785903");
} }
#[test] #[test]
fn string() { fn string() {
lex_test(r#""uwu""#) lex_test(r#""uwu""#);
} }
#[test] #[test]
fn strings() { fn strings() {
lex_test(r#"( "hi" "uwu" "\"uwu\"" "no \\ u" )"#) lex_test(r#"( "hi" "uwu" "\"uwu\"" "no \\ u" )"#);
} }
#[test] #[test]
fn keywords() { 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] #[test]
fn keyword_and_ident() { 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] #[test]
@ -501,7 +500,7 @@ pls :) o(* ̄▽ ̄*)ブ
.map(|word| format!("{} ", word)) .map(|word| format!("{} ", word))
.collect::<StdString>(); .collect::<StdString>();
lex_test(&sentences) lex_test(&sentences);
} }
#[test] #[test]
@ -515,6 +514,6 @@ pls :) o(* ̄▽ ̄*)ブ
println("Hello \\ World!") println("Hello \\ World!")
} }
}"#, }"#,
) );
} }
} }

View file

@ -46,19 +46,19 @@ pub fn run_program(program: &str, cfg: &mut Config) {
let ast = parse::parse(lexer, &ast_alloc); let ast = parse::parse(lexer, &ast_alloc);
match ast { 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), 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 { if cfg.debug {
println!("AST:\n{:?}\n", ast); println!("AST:\n{:?}\n", ast);
} }
let bytecode_alloc = Bump::new(); 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 { match bytecode {
Ok(code) => { Ok(code) => {
@ -107,5 +107,5 @@ pub fn _fuzz_lex(program: &str) {
// SAFETY: Just this scope // SAFETY: Just this scope
let mut runtime = unsafe { RtAlloc::new() }; let mut runtime = unsafe { RtAlloc::new() };
let lexer = lex::Lexer::new(program, &mut runtime); let lexer = lex::Lexer::new(program, &mut runtime);
for _ in lexer {} for _token in lexer {}
} }

View file

@ -29,6 +29,6 @@ fn main() {
} }
} }
} else { } else {
eprintln!("Usage: <filename>") eprintln!("Usage: <filename>");
} }
} }

View file

@ -217,7 +217,7 @@ where
Ok(IfStmt { Ok(IfStmt {
span: keyword_span span: keyword_span
.extend(body.span) .extend(body.span)
.option_extend(else_part.as_ref().map(|part| part.span())), .option_extend(else_part.as_ref().map(ElsePart::span)),
cond, cond,
body, body,
else_part: else_part.map(|part| &*self.bump.alloc(part)), else_part: else_part.map(|part| &*self.bump.alloc(part)),