From 0c783b1e94539fdd074d1555269c6590b3ab4d95 Mon Sep 17 00:00:00 2001 From: Petr Portnov Date: Sun, 29 Oct 2023 21:52:00 +0300 Subject: [PATCH] chore: resolve Clippy warnings --- crates/jrsonnet-evaluator/src/arr/mod.rs | 3 +- crates/jrsonnet-evaluator/src/arr/spec.rs | 6 ++-- crates/jrsonnet-evaluator/src/evaluate/mod.rs | 2 +- .../src/function/builtin.rs | 11 +++---- .../src/integrations/serde.rs | 8 ++--- crates/jrsonnet-evaluator/src/manifest.rs | 1 + crates/jrsonnet-evaluator/src/obj.rs | 11 ++++--- .../jrsonnet-evaluator/src/stdlib/format.rs | 2 +- .../src/typed/conversions.rs | 32 ++++++++----------- crates/jrsonnet-evaluator/src/typed/mod.rs | 2 +- 10 files changed, 37 insertions(+), 41 deletions(-) diff --git a/crates/jrsonnet-evaluator/src/arr/mod.rs b/crates/jrsonnet-evaluator/src/arr/mod.rs index 0c685d7a..e3cc7e40 100644 --- a/crates/jrsonnet-evaluator/src/arr/mod.rs +++ b/crates/jrsonnet-evaluator/src/arr/mod.rs @@ -7,8 +7,7 @@ use jrsonnet_parser::LocExpr; use crate::{function::FuncVal, gc::TraceBox, tb, Context, Result, Thunk, Val}; mod spec; -pub use spec::ArrayLike; -pub(crate) use spec::*; +pub use spec::{ArrayLike, *}; /// Represents a Jsonnet array value. #[derive(Debug, Clone, Trace)] diff --git a/crates/jrsonnet-evaluator/src/arr/spec.rs b/crates/jrsonnet-evaluator/src/arr/spec.rs index 6b367d6c..355fa176 100644 --- a/crates/jrsonnet-evaluator/src/arr/spec.rs +++ b/crates/jrsonnet-evaluator/src/arr/spec.rs @@ -372,7 +372,7 @@ impl RangeArray { pub fn new_inclusive(start: i32, end: i32) -> Self { Self { start, end } } - fn range(&self) -> impl Iterator + ExactSizeIterator + DoubleEndedIterator { + fn range(&self) -> impl ExactSizeIterator + DoubleEndedIterator { WithExactSize( self.start..=self.end, (self.end as usize) @@ -461,7 +461,7 @@ impl ArrayLike for MappedArray { ArrayThunk::Waiting(..) => {} }; - let ArrayThunk::Waiting(_) = + let ArrayThunk::Waiting(()) = replace(&mut self.cached.borrow_mut()[index], ArrayThunk::Pending) else { unreachable!() @@ -508,7 +508,7 @@ impl ArrayLike for MappedArray { match &self.cached.borrow()[index] { ArrayThunk::Computed(c) => return Some(Thunk::evaluated(c.clone())), ArrayThunk::Errored(e) => return Some(Thunk::errored(e.clone())), - ArrayThunk::Waiting(_) | ArrayThunk::Pending => {} + ArrayThunk::Waiting(()) | ArrayThunk::Pending => {} }; Some(Thunk::new(ArrayElement { diff --git a/crates/jrsonnet-evaluator/src/evaluate/mod.rs b/crates/jrsonnet-evaluator/src/evaluate/mod.rs index 79567e3f..127f287c 100644 --- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs +++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs @@ -89,7 +89,7 @@ pub fn evaluate_comp( specs: &[CompSpec], callback: &mut impl FnMut(Context) -> Result<()>, ) -> Result<()> { - match specs.get(0) { + match specs.first() { None => callback(ctx)?, Some(CompSpec::IfSpec(IfSpecData(cond))) => { if bool::from_untyped(evaluate(ctx.clone(), cond)?)? { diff --git a/crates/jrsonnet-evaluator/src/function/builtin.rs b/crates/jrsonnet-evaluator/src/function/builtin.rs index 3656e7c3..293b021c 100644 --- a/crates/jrsonnet-evaluator/src/function/builtin.rs +++ b/crates/jrsonnet-evaluator/src/function/builtin.rs @@ -6,7 +6,7 @@ use jrsonnet_interner::IStr; use super::{arglike::ArgsLike, parse::parse_builtin_call, CallLocation}; use crate::{gc::TraceBox, tb, Context, Result, Val}; -/// Can't have str | IStr, because constant BuiltinParam causes +/// Can't have [`str`] | [`IStr`], because constant [`BuiltinParam`] causes /// E0492: constant functions cannot refer to interior mutable data #[derive(Clone, Trace)] pub struct ParamName(Option>); @@ -27,10 +27,9 @@ impl ParamName { } impl PartialEq for ParamName { fn eq(&self, other: &IStr) -> bool { - match &self.0 { - Some(s) => s.as_bytes() == other.as_bytes(), - None => false, - } + self.0 + .as_ref() + .map_or(false, |s| s.as_bytes() == other.as_bytes()) } } @@ -87,7 +86,7 @@ impl NativeCallback { params: params .into_iter() .map(|n| BuiltinParam { - name: ParamName::new_dynamic(n.to_string()), + name: ParamName::new_dynamic(n), has_default: false, }) .collect(), diff --git a/crates/jrsonnet-evaluator/src/integrations/serde.rs b/crates/jrsonnet-evaluator/src/integrations/serde.rs index d3dab7af..9214ad1f 100644 --- a/crates/jrsonnet-evaluator/src/integrations/serde.rs +++ b/crates/jrsonnet-evaluator/src/integrations/serde.rs @@ -152,18 +152,18 @@ impl<'de> Deserialize<'de> for Val { impl Serialize for Val { fn serialize(&self, serializer: S) -> Result where - S: serde::Serializer, + S: Serializer, { match self { Val::Bool(v) => serializer.serialize_bool(*v), Val::Null => serializer.serialize_none(), Val::Str(s) => serializer.serialize_str(&s.clone().into_flat()), Val::Num(n) => { - if n.fract() != 0.0 { - serializer.serialize_f64(*n) - } else { + if n.fract() == 0.0 { let n = *n as i64; serializer.serialize_i64(n) + } else { + serializer.serialize_f64(*n) } } #[cfg(feature = "exp-bigint")] diff --git a/crates/jrsonnet-evaluator/src/manifest.rs b/crates/jrsonnet-evaluator/src/manifest.rs index ea5f93b4..e7973ca6 100644 --- a/crates/jrsonnet-evaluator/src/manifest.rs +++ b/crates/jrsonnet-evaluator/src/manifest.rs @@ -175,6 +175,7 @@ pub fn manifest_json_ex(val: &Val, options: &JsonFormat<'_>) -> Result { manifest_json_ex_buf(val, &mut out, &mut String::new(), options)?; Ok(out) } +#[allow(clippy::too_many_lines)] fn manifest_json_ex_buf( val: &Val, buf: &mut String, diff --git a/crates/jrsonnet-evaluator/src/obj.rs b/crates/jrsonnet-evaluator/src/obj.rs index a47ab1ab..2db22574 100644 --- a/crates/jrsonnet-evaluator/src/obj.rs +++ b/crates/jrsonnet-evaluator/src/obj.rs @@ -163,6 +163,7 @@ pub struct OopObject { this_entries: Cc>, value_cache: RefCell), CacheValue>>, } +#[allow(clippy::missing_fields_in_debug)] impl Debug for OopObject { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("OopObject") @@ -347,7 +348,7 @@ impl ObjValue { out.with_super(self); let mut member = out.field(key); if value.flags.add() { - member = member.add() + member = member.add(); } if let Some(loc) = value.location { member = member.with_location(loc); @@ -395,7 +396,7 @@ impl ObjValue { pub fn get(&self, key: IStr) -> Result> { self.run_assertions()?; - self.get_for(key, self.0.this().unwrap_or(self.clone())) + self.get_for(key, self.0.this().unwrap_or_else(|| self.clone())) } pub fn get_for(&self, key: IStr, this: ObjValue) -> Result> { @@ -474,7 +475,7 @@ impl ObjValue { type Output = Val; fn get(self: Box) -> Result { - Ok(self.obj.get_or_bail(self.key)?) + self.obj.get_or_bail(self.key) } } @@ -495,7 +496,7 @@ impl ObjValue { SuperDepth::default(), &mut |depth, index, name, visibility| { let new_sort_key = FieldSortKey::new(depth, index); - let entry = out.entry(name.clone()); + let entry = out.entry(name); let (visible, _) = entry.or_insert((true, new_sort_key)); match visibility { Visibility::Normal => {} @@ -634,7 +635,7 @@ impl OopObject { SuperDepth::default(), &mut |depth, index, name, visibility| { let new_sort_key = FieldSortKey::new(depth, index); - let entry = out.entry(name.clone()); + let entry = out.entry(name); let (visible, _) = entry.or_insert((true, new_sort_key)); match visibility { Visibility::Normal => {} diff --git a/crates/jrsonnet-evaluator/src/stdlib/format.rs b/crates/jrsonnet-evaluator/src/stdlib/format.rs index 59e085ca..d82384ad 100644 --- a/crates/jrsonnet-evaluator/src/stdlib/format.rs +++ b/crates/jrsonnet-evaluator/src/stdlib/format.rs @@ -248,7 +248,7 @@ pub fn parse_code(str: &str) -> ParseResult<'_, Code<'_>> { let (cflags, str) = try_parse_cflags(str)?; let (width, str) = try_parse_field_width(str)?; let (precision, str) = try_parse_precision(str)?; - let (_, str) = try_parse_length_modifier(str)?; + let ((), str) = try_parse_length_modifier(str)?; let (convtype, str) = parse_conversion_type(str)?; Ok(( diff --git a/crates/jrsonnet-evaluator/src/typed/conversions.rs b/crates/jrsonnet-evaluator/src/typed/conversions.rs index a4b43504..08f5154d 100644 --- a/crates/jrsonnet-evaluator/src/typed/conversions.rs +++ b/crates/jrsonnet-evaluator/src/typed/conversions.rs @@ -433,25 +433,21 @@ impl Typed for IBytes { } fn from_untyped(value: Val) -> Result { - match &value { - Val::Arr(a) => { - if let Some(bytes) = a.as_any().downcast_ref::() { - return Ok(bytes.0.as_slice().into()); - }; - ::TYPE.check(&value)?; - // Any::downcast_ref::(&a); - let mut out = Vec::with_capacity(a.len()); - for e in a.iter() { - let r = e?; - out.push(u8::from_untyped(r)?); - } - Ok(out.as_slice().into()) - } - _ => { - ::TYPE.check(&value)?; - unreachable!() - } + let Val::Arr(a) = &value else { + ::TYPE.check(&value)?; + unreachable!() + }; + if let Some(bytes) = a.as_any().downcast_ref::() { + return Ok(bytes.0.as_slice().into()); + }; + ::TYPE.check(&value)?; + // Any::downcast_ref::(&a); + let mut out = Vec::with_capacity(a.len()); + for e in a.iter() { + let r = e?; + out.push(u8::from_untyped(r)?); } + Ok(out.as_slice().into()) } } diff --git a/crates/jrsonnet-evaluator/src/typed/mod.rs b/crates/jrsonnet-evaluator/src/typed/mod.rs index 7423f4d0..e4c343b4 100644 --- a/crates/jrsonnet-evaluator/src/typed/mod.rs +++ b/crates/jrsonnet-evaluator/src/typed/mod.rs @@ -90,7 +90,7 @@ fn push_type_description( item: impl Fn() -> Result<()>, ) -> Result<()> { State::push_description(error_reason, || match item() { - Ok(_) => Ok(()), + Ok(()) => Ok(()), Err(mut e) => { if let ErrorKind::TypeError(e) = &mut e.error_mut() { (e.1).0.push(path());