--- a/bindings/jsonnet/src/lib.rs +++ b/bindings/jsonnet/src/lib.rs @@ -70,6 +70,7 @@ /// Creates a new Jsonnet virtual machine. #[no_mangle] +#[allow(clippy::box_default)] pub extern "C" fn jsonnet_make() -> *mut State { let state = State::default(); state.settings_mut().import_resolver = Box::new(FileImportResolver::default()); --- a/crates/jrsonnet-cli/src/stdlib.rs +++ b/crates/jrsonnet-cli/src/stdlib.rs @@ -44,7 +44,7 @@ if out.len() != 2 { return Err("bad ext-file syntax".to_owned()); } - let file = read_to_string(&out[1]); + let file = read_to_string(out[1]); match file { Ok(content) => Ok(Self { name: out[0].into(), --- a/crates/jrsonnet-evaluator/src/error.rs +++ b/crates/jrsonnet-evaluator/src/error.rs @@ -100,7 +100,7 @@ #[error("duplicate local var: {0}")] DuplicateLocalVar(IStr), - #[error("type mismatch: expected {}, got {2} {0}", .1.iter().map(|e| format!("{}", e)).collect::>().join(", "))] + #[error("type mismatch: expected {}, got {2} {0}", .1.iter().map(|e| format!("{e}")).collect::>().join(", "))] TypeMismatch(&'static str, Vec, ValType), #[error("no such field: {}{}", format_empty_str(.0), format_found(.1, "field"))] NoSuchField(IStr, Vec), @@ -113,7 +113,7 @@ BindingParameterASecondTime(IStr), #[error("too many args, function has {0}{}", format_signature(.1))] TooManyArgsFunctionHas(usize, FunctionSignature), - #[error("function argument is not passed: {}{}", .0.as_ref().map(|n| n.as_str()).unwrap_or(""), format_signature(.1))] + #[error("function argument is not passed: {}{}", .0.as_ref().map_or("", IStr::as_str), format_signature(.1))] FunctionParameterNotBoundInCall(Option, FunctionSignature), #[error("external variable is not defined: {0}")] @@ -249,7 +249,7 @@ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { writeln!(f, "{}", self.0 .0)?; for el in &self.0 .1 .0 { - writeln!(f, "\t{:?}", el)?; + writeln!(f, "\t{el:?}")?; } Ok(()) } --- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs +++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs @@ -436,7 +436,7 @@ UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(s, ctx, v)?)?, Var(name) => s.push( CallLocation::new(loc), - || format!("variable <{}> access", name), + || format!("variable <{name}> access"), || ctx.binding(name.clone())?.evaluate(s.clone()), )?, Index(value, index) => { @@ -446,7 +446,7 @@ ) { (Val::Obj(v), Val::Str(key)) => s.push( CallLocation::new(loc), - || format!("field <{}> access", key), + || format!("field <{key}> access"), || match v.get(s.clone(), key.clone()) { Ok(Some(v)) => Ok(v), #[cfg(not(feature = "friendly-errors"))] @@ -611,7 +611,7 @@ if let Some(value) = expr { Ok(Some(s.push( loc, - || format!("slice {}", desc), + || format!("slice {desc}"), || T::from_untyped(evaluate(s.clone(), ctx.clone(), value)?, s.clone()), )?)) } else { --- a/crates/jrsonnet-evaluator/src/evaluate/operator.rs +++ b/crates/jrsonnet-evaluator/src/evaluate/operator.rs @@ -30,8 +30,8 @@ (Str(a), Num(b)) => Str(format!("{a}{b}").into()), (Str(a), o) | (o, Str(a)) if a.is_empty() => Val::Str(o.clone().to_string(s)?), - (Str(a), o) => Str(format!("{}{}", a, o.clone().to_string(s)?).into()), - (o, Str(a)) => Str(format!("{}{}", o.clone().to_string(s)?, a).into()), + (Str(a), o) => Str(format!("{a}{}", o.clone().to_string(s)?).into()), + (o, Str(a)) => Str(format!("{}{a}", o.clone().to_string(s)?).into()), (Obj(v1), Obj(v2)) => Obj(v2.extend_from(v1.clone())), (Arr(a), Arr(b)) => { --- a/crates/jrsonnet-evaluator/src/function/arglike.rs +++ b/crates/jrsonnet-evaluator/src/function/arglike.rs @@ -108,7 +108,7 @@ handler: &mut dyn FnMut(usize, Thunk) -> Result<()>, ) -> Result<()> { for (idx, el) in self.iter().enumerate() { - handler(idx, Thunk::evaluated(el.clone()))? + handler(idx, Thunk::evaluated(el.clone()))?; } Ok(()) } --- a/crates/jrsonnet-evaluator/src/function/parse.rs +++ b/crates/jrsonnet-evaluator/src/function/parse.rs @@ -179,12 +179,7 @@ // FIXME: O(n) for arg existence check let id = params .iter() - .position(|p| { - p.name - .as_ref() - .map(|v| &v as &str == name as &str) - .unwrap_or(false) - }) + .position(|p| p.name.as_ref().map_or(false, |v| v as &str == name as &str)) .ok_or_else(|| UnknownFunctionParameter((name as &str).to_owned()))?; if replace(&mut passed_args[id], Some(arg)).is_some() { throw!(BindingParameterASecondTime(name.clone())); @@ -209,8 +204,7 @@ if param .name .as_ref() - .map(|v| &v as &str == name as &str) - .unwrap_or(false) + .map_or(false, |v| v as &str == name as &str) { found = true; } --- a/crates/jrsonnet-evaluator/src/import.rs +++ b/crates/jrsonnet-evaluator/src/import.rs @@ -123,15 +123,11 @@ }; if meta.is_file() { Ok(SourcePath::new(SourceFile::new( - path.canonicalize() - .map_err(|e| ImportIo(e.to_string()))? - .to_owned(), + path.canonicalize().map_err(|e| ImportIo(e.to_string()))?, ))) } else if meta.is_dir() { Ok(SourcePath::new(SourceDirectory::new( - path.canonicalize() - .map_err(|e| ImportIo(e.to_string()))? - .to_owned(), + path.canonicalize().map_err(|e| ImportIo(e.to_string()))?, ))) } else { unreachable!("this can't be a symlink") --- a/crates/jrsonnet-evaluator/src/integrations/serde.rs +++ b/crates/jrsonnet-evaluator/src/integrations/serde.rs @@ -16,7 +16,7 @@ Self::Null => Val::Null, Self::Bool(v) => Val::Bool(v), Self::Number(n) => Val::Num(n.as_f64().ok_or_else(|| { - RuntimeError(format!("json number can't be represented as jsonnet: {}", n).into()) + RuntimeError(format!("json number can't be represented as jsonnet: {n}").into()) })?), Self::String(s) => Val::Str((&s as &str).into()), Self::Array(a) => { --- a/crates/jrsonnet-evaluator/src/lib.rs +++ b/crates/jrsonnet-evaluator/src/lib.rs @@ -594,7 +594,7 @@ .insert(name, TlaArg::String(value)); } pub fn add_tla_code(&self, name: IStr, code: &str) -> Result<()> { - let source_name = format!("", name); + let source_name = format!(""); let source = Source::new_virtual(source_name.into(), code.into()); let parsed = jrsonnet_parser::parse( code, --- a/crates/jrsonnet-evaluator/src/obj.rs +++ b/crates/jrsonnet-evaluator/src/obj.rs @@ -156,9 +156,9 @@ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { if let Some(super_obj) = self.0.sup.as_ref() { if f.alternate() { - write!(f, "{:#?}", super_obj)?; + write!(f, "{super_obj:#?}")?; } else { - write!(f, "{:?}", super_obj)?; + write!(f, "{super_obj:?}")?; } write!(f, " + ")?; } @@ -395,10 +395,9 @@ })?; self.0.value_cache.borrow_mut().insert( key, - match &value { - Some(v) => CacheValue::Cached(v.clone()), - None => CacheValue::NotFound, - }, + value + .as_ref() + .map_or(CacheValue::NotFound, |v| CacheValue::Cached(v.clone())), ); Ok(value) } --- a/crates/jrsonnet-evaluator/src/stdlib/format.rs +++ b/crates/jrsonnet-evaluator/src/stdlib/format.rs @@ -45,7 +45,7 @@ let mut i = 1; while i < bytes.len() { if bytes[i] == b')' { - return Ok((&str[1..i as usize], &str[i as usize + 1..])); + return Ok((&str[1..i], &str[i + 1..])); } i += 1; } @@ -310,6 +310,7 @@ nums }; let neg = iv < 0.0; + #[allow(clippy::bool_to_int_with_if)] let zp = padding.saturating_sub(if neg || blank || sign { 1 } else { 0 }); let zp2 = zp .max(precision) @@ -406,6 +407,7 @@ ensure_pt: bool, trailing: bool, ) { + #[allow(clippy::bool_to_int_with_if)] let dot_size = if precision == 0 && !ensure_pt { 0 } else { 1 }; padding = padding.saturating_sub(dot_size + precision); render_decimal(out, n.floor(), padding, 0, blank, sign); @@ -478,10 +480,7 @@ precision: Option, ) -> Result<()> { let clfags = &code.cflags; - let (fpprec, iprec) = match precision { - Some(v) => (v, v), - None => (6, 0), - }; + let (fpprec, iprec) = precision.map_or((6, 0), |v| (v, v)); let padding = if clfags.zero && !clfags.left { width } else { @@ -586,8 +585,10 @@ } } ConvTypeV::Char => match value.clone() { - Val::Num(n) => tmp_out - .push(std::char::from_u32(n as u32).ok_or(InvalidUnicodeCodepointGot(n as u32))?), + Val::Num(n) => tmp_out.push( + std::char::from_u32(n as u32) + .ok_or_else(|| InvalidUnicodeCodepointGot(n as u32))?, + ), Val::Str(s) => { if s.chars().count() != 1 { throw!(RuntimeError( --- a/crates/jrsonnet-evaluator/src/stdlib/manifest.rs +++ b/crates/jrsonnet-evaluator/src/stdlib/manifest.rs @@ -49,7 +49,7 @@ } Val::Null => buf.push_str("null"), Val::Str(s) => escape_string_json_buf(s, buf), - Val::Num(n) => write!(buf, "{}", n).unwrap(), + Val::Num(n) => write!(buf, "{n}").unwrap(), Val::Arr(items) => { buf.push('['); if !items.is_empty() { --- a/crates/jrsonnet-evaluator/src/stdlib/mod.rs +++ b/crates/jrsonnet-evaluator/src/stdlib/mod.rs @@ -12,7 +12,7 @@ pub fn std_format(s: State, str: IStr, vals: Val) -> Result { s.push( CallLocation::native(), - || format!("std.format of {}", str), + || format!("std.format of {str}"), || { Ok(match vals { Val::Arr(vals) => format_arr(s.clone(), &str, &vals.evaluated(s.clone())?)?, --- a/crates/jrsonnet-evaluator/src/trace/mod.rs +++ b/crates/jrsonnet-evaluator/src/trace/mod.rs @@ -16,12 +16,9 @@ } impl PathResolver { - /// Will return Self::Relative(cwd), or Self::Absolute on cwd failure + /// Will return `Self::Relative(cwd)`, or `Self::Absolute` on cwd failure pub fn new_cwd_fallback() -> Self { - match std::env::current_dir() { - Ok(v) => Self::Relative(v), - Err(_) => Self::Absolute, - } + std::env::current_dir().map_or(Self::Absolute, Self::Relative) } pub fn resolve(&self, from: &Path) -> String { match self { @@ -97,10 +94,10 @@ use std::fmt::Write; writeln!(out)?; - let mut n = match path.source_path().path() { - Some(r) => self.resolver.resolve(r), - None => path.source_path().to_string(), - }; + let mut n = path.source_path().path().map_or_else( + || path.source_path().to_string(), + |r| self.resolver.resolve(r), + ); let mut offset = error.location.offset; let is_eof = if offset >= path.code().len() { offset = path.code().len().saturating_sub(1); @@ -119,7 +116,7 @@ write!(n, ":").unwrap(); print_code_location(&mut n, &location, &location).unwrap(); - write!(out, "{: r.display().to_string(), - None => source.0.source_path().to_string(), - }; + let resolved_path = source.0.source_path().path().map_or_else( + || source.0.source_path().to_string(), + |r| r.display().to_string(), + ); write!( out, @@ -196,7 +193,7 @@ desc, resolved_path, start_end[0].line, start_end[0].column, )?; } else { - write!(out, " during {}", desc)?; + write!(out, " during {desc}")?; } } Ok(()) @@ -252,7 +249,7 @@ desc, )?; } else { - write!(out, "{}", desc)?; + write!(out, "{desc}")?; } } Ok(()) @@ -280,10 +277,10 @@ .take(end.line_end_offset - end.line_start_offset) .collect(); - let origin = match origin.source_path().path() { - Some(r) => self.resolver.resolve(r), - None => origin.source_path().to_string(), - }; + let origin = origin.source_path().path().map_or_else( + || origin.source_path().to_string(), + |r| self.resolver.resolve(r), + ); let snippet = Snippet { opt: FormatOptions { color: true, @@ -308,7 +305,7 @@ }; let dl = DisplayList::from(snippet); - write!(out, "{}", dl)?; + write!(out, "{dl}")?; Ok(()) } --- a/crates/jrsonnet-evaluator/src/typed/mod.rs +++ b/crates/jrsonnet-evaluator/src/typed/mod.rs @@ -21,8 +21,8 @@ UnionFailed(ComplexValType, TypeLocErrorList), #[error( "number out of bounds: {0} not in {}..{}", - .1.map(|v|v.to_string()).unwrap_or_else(|| "".to_owned()), - .2.map(|v|v.to_string()).unwrap_or_else(|| "".to_owned()), + .1.map(|v|v.to_string()).unwrap_or_default(), + .2.map(|v|v.to_string()).unwrap_or_default(), )] BoundsFailed(f64, Option, Option), } @@ -65,7 +65,7 @@ writeln!(f)?; } out.clear(); - write!(out, "{}", err)?; + write!(out, "{err}")?; for (i, line) in out.lines().enumerate() { if line.trim().is_empty() { @@ -77,7 +77,7 @@ writeln!(f)?; write!(f, " ")?; } - write!(f, "{}", line)?; + write!(f, "{line}")?; } } Ok(()) @@ -125,8 +125,8 @@ impl Display for ValuePathItem { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - Self::Field(name) => write!(f, ".{:?}", name)?, - Self::Index(idx) => write!(f, "[{}]", idx)?, + Self::Field(name) => write!(f, ".{name:?}")?, + Self::Index(idx) => write!(f, "[{idx}]")?, } Ok(()) } @@ -138,7 +138,7 @@ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "self")?; for elem in self.0.iter().rev() { - write!(f, "{}", elem)?; + write!(f, "{elem}")?; } Ok(()) } @@ -171,7 +171,7 @@ for (i, item) in a.iter(s.clone()).enumerate() { push_type_description( s.clone(), - || format!("array index {}", i), + || format!("array index {i}"), || ValuePathItem::Index(i as u64), || elem_type.check(s.clone(), &item.clone()?), )?; @@ -185,7 +185,7 @@ for (i, item) in a.iter(s.clone()).enumerate() { push_type_description( s.clone(), - || format!("array index {}", i), + || format!("array index {i}"), || ValuePathItem::Index(i as u64), || elem_type.check(s.clone(), &item.clone()?), )?; @@ -200,7 +200,7 @@ if let Some(got_v) = obj.get(s.clone(), (*k).into())? { push_type_description( s.clone(), - || format!("property {}", k), + || format!("property {k}"), || ValuePathItem::Field((*k).into()), || v.check(s.clone(), &got_v), )?; --- a/crates/jrsonnet-evaluator/src/val.rs +++ b/crates/jrsonnet-evaluator/src/val.rs @@ -292,7 +292,7 @@ if index >= v.to() { return Ok(None); } - v.inner.get(s, index as usize) + v.inner.get(s, index) } } } @@ -332,7 +332,7 @@ if index >= s.to() { return None; } - s.inner.get_lazy(index as usize) + s.inner.get_lazy(index) } } } @@ -531,8 +531,9 @@ } } -#[cfg(target_pointer_width = "64")] -static_assertions::assert_eq_size!(Val, [u8; 32]); +// Broken between stable and nightly, as there is new layout size optimization +// #[cfg(target_pointer_width = "64")] +// static_assertions::assert_eq_size!(Val, [u8; 24]); impl Val { pub const fn as_bool(&self) -> Option { --- a/crates/jrsonnet-stdlib/src/encoding.rs +++ b/crates/jrsonnet-stdlib/src/encoding.rs @@ -28,7 +28,7 @@ #[builtin] pub fn builtin_base64_decode_bytes(input: IStr) -> Result { - Ok(base64::decode(&input.as_bytes()) + Ok(base64::decode(input.as_bytes()) .map_err(|_| RuntimeError("bad base64".into()))? .as_slice() .into()) @@ -36,6 +36,6 @@ #[builtin] pub fn builtin_base64_decode(input: IStr) -> Result { - let bytes = base64::decode(&input.as_bytes()).map_err(|_| RuntimeError("bad base64".into()))?; + let bytes = base64::decode(input.as_bytes()).map_err(|_| RuntimeError("bad base64".into()))?; Ok(String::from_utf8(bytes).map_err(|_| RuntimeError("bad utf8".into()))?) } --- a/crates/jrsonnet-stdlib/src/hash.rs +++ b/crates/jrsonnet-stdlib/src/hash.rs @@ -2,5 +2,5 @@ #[builtin] pub fn builtin_md5(str: IStr) -> Result { - Ok(format!("{:x}", md5::compute(&str.as_bytes()))) + Ok(format!("{:x}", md5::compute(str.as_bytes()))) } --- a/crates/jrsonnet-stdlib/src/lib.rs +++ b/crates/jrsonnet-stdlib/src/lib.rs @@ -366,7 +366,7 @@ #[builtin] fn builtin_substr(str: IStr, from: usize, len: usize) -> Result { - Ok(str.chars().skip(from as usize).take(len as usize).collect()) + Ok(str.chars().skip(from).take(len).collect()) } #[builtin(fields( @@ -380,7 +380,7 @@ .ext_vars .get(&x) .cloned() - .ok_or(UndefinedExternalVariable(x))? + .ok_or_else(|| UndefinedExternalVariable(x))? .evaluate_arg(s.clone(), ctx, true)? .evaluate(s)?)) } @@ -402,7 +402,7 @@ #[builtin] fn builtin_char(n: u32) -> Result { - Ok(std::char::from_u32(n as u32).ok_or(InvalidUnicodeCodepointGot(n as u32))?) + Ok(std::char::from_u32(n).ok_or_else(|| InvalidUnicodeCodepointGot(n))?) } #[builtin(fields(