From 352ac0135173506be468dc4ca0a1bded5933b8df Mon Sep 17 00:00:00 2001 From: Ziyang Hu Date: Sun, 9 Oct 2022 22:59:18 +0800 Subject: [PATCH] fix clippy warnings --- src/algo/jlines.rs | 6 ++-- src/data/expr.rs | 7 ++--- src/data/program.rs | 35 ++++++++--------------- src/data/relation.rs | 4 +-- src/data/value.rs | 2 +- src/parse/schema.rs | 62 ++++++++++++++++++++++++----------------- src/parse/sys.rs | 4 +-- src/query/magic.rs | 2 +- src/query/relation.rs | 12 +++----- src/runtime/db.rs | 4 +-- src/runtime/relation.rs | 2 +- 11 files changed, 66 insertions(+), 74 deletions(-) diff --git a/src/algo/jlines.rs b/src/algo/jlines.rs index 0cda66e2..5211e109 100644 --- a/src/algo/jlines.rs +++ b/src/algo/jlines.rs @@ -82,7 +82,7 @@ impl AlgoImpl for JsonReader { let line = line.into_diagnostic()?; let line = line.trim(); if !line.is_empty() { - let row = serde_json::from_str(&line).into_diagnostic()?; + let row = serde_json::from_str(line).into_diagnostic()?; process_row(&row)?; } } @@ -104,12 +104,12 @@ impl AlgoImpl for JsonReader { for line in content.lines() { let line = line.trim(); if !line.is_empty() { - let row = serde_json::from_str(&line).into_diagnostic()?; + let row = serde_json::from_str(line).into_diagnostic()?; process_row(&row)?; } } } else { - let data: JsonValue = serde_json::from_str(&content).into_diagnostic()?; + let data: JsonValue = serde_json::from_str(content).into_diagnostic()?; let rows = data .as_array() .ok_or_else(|| miette!("JSON file is not an array"))?; diff --git a/src/data/expr.rs b/src/data/expr.rs index 8b164822..4f8046ef 100644 --- a/src/data/expr.rs +++ b/src/data/expr.rs @@ -343,7 +343,7 @@ impl Expr { return val.eval(bindings); } } - return Ok(DataValue::Null); + Ok(DataValue::Null) } Expr::Try { clauses, .. } => { if clauses.is_empty() { @@ -564,8 +564,7 @@ impl<'de> Visitor<'de> for OpVisitor { E: Error, { let name = v.strip_prefix("OP_").unwrap().to_ascii_lowercase(); - Ok(get_op(&name) - .ok_or_else(|| E::custom(format!("op not found in serialized data: {}", v)))?) + get_op(&name).ok_or_else(|| E::custom(format!("op not found in serialized data: {}", v))) } } @@ -703,7 +702,7 @@ pub(crate) fn get_op(name: &str) -> Option<&'static Op> { } impl Op { - pub(crate) fn post_process_args(&self, args: &mut Vec) { + pub(crate) fn post_process_args(&self, args: &mut [Expr]) { if self.name.starts_with("OP_REGEX_") { args[1] = Expr::Apply { op: &OP_REGEX, diff --git a/src/data/program.rs b/src/data/program.rs index 43017623..dae71c5e 100644 --- a/src/data/program.rs +++ b/src/data/program.rs @@ -35,7 +35,7 @@ pub(crate) enum QueryAssertion { AssertSome(SourceSpan), } -#[derive(Clone, Eq, PartialEq)] +#[derive(Clone, Eq, PartialEq, Default)] pub(crate) struct QueryOutOptions { pub(crate) limit: Option, pub(crate) offset: Option, @@ -54,20 +54,20 @@ impl Debug for QueryOutOptions { impl Display for QueryOutOptions { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { if let Some(l) = self.limit { - write!(f, ":limit {};\n", l)?; + writeln!(f, ":limit {};", l)?; } if let Some(l) = self.offset { - write!(f, ":offset {};\n", l)?; + writeln!(f, ":offset {};", l)?; } if let Some(l) = self.timeout { - write!(f, ":timeout {};\n", l)?; + writeln!(f, ":timeout {};", l)?; } for (symb, dir) in &self.sorters { write!(f, ":order ")?; if *dir == SortDir::Dsc { write!(f, "-")?; } - write!(f, "{};\n", symb)?; + writeln!(f, "{};", symb)?; } if let Some(( InputRelationHandle { @@ -124,16 +124,16 @@ impl Display for QueryOutOptions { write!(f, " = {}", bind)?; } } - write!(f, "}};\n")?; + writeln!(f, "}};")?; } if let Some(a) = &self.assertion { match a { QueryAssertion::AssertNone(_) => { - write!(f, ":assert none;\n")?; + writeln!(f, ":assert none;")?; } QueryAssertion::AssertSome(_) => { - write!(f, ":assert some;\n")?; + writeln!(f, ":assert some;")?; } } } @@ -142,19 +142,6 @@ impl Display for QueryOutOptions { } } -impl Default for QueryOutOptions { - fn default() -> Self { - Self { - limit: None, - offset: None, - timeout: None, - sorters: vec![], - store_relation: None, - assertion: None, - } - } -} - impl QueryOutOptions { pub(crate) fn num_to_take(&self) -> Option { match (self.limit, self.offset) { @@ -607,7 +594,7 @@ impl Display for InputProgram { f.debug_list().entries(&rule.bindings).finish()?; write!(f, " <- ")?; f.debug_list().entries(&rule.data).finish()?; - write!(f, ";\n")?; + writeln!(f, ";")?; } for (name, rules) in &self.prog { match rules { @@ -639,7 +626,7 @@ impl Display for InputProgram { } write!(f, "{}", atom)?; } - write!(f, ";\n")?; + writeln!(f, ";")?; } } InputRulesOrAlgo::Algo { @@ -673,7 +660,7 @@ impl Display for InputProgram { } write!(f, "{}: {}", k, v)?; } - write!(f, ");\n")?; + writeln!(f, ");")?; } } } diff --git a/src/data/relation.rs b/src/data/relation.rs index 50014a8d..f41ba2f8 100644 --- a/src/data/relation.rs +++ b/src/data/relation.rs @@ -154,7 +154,7 @@ impl NullableColType { let make_err = || DataCoercionFailed(self.clone(), data.clone()); - return Ok(match &self.coltype { + Ok(match &self.coltype { ColType::Any => data, ColType::Int => DataValue::from(data.get_int().ok_or_else(make_err)?), ColType::Float => DataValue::from(data.get_float().ok_or_else(make_err)?), @@ -201,6 +201,6 @@ impl NullableColType { bail!(make_err()) } } - }); + }) } } diff --git a/src/data/value.rs b/src/data/value.rs index 48139b46..98711ba6 100644 --- a/src/data/value.rs +++ b/src/data/value.rs @@ -25,7 +25,7 @@ impl Ord for UuidWrapper { s_h.cmp(&o_h) .then_with(|| s_m.cmp(&o_m)) .then_with(|| s_l.cmp(&o_l)) - .then_with(|| s_rest.cmp(&o_rest)) + .then_with(|| s_rest.cmp(o_rest)) } } diff --git a/src/parse/schema.rs b/src/parse/schema.rs index 5979e9df..a51dc525 100644 --- a/src/parse/schema.rs +++ b/src/parse/schema.rs @@ -1,17 +1,19 @@ use std::collections::BTreeSet; use itertools::Itertools; -use miette::{bail, Diagnostic, ensure, Result}; +use miette::{bail, ensure, Diagnostic, Result}; use smartstring::SmartString; use thiserror::Error; use crate::data::relation::{ColType, ColumnDef, NullableColType, StoredRelationMetadata}; use crate::data::symb::Symbol; use crate::data::value::DataValue; -use crate::parse::{ExtractSpan, Pair, Rule, SourceSpan}; use crate::parse::expr::build_expr; +use crate::parse::{ExtractSpan, Pair, Rule, SourceSpan}; -pub(crate) fn parse_schema(pair: Pair<'_>) -> Result<(StoredRelationMetadata, Vec, Vec)> { +pub(crate) fn parse_schema( + pair: Pair<'_>, +) -> Result<(StoredRelationMetadata, Vec, Vec)> { // assert_eq!(pair.as_rule(), Rule::table_schema); let span = pair.extract_span(); @@ -55,10 +57,14 @@ pub(crate) fn parse_schema(pair: Pair<'_>) -> Result<(StoredRelationMetadata, Ve bail!(EmptySchema(span)) } - Ok((StoredRelationMetadata { - keys, - non_keys: dependents, - }, key_bindings, dep_bindings)) + Ok(( + StoredRelationMetadata { + keys, + non_keys: dependents, + }, + key_bindings, + dep_bindings, + )) } fn parse_col(pair: Pair<'_>) -> Result<(ColumnDef, Symbol)> { @@ -75,26 +81,28 @@ fn parse_col(pair: Pair<'_>) -> Result<(ColumnDef, Symbol)> { match nxt.as_rule() { Rule::col_type => typing = parse_nullable_type(nxt)?, Rule::expr => default_gen = Some(build_expr(nxt, &Default::default())?), - Rule::out_arg => binding_candidate = Some(Symbol::new(nxt.as_str(), nxt.extract_span())), - r => unreachable!("{:?}", r) + Rule::out_arg => { + binding_candidate = Some(Symbol::new(nxt.as_str(), nxt.extract_span())) + } + r => unreachable!("{:?}", r), } } - let binding = binding_candidate.unwrap_or_else(|| - Symbol::new(&name as &str, name_p.extract_span())); - Ok((ColumnDef { - name, - typing, - default_gen, - }, binding)) + let binding = + binding_candidate.unwrap_or_else(|| Symbol::new(&name as &str, name_p.extract_span())); + Ok(( + ColumnDef { + name, + typing, + default_gen, + }, + binding, + )) } pub(crate) fn parse_nullable_type(pair: Pair<'_>) -> Result { let nullable = pair.as_str().ends_with('?'); let coltype = parse_type_inner(pair.into_inner().next().unwrap())?; - Ok(NullableColType { - coltype, - nullable, - }) + Ok(NullableColType { coltype, nullable }) } fn parse_type_inner(pair: Pair<'_>) -> Result { @@ -120,17 +128,19 @@ fn parse_type_inner(pair: Pair<'_>) -> Result { #[diagnostic(code(parser::bad_list_len_in_type))] struct BadListLenSpec(DataValue, #[label] SourceSpan); - let n = dv.get_int() - .ok_or_else(|| BadListLenSpec(dv, span))?; + let n = dv.get_int().ok_or(BadListLenSpec(dv, span))?; ensure!(n >= 0, BadListLenSpec(DataValue::from(n), span)); Some(n as usize) } }; - ColType::List { eltype: eltype.into(), len } + ColType::List { + eltype: eltype.into(), + len, + } } Rule::tuple_type => { - ColType::Tuple(pair.into_inner().map(|p| parse_nullable_type(p)).try_collect()?) + ColType::Tuple(pair.into_inner().map(parse_nullable_type).try_collect()?) } - _ => unreachable!() + _ => unreachable!(), }) -} \ No newline at end of file +} diff --git a/src/parse/sys.rs b/src/parse/sys.rs index 7b48c23e..1bbc560e 100644 --- a/src/parse/sys.rs +++ b/src/parse/sys.rs @@ -16,7 +16,7 @@ pub(crate) enum SysOp { ListRelations, ListRunning, KillRunning(u64), - Explain(InputProgram), + Explain(Box), RemoveRelation(Vec), RenameRelation(Vec<(Symbol, Symbol)>), ShowTrigger(Symbol), @@ -44,7 +44,7 @@ pub(crate) fn parse_sys( } Rule::explain_op => { let prog = parse_query(inner.into_inner().next().unwrap().into_inner(), param_pool)?; - SysOp::Explain(prog) + SysOp::Explain(Box::new(prog)) } Rule::list_relations_op => SysOp::ListRelations, Rule::remove_relations_op => { diff --git a/src/query/magic.rs b/src/query/magic.rs index 5530b8ae..2b61ab54 100644 --- a/src/query/magic.rs +++ b/src/query/magic.rs @@ -332,7 +332,7 @@ impl NormalFormProgram { bindings, span, } => { - let relation = tx.get_relation(&name, false)?; + let relation = tx.get_relation(name, false)?; let fields: BTreeSet<_> = relation .metadata .keys diff --git a/src/query/relation.rs b/src/query/relation.rs index 38e754d7..bf0fec95 100644 --- a/src/query/relation.rs +++ b/src/query/relation.rs @@ -790,8 +790,7 @@ impl RelationRA { ret.extend(found.0); Ok(Some(Tuple(ret))) }) - .map(swap_option_result) - .flatten(), + .filter_map(swap_option_result), ); } } @@ -810,8 +809,7 @@ impl RelationRA { ret.extend(found.0); Ok(Some(Tuple(ret))) }) - .map(swap_option_result) - .flatten(), + .filter_map(swap_option_result), ) }) .flatten_ok() @@ -1169,8 +1167,7 @@ impl InMemRelationRA { ret.extend(found.0); Ok(Some(Tuple(ret))) }) - .map(swap_option_result) - .flatten(), + .filter_map(swap_option_result), ); } } @@ -1189,8 +1186,7 @@ impl InMemRelationRA { ret.extend(found.0); Ok(Some(Tuple(ret))) }) - .map(swap_option_result) - .flatten(), + .filter_map(swap_option_result), ) }) .flatten_ok() diff --git a/src/runtime/db.rs b/src/runtime/db.rs index 9df8b038..e5f62d1b 100644 --- a/src/runtime/db.rs +++ b/src/runtime/db.rs @@ -603,7 +603,7 @@ impl Db { self, sorted_iter, *relation_op, - &meta, + meta, &input_program.get_entry_out_head_or_default()?, ) .wrap_err_with(|| format!("when executing against relation '{}'", meta.name))?; @@ -635,7 +635,7 @@ impl Db { self, scan, *relation_op, - &meta, + meta, &input_program.get_entry_out_head_or_default()?, ) .wrap_err_with(|| format!("when executing against relation '{}'", meta.name))?; diff --git a/src/runtime/relation.rs b/src/runtime/relation.rs index 934ca04c..8854f887 100644 --- a/src/runtime/relation.rs +++ b/src/runtime/relation.rs @@ -320,7 +320,7 @@ impl SessionTx { let metadata = input_meta.metadata.clone(); let last_id = self.relation_store_id.fetch_add(1, Ordering::SeqCst); let meta = RelationHandle { - name: input_meta.name.name.clone(), + name: input_meta.name.name, id: RelationId::new(last_id + 1), metadata, put_triggers: vec![],