Skip to content

Commit

Permalink
Apply clippy auto-fixes (#1376)
Browse files Browse the repository at this point in the history
Signed-off-by: Shaobo He <shaobohe@amazon.com>
  • Loading branch information
shaobo-he-aws authored Dec 30, 2024
1 parent 2e8cbf1 commit 9e1086c
Show file tree
Hide file tree
Showing 16 changed files with 148 additions and 186 deletions.
22 changes: 8 additions & 14 deletions cedar-policy-cli/tests/sample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ fn run_link_test(
links_file: impl Into<String>,
template_id: impl Into<String>,
linked_id: impl Into<String>,
env: HashMap<SlotId, String>,
env: impl IntoIterator<Item = (SlotId, String)>,
expected: CedarExitCode,
) {
let cmd = LinkArgs {
Expand All @@ -117,7 +117,9 @@ fn run_link_test(
},
template_id: template_id.into(),
new_id: linked_id.into(),
arguments: Arguments { data: env },
arguments: Arguments {
data: HashMap::from_iter(env),
},
};
let output = link(&cmd);
assert_eq!(output, expected);
Expand Down Expand Up @@ -792,9 +794,7 @@ fn test_link_samples() {
&linked_file_name,
"AccessVacation",
"AliceAccess",
[(SlotId::principal(), "User::\"alice\"".to_string())]
.into_iter()
.collect(),
[(SlotId::principal(), "User::\"alice\"".to_string())],
CedarExitCode::Failure,
);

Expand All @@ -803,9 +803,7 @@ fn test_link_samples() {
&linked_file_name,
"AccessVacation",
"AliceAccess",
[(SlotId::principal(), "invalid".to_string())]
.into_iter()
.collect(),
[(SlotId::principal(), "invalid".to_string())],
CedarExitCode::Failure,
);

Expand All @@ -814,9 +812,7 @@ fn test_link_samples() {
&linked_file_name,
"AccessVacation",
"AliceAccess",
[(SlotId::principal(), "User::\"alice\"".to_string())]
.into_iter()
.collect(),
[(SlotId::principal(), "User::\"alice\"".to_string())],
CedarExitCode::Success,
);

Expand Down Expand Up @@ -845,9 +841,7 @@ fn test_link_samples() {
&linked_file_name,
"AccessVacation",
"BobAccess",
[(SlotId::principal(), "User::\"bob\"".to_string())]
.into_iter()
.collect(),
[(SlotId::principal(), "User::\"bob\"".to_string())],
CedarExitCode::Success,
);

Expand Down
24 changes: 9 additions & 15 deletions cedar-policy-core/src/ast/policy_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,11 +630,10 @@ mod test {
.expect("Failed to parse");
pset.add_template(template).expect("Add failed");

let env: HashMap<SlotId, EntityUID> = [(
let env: HashMap<SlotId, EntityUID> = std::iter::once((
SlotId::principal(),
r#"Test::"test""#.parse().expect("Failed to parse"),
)]
.into_iter()
))
.collect();

let r = pset.link(PolicyID::from_string("t"), PolicyID::from_string("id"), env);
Expand Down Expand Up @@ -669,11 +668,10 @@ mod test {
)
.expect("Failed to parse"),
);
let env1: HashMap<SlotId, EntityUID> = [(
let env1: HashMap<SlotId, EntityUID> = std::iter::once((
SlotId::principal(),
r#"Test::"test1""#.parse().expect("Failed to parse"),
)]
.into_iter()
))
.collect();

let p1 = Template::link(Arc::clone(&template), PolicyID::from_string("link"), env1)
Expand All @@ -686,11 +684,10 @@ mod test {
"Adding link should implicitly add the template"
);

let env2: HashMap<SlotId, EntityUID> = [(
let env2: HashMap<SlotId, EntityUID> = std::iter::once((
SlotId::principal(),
r#"Test::"test2""#.parse().expect("Failed to parse"),
)]
.into_iter()
))
.collect();

let p2 = Template::link(
Expand All @@ -717,11 +714,10 @@ mod test {
)
.expect("Failed to parse"),
);
let env3: HashMap<SlotId, EntityUID> = [(
let env3: HashMap<SlotId, EntityUID> = std::iter::once((
SlotId::resource(),
r#"Test::"test3""#.parse().expect("Failed to parse"),
)]
.into_iter()
))
.collect();

let p4 = Template::link(
Expand Down Expand Up @@ -781,9 +777,7 @@ mod test {
set.link(
PolicyID::from_string("template"),
PolicyID::from_string("id"),
[(SlotId::principal(), EntityUID::with_eid("eid"))]
.into_iter()
.collect(),
std::iter::once((SlotId::principal(), EntityUID::with_eid("eid"))).collect(),
)
.expect("Linking failed!");
assert_eq!(set.static_policies().count(), 1);
Expand Down
29 changes: 14 additions & 15 deletions cedar-policy-core/src/entities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ pub enum TCComputation {
#[cfg(test)]
// PANIC SAFETY unit tests
#[allow(clippy::panic)]
#[allow(clippy::cognitive_complexity)]
mod json_parsing_tests {

use super::*;
Expand Down Expand Up @@ -1731,13 +1732,13 @@ mod json_parsing_tests {
}

/// helper function
fn test_entities() -> (Entity, Entity, Entity, Entity) {
(
fn test_entities() -> [Entity; 4] {
[
Entity::with_uid(EntityUID::with_eid("test_principal")),
Entity::with_uid(EntityUID::with_eid("test_action")),
Entity::with_uid(EntityUID::with_eid("test_resource")),
Entity::with_uid(EntityUID::with_eid("test")),
)
]
}

/// Test that we can take an Entities, write it to JSON, parse that JSON
Expand All @@ -1750,9 +1751,8 @@ mod json_parsing_tests {
roundtrip(&empty_entities).expect("should roundtrip without errors")
);

let (e0, e1, e2, e3) = test_entities();
let entities = Entities::from_entities(
[e0, e1, e2, e3],
test_entities(),
None::<&NoEntitiesSchema>,
TCComputation::ComputeNow,
Extensions::none(),
Expand Down Expand Up @@ -1960,17 +1960,18 @@ mod json_parsing_tests {

// PANIC SAFETY: Unit Test Code
#[allow(clippy::panic)]
#[allow(clippy::cognitive_complexity)]
#[cfg(test)]
// PANIC SAFETY unit tests
#[allow(clippy::panic)]
mod entities_tests {
use super::*;

#[test]
fn empty_entities() {
let e = Entities::new();
let es = e.iter().collect::<Vec<_>>();
assert!(es.is_empty(), "This vec should be empty");
assert!(
e.iter().next().is_none(),
"The entity store should be empty"
);
}

/// helper function
Expand Down Expand Up @@ -2051,6 +2052,7 @@ mod entities_tests {

// PANIC SAFETY: Unit Test Code
#[allow(clippy::panic)]
#[allow(clippy::cognitive_complexity)]
#[cfg(test)]
mod schema_based_parsing_tests {
use super::json::NullEntityTypeDescription;
Expand Down Expand Up @@ -2079,9 +2081,7 @@ mod schema_based_parsing_tests {
r#"Action::"view""# => Some(Arc::new(Entity::new_with_attr_partial_value(
action.clone(),
[(SmolStr::from("foo"), PartialValue::from(34))],
[r#"Action::"readOnly""#.parse().expect("valid uid")]
.into_iter()
.collect(),
std::iter::once(r#"Action::"readOnly""#.parse().expect("valid uid")).collect(),
))),
r#"Action::"readOnly""# => Some(Arc::new(Entity::with_uid(
r#"Action::"readOnly""#.parse().expect("valid uid"),
Expand Down Expand Up @@ -2175,11 +2175,10 @@ mod schema_based_parsing_tests {
(
"inner3".into(),
AttributeType::required(SchemaType::Record {
attrs: [(
attrs: std::iter::once((
"innerinner".into(),
AttributeType::required(employee_ty()),
)]
.into_iter()
))
.collect(),
open_attrs: false,
}),
Expand Down
6 changes: 3 additions & 3 deletions cedar-policy-core/src/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,7 @@ fn stack_size_check() -> Result<()> {

// PANIC SAFETY: Unit Test Code
#[allow(clippy::panic)]
#[allow(clippy::cognitive_complexity)]
#[cfg(test)]
pub(crate) mod test {
use std::str::FromStr;
Expand Down Expand Up @@ -5033,9 +5034,8 @@ pub(crate) mod test {
Either::Right(expr) => {
println!("{expr}");
assert!(expr.contains_unknown());
let m: HashMap<_, _> = [("principal".into(), Value::from(euid))]
.into_iter()
.collect();
let m: HashMap<_, _> =
std::iter::once(("principal".into(), Value::from(euid))).collect();
let new_expr = expr.substitute_typed(&m).unwrap();
assert_eq!(
e.partial_interpret(&new_expr, &HashMap::new())
Expand Down
1 change: 1 addition & 0 deletions cedar-policy-core/src/extensions/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,7 @@ pub fn extension() -> Extension {
}

#[cfg(test)]
#[allow(clippy::cognitive_complexity)]
mod tests {
use std::{str::FromStr, sync::Arc};

Expand Down
1 change: 1 addition & 0 deletions cedar-policy-core/src/extensions/ipaddr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ pub fn extension() -> Extension {
// PANIC SAFETY: Unit Test Code
#[allow(clippy::panic)]
#[cfg(test)]
#[allow(clippy::cognitive_complexity)]
mod tests {
use super::*;
use crate::ast::{Expr, Type, Value};
Expand Down
1 change: 1 addition & 0 deletions cedar-policy-core/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ pub(crate) mod test_utils {

// PANIC SAFETY: Unit Test Code
#[allow(clippy::panic, clippy::indexing_slicing)]
#[allow(clippy::cognitive_complexity)]
#[cfg(test)]
/// Tests for the top-level parsing APIs
mod tests {
Expand Down
1 change: 1 addition & 0 deletions cedar-policy-core/src/parser/cst_to_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2046,6 +2046,7 @@ fn construct_expr_record(kvs: Vec<(SmolStr, ast::Expr)>, loc: Loc) -> Result<ast
#[allow(clippy::panic)]
// PANIC SAFETY: Unit Test Code
#[allow(clippy::indexing_slicing)]
#[allow(clippy::cognitive_complexity)]
#[cfg(test)]
mod tests {
use super::*;
Expand Down
7 changes: 1 addition & 6 deletions cedar-policy-core/src/parser/text_to_cst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1003,12 +1003,7 @@ mod tests {
.expect("parser error")
.node
.expect("no data");
let success = policies
.0
.into_iter()
.filter_map(|p| p.node)
.collect::<Vec<_>>();
assert!(success.len() == 2);
assert_eq!(policies.0.into_iter().filter_map(|p| p.node).count(), 2);
}

#[test]
Expand Down
3 changes: 2 additions & 1 deletion cedar-policy-validator/src/cedar_schema/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#![allow(clippy::cognitive_complexity)]

// PANIC SAFETY: unit tests
#[allow(clippy::panic)]
Expand Down Expand Up @@ -737,7 +738,7 @@ namespace Baz {action "Foo" appliesTo {
}),
);
assert_has_type(
&attributes.get("viewACL").unwrap(),
attributes.get("viewACL").unwrap(),
json_schema::Type::Type(json_schema::TypeVariant::EntityOrCommon {
type_name: "DocumentShare".parse().unwrap(),
}),
Expand Down
27 changes: 9 additions & 18 deletions cedar-policy-validator/src/json_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2779,8 +2779,7 @@ mod test_json_roundtrip {
entity_types: BTreeMap::new(),
actions: BTreeMap::new(),
annotations: Annotations::new(),
}
.into(),
},
)]));
roundtrip(fragment);
}
Expand All @@ -2794,8 +2793,7 @@ mod test_json_roundtrip {
entity_types: BTreeMap::new(),
actions: BTreeMap::new(),
annotations: Annotations::new(),
}
.into(),
},
)]));
roundtrip(fragment);
}
Expand All @@ -2816,8 +2814,7 @@ mod test_json_roundtrip {
}))),
tags: None,
annotations: Annotations::new(),
}
.into(),
},
)]),
actions: BTreeMap::from([(
"action".into(),
Expand All @@ -2835,12 +2832,10 @@ mod test_json_roundtrip {
}),
member_of: None,
annotations: Annotations::new(),
}
.into(),
},
)]),
annotations: Annotations::new(),
}
.into(),
},
)]));
roundtrip(fragment);
}
Expand All @@ -2864,13 +2859,11 @@ mod test_json_roundtrip {
))),
tags: None,
annotations: Annotations::new(),
}
.into(),
},
)]),
actions: BTreeMap::new(),
annotations: Annotations::new(),
}
.into(),
},
),
(
None,
Expand All @@ -2893,12 +2886,10 @@ mod test_json_roundtrip {
}),
member_of: None,
annotations: Annotations::new(),
}
.into(),
},
)]),
annotations: Annotations::new(),
}
.into(),
},
),
]));
roundtrip(fragment);
Expand Down
2 changes: 2 additions & 0 deletions cedar-policy-validator/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3588,6 +3588,7 @@ pub(crate) mod test {
mod test_579; // located in separate file test_579.rs

#[cfg(test)]
#[allow(clippy::cognitive_complexity)]
mod test_rfc70 {
use super::test::utils::*;
use super::ValidatorSchema;
Expand Down Expand Up @@ -4578,6 +4579,7 @@ mod test_rfc70 {

/// Tests involving entity tags (RFC 82)
#[cfg(test)]
#[allow(clippy::cognitive_complexity)]
mod entity_tags {
use super::{test::utils::*, *};
use cedar_policy_core::{
Expand Down
Loading

0 comments on commit 9e1086c

Please sign in to comment.