Skip to content

Commit

Permalink
chore: address clippy lints
Browse files Browse the repository at this point in the history
  • Loading branch information
martinohmann committed Dec 27, 2024
1 parent 70910ac commit 1ffd789
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 37 deletions.
1 change: 1 addition & 0 deletions crates/hcl-edit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
clippy::module_name_repetitions,
clippy::must_use_candidate,
clippy::naive_bytecount,
clippy::needless_lifetimes,
clippy::return_self_not_must_use
)]

Expand Down
11 changes: 11 additions & 0 deletions crates/hcl-edit/src/structure/mod.rs
Original file line number Diff line number Diff line change
@@ -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<Attribute, Structure>` and `Result<Block, Structure>`.
//
// 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;
Expand Down
3 changes: 2 additions & 1 deletion crates/hcl-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
69 changes: 33 additions & 36 deletions crates/hcl-rs/src/eval/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,46 +445,43 @@ impl Evaluate for ForExpr {
fn evaluate(&self, ctx: &Context) -> EvalResult<Self::Output> {
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::<EvalResult<_>>()?;

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::<EvalResult<_>>()?;

Ok(Value::Array(result))
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/hcl-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 1ffd789

Please sign in to comment.