Skip to content

Commit

Permalink
fix: Clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
can-keklik committed Jun 27, 2024
1 parent 43bf988 commit 1881aa2
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 92 deletions.
1 change: 0 additions & 1 deletion lykiadb-connect/src/tcp.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::session::ClientSession;
use lykiadb_server::net::tcp::TcpConnection;
use lykiadb_server::net::{CommunicationError, Message, Request};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpStream;

pub(crate) struct TcpClientSession {
Expand Down
6 changes: 6 additions & 0 deletions lykiadb-server/src/engine/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ pub struct Environment {
envs: Vec<EnvironmentFrame>,
}

impl Default for Environment {
fn default() -> Self {
Self::new()
}
}

impl Environment {
pub fn new() -> Self {
let mut instance = Environment { envs: vec![] };
Expand Down
48 changes: 30 additions & 18 deletions lykiadb-server/src/engine/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ pub struct SourceProcessor {
locals: FxHashMap<usize, usize>,
}

impl Default for SourceProcessor {
fn default() -> Self {
Self::new()
}
}

impl SourceProcessor {
pub fn new() -> SourceProcessor {
SourceProcessor {
Expand All @@ -41,7 +47,7 @@ impl SourceProcessor {
let (scopes, locals) = resolver.resolve().unwrap();

self.scopes = scopes;
self.locals = locals.clone();
self.locals.clone_from(&locals);
program.set_locals(self.locals.clone());

Ok(program)
Expand Down Expand Up @@ -320,7 +326,7 @@ impl VisitorMut<RV, HaltReason> for Interpreter {
value,
raw: _,
span: _,
} => Ok(self.literal_to_rv(&value)),
} => Ok(self.literal_to_rv(value)),
Expr::Grouping { expr, span: _ } => self.visit_expr(expr),
Expr::Unary {
operation,
Expand Down Expand Up @@ -425,7 +431,7 @@ impl VisitorMut<RV, HaltReason> for Interpreter {
name: fn_name.to_string(),
body: Arc::clone(body),
parameters: parameters.iter().map(|x| x.name.to_string()).collect(),
closure: self.env.clone(),
closure: self.env,
};

let callable = Callable(Some(parameters.len()), fun.into());
Expand Down Expand Up @@ -500,13 +506,13 @@ impl VisitorMut<RV, HaltReason> for Interpreter {
body: stmts,
span: _,
} => {
return self.execute_block(&stmts, Some(self.env.clone()));
return self.execute_block(stmts, Some(self.env));
}
Stmt::Expression { expr, span: _ } => {
return self.visit_expr(&expr);
return self.visit_expr(expr);
}
Stmt::Declaration { dst, expr, span: _ } => {
let evaluated = self.visit_expr(&expr)?;
let evaluated = self.visit_expr(expr)?;
self.env_man.write().unwrap().declare(
self.env,
dst.name.to_string(),
Expand All @@ -517,18 +523,18 @@ impl VisitorMut<RV, HaltReason> for Interpreter {
body: stmts,
span: _,
} => {
return self.execute_block(&stmts, None);
return self.execute_block(stmts, None);
}
Stmt::If {
condition,
body,
r#else_body: r#else,
span: _,
} => {
if self.visit_expr(&condition)?.as_bool() {
self.visit_stmt(&body)?;
if self.visit_expr(condition)?.as_bool() {
self.visit_stmt(body)?;
} else if let Some(else_stmt) = r#else {
self.visit_stmt(&else_stmt)?;
self.visit_stmt(else_stmt)?;
}
}
Stmt::Loop {
Expand All @@ -540,13 +546,13 @@ impl VisitorMut<RV, HaltReason> for Interpreter {
self.loop_stack.push_loop(LoopState::Go);
while !self.loop_stack.is_loop_at(LoopState::Broken)
&& (condition.is_none()
|| self.visit_expr(&condition.as_ref().unwrap())?.as_bool())
|| self.visit_expr(condition.as_ref().unwrap())?.as_bool())
{
self.visit_stmt(&body)?;
self.visit_stmt(body)?;
self.loop_stack
.set_loop_state(LoopState::Go, Some(LoopState::Continue));
if let Some(post_id) = post {
self.visit_stmt(&post_id)?;
self.visit_stmt(post_id)?;
}
}
self.loop_stack.pop_loop();
Expand All @@ -567,7 +573,7 @@ impl VisitorMut<RV, HaltReason> for Interpreter {
}
Stmt::Return { span: _, expr } => {
if expr.is_some() {
let ret = self.visit_expr(&expr.as_ref().unwrap())?;
let ret = self.visit_expr(expr.as_ref().unwrap())?;
return Err(HaltReason::Return(ret));
}
return Err(HaltReason::Return(RV::Undefined));
Expand All @@ -582,6 +588,12 @@ pub struct Output {
out: Vec<RV>,
}

impl Default for Output {
fn default() -> Self {
Self::new()
}
}

impl Output {
pub fn new() -> Output {
Output { out: Vec::new() }
Expand Down Expand Up @@ -618,7 +630,7 @@ pub mod test_helpers {
(out.clone(), Runtime::new(RuntimeMode::File, Some(out)))
}

pub fn exec_assert(code: &str, output: Vec<RV>) -> () {
pub fn exec_assert(code: &str, output: Vec<RV>) {
let (out, mut runtime) = get_runtime();
runtime.interpret(code).unwrap();
out.write().unwrap().expect(output);
Expand All @@ -641,7 +653,7 @@ mod test {
TestUtils.out(!!!3);
";
let (out, mut runtime) = get_runtime();
runtime.interpret(&code).unwrap();
runtime.interpret(code).unwrap();
out.write().unwrap().expect(vec![
RV::Num(-2.0),
RV::Num(2.0),
Expand All @@ -660,7 +672,7 @@ mod test {
TestUtils.out(-5-2);
";
let (out, mut runtime) = get_runtime();
runtime.interpret(&code).unwrap();
runtime.interpret(code).unwrap();
out.write().unwrap().expect(vec![
RV::Num(7.0),
RV::Num(28.0),
Expand All @@ -682,7 +694,7 @@ mod test {
TestUtils.out(!(5 || 0) || (1 && 0));
";
let (out, mut runtime) = get_runtime();
runtime.interpret(&code).unwrap();
runtime.interpret(code).unwrap();
out.write().unwrap().expect(vec![
RV::Bool(true),
RV::Bool(true),
Expand Down
48 changes: 23 additions & 25 deletions lykiadb-server/src/engine/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl Serialize for RV {
RV::Array(arr) => {
let mut seq = serializer.serialize_seq(None).unwrap();
let arr = (arr.borrow() as &RwLock<Vec<RV>>).read().unwrap();
for item in (&arr).iter() {
for item in arr.iter() {
seq.serialize_element(&item)?;
}
seq.end()
Expand All @@ -107,7 +107,7 @@ impl Serialize for RV {
let arr = (obj.borrow() as &RwLock<FxHashMap<String, RV>>)
.read()
.unwrap();
for (key, value) in (&arr).iter() {
for (key, value) in arr.iter() {
map.serialize_entry(key, value)?;
}
map.end()
Expand Down Expand Up @@ -393,35 +393,33 @@ mod test {

#[test]
fn test_is_value_truthy() {
assert_eq!((RV::Null).as_bool(), false);
assert_eq!((RV::Undefined).as_bool(), false);
assert_eq!((RV::NaN).as_bool(), false);
assert_eq!((RV::Bool(false)).as_bool(), false);
assert_eq!((RV::Bool(true)).as_bool(), true);
assert_eq!((RV::Num(0.0)).as_bool(), false);
assert_eq!((RV::Num(0.1)).as_bool(), true);
assert_eq!((RV::Num(-0.1)).as_bool(), true);
assert_eq!((RV::Num(1.0)).as_bool(), true);
assert_eq!((RV::Num(-1.0)).as_bool(), true);
assert_eq!((RV::Str(Arc::new("".to_owned()))).as_bool(), false);
assert_eq!((RV::Str(Arc::new("0".to_owned()))).as_bool(), true);
assert_eq!((RV::Str(Arc::new("false".to_owned()))).as_bool(), true);
assert_eq!((RV::Str(Arc::new("true".to_owned()))).as_bool(), true);
assert_eq!((RV::Str(Arc::new("foo".to_owned()))).as_bool(), true);
assert_eq!((RV::Array(alloc_shared(vec![]))).as_bool(), true);
assert_eq!(
(RV::Object(alloc_shared(FxHashMap::default()))).as_bool(),
true
);
assert_eq!(
assert!(!(RV::Null).as_bool());
assert!(!(RV::Undefined).as_bool());
assert!(!(RV::NaN).as_bool());
assert!(!(RV::Bool(false)).as_bool());
assert!((RV::Bool(true)).as_bool());
assert!(!(RV::Num(0.0)).as_bool());
assert!((RV::Num(0.1)).as_bool());
assert!((RV::Num(-0.1)).as_bool());
assert!((RV::Num(1.0)).as_bool());
assert!((RV::Num(-1.0)).as_bool());
assert!(!(RV::Str(Arc::new("".to_owned()))).as_bool());
assert!((RV::Str(Arc::new("0".to_owned()))).as_bool());
assert!((RV::Str(Arc::new("false".to_owned()))).as_bool());
assert!((RV::Str(Arc::new("true".to_owned()))).as_bool());
assert!((RV::Str(Arc::new("foo".to_owned()))).as_bool());
assert!((RV::Array(alloc_shared(vec![]))).as_bool());
assert!(
(RV::Object(alloc_shared(FxHashMap::default()))).as_bool()
);
assert!(
(RV::Callable(
Some(1),
Arc::new(Function::Lambda {
function: |_, _| Ok(RV::Undefined)
})
))
.as_bool(),
true
.as_bool()
);
}

Expand Down
32 changes: 16 additions & 16 deletions lykiadb-server/src/lang/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ impl<'a> Parser<'a> {
return false;
}
}
return true;
true
}

fn match_next_all_of(&mut self, tokens: &[TokenType]) -> bool {
Expand All @@ -685,7 +685,7 @@ impl<'a> Parser<'a> {
for _ in 0..tokens.len() {
self.advance();
}
return true;
true
}

fn match_next_one_of(&mut self, tokens: &[TokenType]) -> bool {
Expand Down Expand Up @@ -1111,9 +1111,9 @@ impl<'a> Parser<'a> {
}
}

return Ok(SqlCollectionSubquery::Group {
Ok(SqlCollectionSubquery::Group {
values: subquery_group,
});
})
}

fn sql_select_where(&mut self) -> ParseResult<Option<Box<SqlExpr>>> {
Expand Down Expand Up @@ -1156,7 +1156,7 @@ impl<'a> Parser<'a> {
// If the next token is a left paren, then it must be either a select statement or a recursive subquery
let parsed = self.sql_select_subquery_join()?; // TODO(vck): Check if using _collection variant makes sense.
self.expected(sym!(RightParen))?; // closing paren
return Ok(parsed);
Ok(parsed)
} else if let Some(collection) = self.sql_collection_identifier()? {
return Ok(SqlCollectionSubquery::Collection(collection));
} else {
Expand Down Expand Up @@ -1196,36 +1196,36 @@ impl<'a> Parser<'a> {
match (expr_conj_fst, expr_conj_sec) {
(Some(SqlKeyword(Is)), None) => {
let right = self.sql_expression()?;
return Ok(Box::new(SqlExpr::Is { left, right }));
Ok(Box::new(SqlExpr::Is { left, right }))
}
(Some(SqlKeyword(Is)), Some(SqlKeyword(Not))) => {
let right = self.sql_expression()?;
return Ok(Box::new(SqlExpr::IsNot { left, right }));
Ok(Box::new(SqlExpr::IsNot { left, right }))
}
(Some(SqlKeyword(In)), None) => {
let right = self.sql_expression()?;
return Ok(Box::new(SqlExpr::In { left, right }));
Ok(Box::new(SqlExpr::In { left, right }))
}
(Some(SqlKeyword(Not)), Some(SqlKeyword(In))) => {
let right = self.sql_expression()?;
return Ok(Box::new(SqlExpr::NotIn { left, right }));
Ok(Box::new(SqlExpr::NotIn { left, right }))
}
(Some(SqlKeyword(Like)), None) => {
let right = self.sql_expression()?;
return Ok(Box::new(SqlExpr::Like { left, right }));
Ok(Box::new(SqlExpr::Like { left, right }))
}
(Some(SqlKeyword(Not)), Some(SqlKeyword(Like))) => {
let right = self.sql_expression()?;
return Ok(Box::new(SqlExpr::NotLike { left, right }));
Ok(Box::new(SqlExpr::NotLike { left, right }))
}
(Some(SqlKeyword(Between)), None) => {
return Ok(self.sql_expression_between_and(left, false)?);
self.sql_expression_between_and(left, false)
}
(Some(SqlKeyword(Not)), Some(SqlKeyword(Between))) => {
return Ok(self.sql_expression_between_and(left, true)?);
self.sql_expression_between_and(left, true)
}
_ => {
return Ok(Box::new(SqlExpr::Default(expr)));
Ok(Box::new(SqlExpr::Default(expr)))
}
}
}
Expand All @@ -1250,10 +1250,10 @@ impl<'a> Parser<'a> {
}));
}

return Ok(Box::new(SqlExpr::Between {
Ok(Box::new(SqlExpr::Between {
expr: left,
lower,
upper,
}));
}))
}
}
Loading

0 comments on commit 1881aa2

Please sign in to comment.