From 1ffd789cd109998c0761cb9eaf0ff0ee5c8fd1e3 Mon Sep 17 00:00:00 2001 From: martinohmann Date: Wed, 6 Nov 2024 08:58:36 +0100 Subject: [PATCH] chore: address clippy lints --- crates/hcl-edit/src/lib.rs | 1 + crates/hcl-edit/src/structure/mod.rs | 11 +++++ crates/hcl-primitives/src/lib.rs | 3 +- crates/hcl-rs/src/eval/impls.rs | 69 +++++++++++++--------------- crates/hcl-rs/src/lib.rs | 1 + 5 files changed, 48 insertions(+), 37 deletions(-) diff --git a/crates/hcl-edit/src/lib.rs b/crates/hcl-edit/src/lib.rs index 26388810..b223eae2 100644 --- a/crates/hcl-edit/src/lib.rs +++ b/crates/hcl-edit/src/lib.rs @@ -8,6 +8,7 @@ clippy::module_name_repetitions, clippy::must_use_candidate, clippy::naive_bytecount, + clippy::needless_lifetimes, clippy::return_self_not_must_use )] diff --git a/crates/hcl-edit/src/structure/mod.rs b/crates/hcl-edit/src/structure/mod.rs index 8ece068e..858cbbee 100644 --- a/crates/hcl-edit/src/structure/mod.rs +++ b/crates/hcl-edit/src/structure/mod.rs @@ -1,5 +1,16 @@ //! Types to represent the HCL structural sub-language. +// `Attribute` and `Block` have the same size, yet clippy complains about large size difference +// between the enum variants of `Structure` because it determines the size of `Attribute` to be +// zero. As pointed out in the issue below, clippy seems to choke on recursive types, which is the +// case for the `Expression` type used as attribute value. +// +// The incorrect size calculation also produces false positives for result types like +// `Result` and `Result`. +// +// See https://github.com/rust-lang/rust-clippy/issues/9798 +#![allow(clippy::large_enum_variant, clippy::result_large_err)] + mod attribute; mod block; mod body; diff --git a/crates/hcl-primitives/src/lib.rs b/crates/hcl-primitives/src/lib.rs index 94caebf1..1091a0d4 100644 --- a/crates/hcl-primitives/src/lib.rs +++ b/crates/hcl-primitives/src/lib.rs @@ -5,7 +5,8 @@ #![allow( clippy::must_use_candidate, clippy::module_name_repetitions, - clippy::missing_panics_doc + clippy::missing_panics_doc, + clippy::needless_lifetimes )] #![cfg_attr(not(feature = "std"), no_std)] #![forbid(unsafe_code)] diff --git a/crates/hcl-rs/src/eval/impls.rs b/crates/hcl-rs/src/eval/impls.rs index 35bb6132..6bfe8da9 100644 --- a/crates/hcl-rs/src/eval/impls.rs +++ b/crates/hcl-rs/src/eval/impls.rs @@ -445,46 +445,43 @@ impl Evaluate for ForExpr { fn evaluate(&self, ctx: &Context) -> EvalResult { let collection = expr::Collection::from_for_expr(self, ctx)?; - match &self.key_expr { - Some(key_expr) => { - // Result will be an object. - let mut result = Map::with_capacity(collection.len()); - - for ctx in collection { - let ctx = &ctx?; - let key = expr::evaluate_object_key(key_expr, ctx)?; - let value = self.value_expr.evaluate(ctx)?; - - if self.grouping { - result - .entry(key) - .or_insert_with(|| Value::Array(Vec::new())) - .as_array_mut() - .unwrap() - .push(value); - } else { - match result.entry(key) { - Entry::Occupied(entry) => { - return Err(ctx.error(ErrorKind::KeyExists(entry.key().clone()))) - } - Entry::Vacant(entry) => { - entry.insert(value); - } + if let Some(key_expr) = &self.key_expr { + // Result will be an object. + let mut result = Map::with_capacity(collection.len()); + + for ctx in collection { + let ctx = &ctx?; + let key = expr::evaluate_object_key(key_expr, ctx)?; + let value = self.value_expr.evaluate(ctx)?; + + if self.grouping { + result + .entry(key) + .or_insert_with(|| Value::Array(Vec::new())) + .as_array_mut() + .unwrap() + .push(value); + } else { + match result.entry(key) { + Entry::Occupied(entry) => { + return Err(ctx.error(ErrorKind::KeyExists(entry.key().clone()))) + } + Entry::Vacant(entry) => { + entry.insert(value); } } } - - Ok(Value::Object(result)) - } - None => { - // Result will be an array. - let result = collection - .into_iter() - .map(|ctx| self.value_expr.evaluate(&ctx?)) - .collect::>()?; - - Ok(Value::Array(result)) } + + Ok(Value::Object(result)) + } else { + // Result will be an array. + let result = collection + .into_iter() + .map(|ctx| self.value_expr.evaluate(&ctx?)) + .collect::>()?; + + Ok(Value::Array(result)) } } diff --git a/crates/hcl-rs/src/lib.rs b/crates/hcl-rs/src/lib.rs index 0b050d4a..06a602c2 100644 --- a/crates/hcl-rs/src/lib.rs +++ b/crates/hcl-rs/src/lib.rs @@ -14,6 +14,7 @@ clippy::missing_panics_doc, clippy::module_name_repetitions, clippy::must_use_candidate, + clippy::needless_lifetimes, clippy::needless_pass_by_value, clippy::option_option, clippy::return_self_not_must_use,