git.delta.rocks / jrsonnet / refs/commits / 4f2e8db91a47

difftreelog

style fix clippy warnings

Yaroslav Bolyukin2024-05-17parent: #3048d02.patch.diff
in: master

8 files changed

modifiedcrates/jrsonnet-evaluator/src/arr/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/arr/mod.rs
+++ b/crates/jrsonnet-evaluator/src/arr/mod.rs
@@ -1,7 +1,4 @@
-use std::{
-	any::Any,
-	num::{NonZeroU32, NonZeroUsize},
-};
+use std::{any::Any, num::NonZeroU32};
 
 use jrsonnet_gcmodule::{Cc, Trace};
 use jrsonnet_interner::IBytes;
modifiedcrates/jrsonnet-evaluator/src/evaluate/destructure.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/evaluate/destructure.rs
1use jrsonnet_gcmodule::Trace;2use jrsonnet_interner::IStr;3use jrsonnet_parser::{BindSpec, Destruct, LocExpr, ParamsDesc};45use crate::{6	bail,7	error::{ErrorKind::*, Result},8	evaluate, evaluate_method, evaluate_named,9	gc::GcHashMap,10	val::ThunkValue,11	Context, Pending, Thunk, Val,12};1314#[allow(clippy::too_many_lines)]15#[allow(unused_variables)]16pub fn destruct(17	d: &Destruct,18	parent: Thunk<Val>,19	fctx: Pending<Context>,20	new_bindings: &mut GcHashMap<IStr, Thunk<Val>>,21) -> Result<()> {22	match d {23		Destruct::Full(v) => {24			let old = new_bindings.insert(v.clone(), parent);25			if old.is_some() {26				bail!(DuplicateLocalVar(v.clone()))27			}28		}29		#[cfg(feature = "exp-destruct")]30		Destruct::Skip => {}31		#[cfg(feature = "exp-destruct")]32		Destruct::Array { start, rest, end } => {33			use jrsonnet_parser::DestructRest;3435			use crate::arr::ArrValue;3637			#[derive(Trace)]38			struct DataThunk {39				parent: Thunk<Val>,40				min_len: usize,41				has_rest: bool,42			}43			impl ThunkValue for DataThunk {44				type Output = ArrValue;4546				fn get(self: Box<Self>) -> Result<Self::Output> {47					let v = self.parent.evaluate()?;48					let Val::Arr(arr) = v else {49						bail!("expected array");50					};51					if !self.has_rest {52						if arr.len() != self.min_len {53							bail!("expected {} elements, got {}", self.min_len, arr.len())54						}55					} else if arr.len() < self.min_len {56						bail!(57							"expected at least {} elements, but array was only {}",58							self.min_len,59							arr.len()60						)61					}62					Ok(arr)63				}64			}6566			let full = Thunk::new(DataThunk {67				min_len: start.len() + end.len(),68				has_rest: rest.is_some(),69				parent,70			});7172			{73				#[derive(Trace)]74				struct BaseThunk {75					full: Thunk<ArrValue>,76					index: usize,77				}78				impl ThunkValue for BaseThunk {79					type Output = Val;8081					fn get(self: Box<Self>) -> Result<Self::Output> {82						let full = self.full.evaluate()?;83						Ok(full.get(self.index)?.expect("length is checked"))84					}85				}86				for (i, d) in start.iter().enumerate() {87					destruct(88						d,89						Thunk::new(BaseThunk {90							full: full.clone(),91							index: i,92						}),93						fctx.clone(),94						new_bindings,95					)?;96				}97			}9899			match rest {100				Some(DestructRest::Keep(v)) => {101					#[derive(Trace)]102					struct RestThunk {103						full: Thunk<ArrValue>,104						start: usize,105						end: usize,106					}107					impl ThunkValue for RestThunk {108						type Output = Val;109110						fn get(self: Box<Self>) -> Result<Self::Output> {111							let full = self.full.evaluate()?;112							let to = full.len() - self.end;113							Ok(Val::Arr(full.slice(Some(self.start as i32), Some(to as i32), None)))114						}115					}116117					destruct(118						&Destruct::Full(v.clone()),119						Thunk::new(RestThunk {120							full: full.clone(),121							start: start.len(),122							end: end.len(),123						}),124						fctx.clone(),125						new_bindings,126					)?;127				}128				Some(DestructRest::Drop) | None => {}129			}130131			{132				#[derive(Trace)]133				struct EndThunk {134					full: Thunk<ArrValue>,135					index: usize,136					end: usize,137				}138				impl ThunkValue for EndThunk {139					type Output = Val;140141					fn get(self: Box<Self>) -> Result<Self::Output> {142						let full = self.full.evaluate()?;143						Ok(full144							.get(full.len() - self.end + self.index)?145							.expect("length is checked"))146					}147				}148				for (i, d) in end.iter().enumerate() {149					destruct(150						d,151						Thunk::new(EndThunk {152							full: full.clone(),153							index: i,154							end: end.len(),155						}),156						fctx.clone(),157						new_bindings,158					)?;159				}160			}161		}162		#[cfg(feature = "exp-destruct")]163		Destruct::Object { fields, rest } => {164			use crate::obj::ObjValue;165166			#[derive(Trace)]167			struct DataThunk {168				parent: Thunk<Val>,169				field_names: Vec<IStr>,170				has_rest: bool,171			}172			impl ThunkValue for DataThunk {173				type Output = ObjValue;174175				fn get(self: Box<Self>) -> Result<Self::Output> {176					let v = self.parent.evaluate()?;177					let Val::Obj(obj) = v else {178						bail!("expected object");179					};180					for field in &self.field_names {181						if !obj.has_field_ex(field.clone(), true) {182							bail!("missing field: {field}");183						}184					}185					if !self.has_rest {186						let len = obj.len();187						if len != self.field_names.len() {188							bail!("too many fields, and rest not found");189						}190					}191					Ok(obj)192				}193			}194			let field_names: Vec<_> = fields195				.iter()196				.filter(|f| f.2.is_none())197				.map(|f| f.0.clone())198				.collect();199			let full = Thunk::new(DataThunk {200				parent,201				field_names,202				has_rest: rest.is_some(),203			});204205			for (field, d, default) in fields {206				#[derive(Trace)]207				struct FieldThunk {208					full: Thunk<ObjValue>,209					field: IStr,210					default: Option<(Pending<Context>, LocExpr)>,211				}212				impl ThunkValue for FieldThunk {213					type Output = Val;214215					fn get(self: Box<Self>) -> Result<Self::Output> {216						let full = self.full.evaluate()?;217						if let Some(field) = full.get(self.field)? {218							Ok(field)219						} else {220							let (fctx, expr) = self.default.as_ref().expect("shape is checked");221							Ok(evaluate(fctx.clone().unwrap(), expr)?)222						}223					}224				}225				let value = Thunk::new(FieldThunk {226					full: full.clone(),227					field: field.clone(),228					default: default.clone().map(|e| (fctx.clone(), e)),229				});230				if let Some(d) = d {231					destruct(d, value, fctx.clone(), new_bindings)?;232				} else {233					destruct(234						&Destruct::Full(field.clone()),235						value,236						fctx.clone(),237						new_bindings,238					)?;239				}240			}241		}242	}243	Ok(())244}245246pub fn evaluate_dest(247	d: &BindSpec,248	fctx: Pending<Context>,249	new_bindings: &mut GcHashMap<IStr, Thunk<Val>>,250) -> Result<()> {251	match d {252		BindSpec::Field { into, value } => {253			#[derive(Trace)]254			struct EvaluateThunkValue {255				name: Option<IStr>,256				fctx: Pending<Context>,257				expr: LocExpr,258			}259			impl ThunkValue for EvaluateThunkValue {260				type Output = Val;261				fn get(self: Box<Self>) -> Result<Self::Output> {262					self.name.map_or_else(263						|| evaluate(self.fctx.unwrap(), &self.expr),264						|name| evaluate_named(self.fctx.unwrap(), &self.expr, name),265					)266				}267			}268			let data = Thunk::new(EvaluateThunkValue {269				name: into.name(),270				fctx: fctx.clone(),271				expr: value.clone(),272			});273			destruct(into, data, fctx, new_bindings)?;274		}275		BindSpec::Function {276			name,277			params,278			value,279		} => {280			#[derive(Trace)]281			struct MethodThunk {282				fctx: Pending<Context>,283				name: IStr,284				params: ParamsDesc,285				value: LocExpr,286			}287			impl ThunkValue for MethodThunk {288				type Output = Val;289290				fn get(self: Box<Self>) -> Result<Self::Output> {291					Ok(evaluate_method(292						self.fctx.unwrap(),293						self.name,294						self.params,295						self.value,296					))297				}298			}299300			let old = new_bindings.insert(301				name.clone(),302				Thunk::new(MethodThunk {303					fctx,304					name: name.clone(),305					params: params.clone(),306					value: value.clone(),307				}),308			);309			if old.is_some() {310				bail!(DuplicateLocalVar(name.clone()))311			}312		}313	}314	Ok(())315}
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -2,7 +2,7 @@
 	cell::RefCell,
 	fmt::{self, Debug, Display},
 	mem::replace,
-	num::{NonZeroU32, NonZeroUsize},
+	num::NonZeroU32,
 	rc::Rc,
 };
 
modifiedcrates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-macros/src/lib.rs
+++ b/crates/jrsonnet-macros/src/lib.rs
@@ -218,8 +218,7 @@
 	item: proc_macro::TokenStream,
 ) -> proc_macro::TokenStream {
 	let attr = parse_macro_input!(attr as BuiltinAttrs);
-	let item_fn = item.clone();
-	let item_fn: ItemFn = parse_macro_input!(item_fn);
+	let item_fn = parse_macro_input!(item as ItemFn);
 
 	match builtin_inner(attr, item_fn) {
 		Ok(v) => v.into(),
modifiedcrates/jrsonnet-stdlib/src/manifest/toml.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/manifest/toml.rs
+++ b/crates/jrsonnet-stdlib/src/manifest/toml.rs
@@ -4,7 +4,7 @@
 	bail,
 	manifest::{escape_string_json_buf, ManifestFormat},
 	val::ArrValue,
-	IStr, ObjValue, Result, ResultExt, Val, State,
+	IStr, ObjValue, Result, ResultExt, State, Val,
 };
 
 pub struct TomlFormat<'s> {
modifiedcrates/jrsonnet-stdlib/src/manifest/xml.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/manifest/xml.rs
+++ b/crates/jrsonnet-stdlib/src/manifest/xml.rs
@@ -1,9 +1,9 @@
 use jrsonnet_evaluator::{
 	bail,
 	manifest::{ManifestFormat, ToStringFormat},
-	typed::{ComplexValType, Either2, Either4, Typed, ValType},
-	val::{ArrValue, IndexableVal},
-	Either, ObjValue, Result, ResultExt, Val, State,
+	typed::{ComplexValType, Either2, Typed, ValType},
+	val::ArrValue,
+	Either, ObjValue, Result, ResultExt, State, Val,
 };
 
 pub struct XmlJsonmlFormat {
@@ -39,20 +39,20 @@
 
 	fn from_untyped(untyped: Val) -> Result<Self> {
 		let val = <Either![ArrValue, String]>::from_untyped(untyped)
-			.with_description(|| format!("parsing JSONML value (an array or string)"))?;
+			.description("parsing JSONML value (an array or string)")?;
 		let arr = match val {
 			Either2::A(a) => a,
 			Either2::B(s) => return Ok(Self::String(s)),
 		};
-		if arr.len() < 1 {
+		if arr.is_empty() {
 			bail!("JSONML value should have tag (array length should be >=1)");
 		};
 		let tag = String::from_untyped(
 			arr.get(0)
-				.with_description(|| "getting JSONML tag")?
+				.description("getting JSONML tag")?
 				.expect("length checked"),
 		)
-		.with_description(|| format!("parsing JSONML tag"))?;
+		.description("parsing JSONML tag")?;
 
 		let (has_attrs, attrs) = if arr.len() >= 2 {
 			let maybe_attrs = arr
@@ -71,7 +71,7 @@
 			tag,
 			attrs,
 			children: State::push_description(
-				|| format!("parsing children"),
+				|| "parsing children".to_owned(),
 				|| {
 					Typed::from_untyped(Val::Arr(arr.slice(
 						Some(if has_attrs { 2 } else { 1 }),
@@ -100,7 +100,7 @@
 		} => {
 			let has_children = !children.is_empty();
 			buf.push('<');
-			buf.push_str(&tag);
+			buf.push_str(tag);
 			attrs.run_assertions()?;
 			for (key, value) in attrs.iter(
 				// Not much sense to preserve order here
@@ -125,12 +125,12 @@
 			}
 			buf.push('>');
 			for child in children {
-				manifest_jsonml(&child, buf, opts)?;
+				manifest_jsonml(child, buf, opts)?;
 			}
 			if has_children || opts.force_closing {
 				buf.push('<');
 				buf.push('/');
-				buf.push_str(&tag);
+				buf.push_str(tag);
 				buf.push('>');
 			}
 			Ok(())
@@ -177,8 +177,8 @@
 	}
 	if !found {
 		// No match - no escapes required
-		out.push_str(&str);
+		out.push_str(str);
 		return;
 	}
-	out.push_str(&remaining);
+	out.push_str(remaining);
 }
modifiedcrates/jrsonnet-stdlib/src/misc.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/misc.rs
+++ b/crates/jrsonnet-stdlib/src/misc.rs
@@ -28,8 +28,7 @@
 	o: ObjValue,
 	f: IStr,
 	default: Option<Thunk<Val>>,
-	#[default(true)]
-	inc_hidden: bool,
+	#[default(true)] inc_hidden: bool,
 ) -> Result<Val> {
 	let do_default = move || {
 		let Some(default) = default else {
modifiedcrates/jrsonnet-stdlib/src/objects.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/objects.rs
+++ b/crates/jrsonnet-stdlib/src/objects.rs
@@ -57,8 +57,7 @@
 	o: ObjValue,
 	include_hidden: bool,
 
-	#[cfg(feature = "exp-preserve-order")]
-	preserve_order: bool,
+	#[cfg(feature = "exp-preserve-order")] preserve_order: bool,
 ) -> ArrValue {
 	o.values_ex(
 		include_hidden,
@@ -101,8 +100,7 @@
 	o: ObjValue,
 	include_hidden: bool,
 
-	#[cfg(feature = "exp-preserve-order")]
-	preserve_order: bool,
+	#[cfg(feature = "exp-preserve-order")] preserve_order: bool,
 ) -> ArrValue {
 	o.key_values_ex(
 		include_hidden,