From 4aa032139defce07c0281abc64027d82f51f7df6 Mon Sep 17 00:00:00 2001 From: Teascade Date: Thu, 25 Jun 2020 01:13:35 +0300 Subject: [PATCH] Fix clippy warnings --- src/compiler.rs | 18 +++++++++++------- src/errors.rs | 2 +- src/main.rs | 1 + src/parser/mod.rs | 30 +++++++++++++----------------- src/parser/parsed_reid.rs | 12 ++++-------- src/vm/mod.rs | 4 ++-- 6 files changed, 32 insertions(+), 35 deletions(-) diff --git a/src/compiler.rs b/src/compiler.rs index 4b1f16a..2ec1758 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -1,3 +1,4 @@ +use std::collections::hash_map::Entry; use std::collections::HashMap; use std::num::ParseIntError; @@ -216,14 +217,17 @@ impl Scope { variable: String, vtype: VariableType, ) -> Result<(), CompilerError> { - if self.variables.contains_key(&variable) { - Err(CompilerError::VariableExists(pos, variable)) - } else if let Some(inner) = &mut self.inner_scope { - inner.add_var(pos, variable, vtype) + let entry = self.variables.entry(variable.clone()); + if let Entry::Vacant(e) = entry { + if let Some(inner) = &mut self.inner_scope { + inner.add_var(pos, variable, vtype) + } else { + e.insert((self.counter, vtype)); + self.counter += 1; + Ok(()) + } } else { - self.variables.insert(variable, (self.counter, vtype)); - self.counter += 1; - Ok(()) + Err(CompilerError::VariableExists(pos, variable)) } } diff --git a/src/errors.rs b/src/errors.rs index f003ba4..79762e4 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -89,7 +89,7 @@ impl Display for CompilerError { CompilerError::LetFailed(pos, error) => { format!("Let statement failed at {}:\n {}", pos, error) } - CompilerError::CanNotAssignVoidType => format!("Can not assign void type here"), + CompilerError::CanNotAssignVoidType => "Can not assign void type here".to_string(), CompilerError::FunctionNotFound(pos, name, params) => format!( "Function with signature {}{} not found at {}", name, diff --git a/src/main.rs b/src/main.rs index 533bf05..afb2cef 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ #![forbid(unsafe_code)] +#![warn(clippy::all)] mod compiler; mod errors; diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 09308a5..0ae0fed 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -122,14 +122,12 @@ impl Parser { } else { false } + } else if ALLOWED_IDENT_BEGIN_CHARS.contains(lowercase) { + ident = Some(peek.to_string()); + self.inconfidence += 1; + true } else { - if ALLOWED_IDENT_BEGIN_CHARS.contains(lowercase) { - ident = Some(peek.to_string()); - self.inconfidence += 1; - true - } else { - false - } + false } } else { false @@ -155,14 +153,12 @@ impl Parser { self.inconfidence += 1; true } + } else if peek == '"' { + content = Some(String::new()); + self.inconfidence += 1; + true } else { - if peek == '"' { - content = Some(String::new()); - self.inconfidence += 1; - true - } else { - false - } + false } } else { false @@ -273,7 +269,7 @@ impl<'a> Expect<'a> { } } - pub fn and<'b, T: Into>(self, other: Expectable) -> Expects<'a> { + pub fn and>(self, other: Expectable) -> Expects<'a> { let texts = if let Some(text) = self.text { Some(vec![text]) } else { @@ -281,7 +277,7 @@ impl<'a> Expect<'a> { }; Expects::and( Expects { - texts: texts, + texts, parser: self.parser, }, other, @@ -295,7 +291,7 @@ pub struct Expects<'a> { } impl<'a> Expects<'a> { - pub fn and<'b, T: Into>(mut self, expect: Expectable) -> Expects<'a> { + pub fn and>(mut self, expect: Expectable) -> Expects<'a> { let other = self.parser.expect(expect); if let Some(texts) = &mut self.texts { if let Some(text) = other.get_inconfident() { diff --git a/src/parser/parsed_reid.rs b/src/parser/parsed_reid.rs index 29d6e03..51242bd 100644 --- a/src/parser/parsed_reid.rs +++ b/src/parser/parsed_reid.rs @@ -15,7 +15,7 @@ impl Statement { pub fn parse(parser: &mut Parser) -> Result { let pos = parser.pos(); - if let Some(_) = parser.expect_static("let").get() { + if parser.expect_static("let").get().is_some() { let ident = parser .expect_ident() .get_or(SyntaxError::ExpectedIdent(pos))?; @@ -57,7 +57,7 @@ impl Expression { pub fn parse(parser: &mut Parser) -> Result { let begin_pos = parser.pos(); - if let Some(_) = parser.expect_static("{").get() { + if parser.expect_static("{").get().is_some() { let mut statement_list = Vec::new(); while { match Statement::parse(parser) { @@ -68,7 +68,7 @@ impl Expression { Err(_) => false, } } {} - if let Some(_) = parser.expect_static("}").get() { + if parser.expect_static("}").get().is_some() { Ok(Expression::BlockExpr(begin_pos, statement_list)) } else { Err(SyntaxError::ExpectedToken(parser.pos(), '}')) @@ -81,11 +81,7 @@ impl Expression { match Expression::parse(parser) { Ok(exp) => { arg_list.push(exp); - if let Some(_) = parser.expect_static(",").get() { - true - } else { - false - } + parser.expect_static(",").get().is_some() } Err(err) => { if arg_list.is_empty() { diff --git a/src/vm/mod.rs b/src/vm/mod.rs index 77a05d4..3e79ba5 100644 --- a/src/vm/mod.rs +++ b/src/vm/mod.rs @@ -43,7 +43,7 @@ impl VirtualMachine { pub fn run(&mut self) -> Result<(), RuntimePanic> { let mut error = None; while error.is_none() { - if let Some(command) = self.compiled.list.get(self.position).map(|v| v.clone()) { + if let Some(command) = self.compiled.list.get(self.position).cloned() { self.position += 1; if let Err(err) = self.execute(command.clone()) { error = Some(err); @@ -140,7 +140,7 @@ impl VirtualMachine { } Command::StringLit(string) => { if self.stack.len() < usize::MAX { - self.stack.push(Value::StringVal(string.clone())); + self.stack.push(Value::StringVal(string)); //dbg!("String literal added to stack", string, &self.stack); Ok(()) } else {