From 9e1086c8dcc6ec249aba3d8d445d458bbfcd8359 Mon Sep 17 00:00:00 2001 From: shaobo-he-aws <130499339+shaobo-he-aws@users.noreply.github.com> Date: Mon, 30 Dec 2024 09:41:47 -0800 Subject: [PATCH] Apply clippy auto-fixes (#1376) Signed-off-by: Shaobo He --- cedar-policy-cli/tests/sample.rs | 22 +-- cedar-policy-core/src/ast/policy_set.rs | 24 +-- cedar-policy-core/src/entities.rs | 29 ++- cedar-policy-core/src/evaluator.rs | 6 +- cedar-policy-core/src/extensions/datetime.rs | 1 + cedar-policy-core/src/extensions/ipaddr.rs | 1 + cedar-policy-core/src/parser.rs | 1 + cedar-policy-core/src/parser/cst_to_ast.rs | 1 + cedar-policy-core/src/parser/text_to_cst.rs | 7 +- .../src/cedar_schema/test.rs | 3 +- cedar-policy-validator/src/json_schema.rs | 27 +-- cedar-policy-validator/src/schema.rs | 2 + cedar-policy-validator/src/typecheck/test.rs | 1 + cedar-policy/src/ffi/check_parse.rs | 36 ++-- cedar-policy/src/prop_test_policy_set.rs | 2 +- cedar-policy/src/tests.rs | 171 ++++++++---------- 16 files changed, 148 insertions(+), 186 deletions(-) diff --git a/cedar-policy-cli/tests/sample.rs b/cedar-policy-cli/tests/sample.rs index bf2c14d23..ae36bca5a 100644 --- a/cedar-policy-cli/tests/sample.rs +++ b/cedar-policy-cli/tests/sample.rs @@ -106,7 +106,7 @@ fn run_link_test( links_file: impl Into, template_id: impl Into, linked_id: impl Into, - env: HashMap, + env: impl IntoIterator, expected: CedarExitCode, ) { let cmd = LinkArgs { @@ -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); @@ -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, ); @@ -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, ); @@ -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, ); @@ -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, ); diff --git a/cedar-policy-core/src/ast/policy_set.rs b/cedar-policy-core/src/ast/policy_set.rs index af844f8a5..1508c7922 100644 --- a/cedar-policy-core/src/ast/policy_set.rs +++ b/cedar-policy-core/src/ast/policy_set.rs @@ -630,11 +630,10 @@ mod test { .expect("Failed to parse"); pset.add_template(template).expect("Add failed"); - let env: HashMap = [( + let env: HashMap = 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); @@ -669,11 +668,10 @@ mod test { ) .expect("Failed to parse"), ); - let env1: HashMap = [( + let env1: HashMap = 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) @@ -686,11 +684,10 @@ mod test { "Adding link should implicitly add the template" ); - let env2: HashMap = [( + let env2: HashMap = std::iter::once(( SlotId::principal(), r#"Test::"test2""#.parse().expect("Failed to parse"), - )] - .into_iter() + )) .collect(); let p2 = Template::link( @@ -717,11 +714,10 @@ mod test { ) .expect("Failed to parse"), ); - let env3: HashMap = [( + let env3: HashMap = std::iter::once(( SlotId::resource(), r#"Test::"test3""#.parse().expect("Failed to parse"), - )] - .into_iter() + )) .collect(); let p4 = Template::link( @@ -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); diff --git a/cedar-policy-core/src/entities.rs b/cedar-policy-core/src/entities.rs index b5c039b70..6ea78cef0 100644 --- a/cedar-policy-core/src/entities.rs +++ b/cedar-policy-core/src/entities.rs @@ -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::*; @@ -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 @@ -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(), @@ -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::>(); - assert!(es.is_empty(), "This vec should be empty"); + assert!( + e.iter().next().is_none(), + "The entity store should be empty" + ); } /// helper function @@ -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; @@ -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"), @@ -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, }), diff --git a/cedar-policy-core/src/evaluator.rs b/cedar-policy-core/src/evaluator.rs index a8b30e520..864eaca9e 100644 --- a/cedar-policy-core/src/evaluator.rs +++ b/cedar-policy-core/src/evaluator.rs @@ -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; @@ -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()) diff --git a/cedar-policy-core/src/extensions/datetime.rs b/cedar-policy-core/src/extensions/datetime.rs index 31e43acaf..5b97b4852 100644 --- a/cedar-policy-core/src/extensions/datetime.rs +++ b/cedar-policy-core/src/extensions/datetime.rs @@ -719,6 +719,7 @@ pub fn extension() -> Extension { } #[cfg(test)] +#[allow(clippy::cognitive_complexity)] mod tests { use std::{str::FromStr, sync::Arc}; diff --git a/cedar-policy-core/src/extensions/ipaddr.rs b/cedar-policy-core/src/extensions/ipaddr.rs index a5c623043..590f962bd 100644 --- a/cedar-policy-core/src/extensions/ipaddr.rs +++ b/cedar-policy-core/src/extensions/ipaddr.rs @@ -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}; diff --git a/cedar-policy-core/src/parser.rs b/cedar-policy-core/src/parser.rs index 428adb6b4..b6d06dc0e 100644 --- a/cedar-policy-core/src/parser.rs +++ b/cedar-policy-core/src/parser.rs @@ -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 { diff --git a/cedar-policy-core/src/parser/cst_to_ast.rs b/cedar-policy-core/src/parser/cst_to_ast.rs index bb329ea4c..882ce4480 100644 --- a/cedar-policy-core/src/parser/cst_to_ast.rs +++ b/cedar-policy-core/src/parser/cst_to_ast.rs @@ -2046,6 +2046,7 @@ fn construct_expr_record(kvs: Vec<(SmolStr, ast::Expr)>, loc: Loc) -> Result>(); - assert!(success.len() == 2); + assert_eq!(policies.0.into_iter().filter_map(|p| p.node).count(), 2); } #[test] diff --git a/cedar-policy-validator/src/cedar_schema/test.rs b/cedar-policy-validator/src/cedar_schema/test.rs index bf72a6abb..4cf7628c5 100644 --- a/cedar-policy-validator/src/cedar_schema/test.rs +++ b/cedar-policy-validator/src/cedar_schema/test.rs @@ -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)] @@ -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(), }), diff --git a/cedar-policy-validator/src/json_schema.rs b/cedar-policy-validator/src/json_schema.rs index f05a9cc10..d5825c690 100644 --- a/cedar-policy-validator/src/json_schema.rs +++ b/cedar-policy-validator/src/json_schema.rs @@ -2779,8 +2779,7 @@ mod test_json_roundtrip { entity_types: BTreeMap::new(), actions: BTreeMap::new(), annotations: Annotations::new(), - } - .into(), + }, )])); roundtrip(fragment); } @@ -2794,8 +2793,7 @@ mod test_json_roundtrip { entity_types: BTreeMap::new(), actions: BTreeMap::new(), annotations: Annotations::new(), - } - .into(), + }, )])); roundtrip(fragment); } @@ -2816,8 +2814,7 @@ mod test_json_roundtrip { }))), tags: None, annotations: Annotations::new(), - } - .into(), + }, )]), actions: BTreeMap::from([( "action".into(), @@ -2835,12 +2832,10 @@ mod test_json_roundtrip { }), member_of: None, annotations: Annotations::new(), - } - .into(), + }, )]), annotations: Annotations::new(), - } - .into(), + }, )])); roundtrip(fragment); } @@ -2864,13 +2859,11 @@ mod test_json_roundtrip { ))), tags: None, annotations: Annotations::new(), - } - .into(), + }, )]), actions: BTreeMap::new(), annotations: Annotations::new(), - } - .into(), + }, ), ( None, @@ -2893,12 +2886,10 @@ mod test_json_roundtrip { }), member_of: None, annotations: Annotations::new(), - } - .into(), + }, )]), annotations: Annotations::new(), - } - .into(), + }, ), ])); roundtrip(fragment); diff --git a/cedar-policy-validator/src/schema.rs b/cedar-policy-validator/src/schema.rs index c3241fc83..c668b04d1 100644 --- a/cedar-policy-validator/src/schema.rs +++ b/cedar-policy-validator/src/schema.rs @@ -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; @@ -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::{ diff --git a/cedar-policy-validator/src/typecheck/test.rs b/cedar-policy-validator/src/typecheck/test.rs index 813a73e68..ce7343260 100644 --- a/cedar-policy-validator/src/typecheck/test.rs +++ b/cedar-policy-validator/src/typecheck/test.rs @@ -19,6 +19,7 @@ #![allow(clippy::panic)] // PANIC SAFETY unit tests #![allow(clippy::indexing_slicing)] +#![allow(clippy::cognitive_complexity)] pub(crate) mod test_utils; diff --git a/cedar-policy/src/ffi/check_parse.rs b/cedar-policy/src/ffi/check_parse.rs index 28e45f2b7..eb18f080f 100644 --- a/cedar-policy/src/ffi/check_parse.rs +++ b/cedar-policy/src/ffi/check_parse.rs @@ -260,12 +260,12 @@ mod test { use serde_json::json; #[track_caller] - fn assert_check_parse_is_ok(parse_result: CheckParseAnswer) { + fn assert_check_parse_is_ok(parse_result: &CheckParseAnswer) { assert_matches!(parse_result, CheckParseAnswer::Success); } #[track_caller] - fn assert_check_parse_is_err(parse_result: CheckParseAnswer) -> Vec { + fn assert_check_parse_is_err(parse_result: &CheckParseAnswer) -> &[DetailedError] { assert_matches!( parse_result, CheckParseAnswer::Failure { errors } => errors @@ -278,7 +278,7 @@ mod test { "staticPolicies": "permit(principal, action, resource);" }); let answer = serde_json::from_value(check_parse_policy_set_json(call).unwrap()).unwrap(); - assert_check_parse_is_ok(answer); + assert_check_parse_is_ok(&answer); } #[test] @@ -287,7 +287,7 @@ mod test { "staticPolicies": "forbid(principal, action, resource); permit(principal == User::\"alice\", action == Action::\"view\", resource in Albums::\"alice_albums\");" }); let answer = serde_json::from_value(check_parse_policy_set_json(call).unwrap()).unwrap(); - assert_check_parse_is_ok(answer); + assert_check_parse_is_ok(&answer); } #[test] @@ -296,9 +296,9 @@ mod test { "staticPolicies": "forbid(principal, action, resource);permit(2pac, action, resource)" }); let answer = serde_json::from_value(check_parse_policy_set_json(call).unwrap()).unwrap(); - let errs = assert_check_parse_is_err(answer); + let errs = assert_check_parse_is_err(&answer); assert_exactly_one_error( - &errs, + errs, "failed to parse policies from string: unexpected token `2`", None, ); @@ -312,14 +312,14 @@ mod test { } }); let answer = serde_json::from_value(check_parse_policy_set_json(call).unwrap()).unwrap(); - assert_check_parse_is_ok(answer); + assert_check_parse_is_ok(&answer); } #[test] fn check_parse_schema_succeeds_empty_schema() { let call = json!({}); let answer = serde_json::from_value(check_parse_schema_json(call).unwrap()).unwrap(); - assert_check_parse_is_ok(answer); + assert_check_parse_is_ok(&answer); } #[test] @@ -331,7 +331,7 @@ mod test { } }); let answer = serde_json::from_value(check_parse_schema_json(call).unwrap()).unwrap(); - assert_check_parse_is_ok(answer); + assert_check_parse_is_ok(&answer); } #[test] @@ -342,9 +342,9 @@ mod test { } }); let answer = serde_json::from_value(check_parse_schema_json(call).unwrap()).unwrap(); - let errs = assert_check_parse_is_err(answer); + let errs = assert_check_parse_is_err(&answer); assert_exactly_one_error( - &errs, + errs, "failed to parse schema from JSON: missing field `actions`", None, ); @@ -389,7 +389,7 @@ mod test { } }); let answer = serde_json::from_value(check_parse_entities_json(call).unwrap()).unwrap(); - assert_check_parse_is_ok(answer); + assert_check_parse_is_ok(&answer); } #[test] @@ -410,7 +410,7 @@ mod test { ] }); let answer = serde_json::from_value(check_parse_entities_json(call).unwrap()).unwrap(); - assert_check_parse_is_ok(answer); + assert_check_parse_is_ok(&answer); } #[test] @@ -445,9 +445,9 @@ mod test { } }); let answer = serde_json::from_value(check_parse_entities_json(call).unwrap()).unwrap(); - let errs = assert_check_parse_is_err(answer); + let errs = assert_check_parse_is_err(&answer); assert_exactly_one_error( - &errs, + errs, "error during entity deserialization: in uid field of , expected a literal entity reference, but got `\"TheNamespace::User::\\\"alice\\\"\"`", Some("literal entity references can be made with `{ \"type\": \"SomeType\", \"id\": \"SomeId\" }`") ); @@ -491,7 +491,7 @@ mod test { }); let answer = serde_json::from_value(check_parse_context_json(call).unwrap()).unwrap(); - assert_check_parse_is_ok(answer); + assert_check_parse_is_ok(&answer); } #[test] @@ -531,7 +531,7 @@ mod test { } }); let answer = serde_json::from_value(check_parse_context_json(call).unwrap()).unwrap(); - let errs = assert_check_parse_is_err(answer); - assert_exactly_one_error(&errs, "while parsing context, expected the record to have an attribute `referrer`, but it does not", None); + let errs = assert_check_parse_is_err(&answer); + assert_exactly_one_error(errs, "while parsing context, expected the record to have an attribute `referrer`, but it does not", None); } } diff --git a/cedar-policy/src/prop_test_policy_set.rs b/cedar-policy/src/prop_test_policy_set.rs index 6772311f6..4d81a632f 100644 --- a/cedar-policy/src/prop_test_policy_set.rs +++ b/cedar-policy/src/prop_test_policy_set.rs @@ -112,7 +112,7 @@ impl PolicySetModel { panic!("template to link map should have Vec for existing template") } }; - assert!(self.link_to_template_map.get(policy_name).is_none()); + assert!(!self.link_to_template_map.contains_key(policy_name)); self.link_to_template_map .insert(policy_name.to_owned(), template_name.clone()); } diff --git a/cedar-policy/src/tests.rs b/cedar-policy/src/tests.rs index e8b0fb779..7ed580b05 100644 --- a/cedar-policy/src/tests.rs +++ b/cedar-policy/src/tests.rs @@ -17,6 +17,7 @@ #![cfg(test)] // PANIC SAFETY unit tests #![allow(clippy::panic)] +#![allow(clippy::cognitive_complexity, clippy::too_many_lines)] use super::*; @@ -4858,7 +4859,7 @@ mod policy_set_est_tests { #[test] fn test_partition_fold_err() { let even_or_odd = |s: &str| { - i64::from_str_radix(s, 10).map(|i| { + s.parse::().map(|i| { if i % 2 == 0 { Either::Left(i) } else { @@ -5074,10 +5075,11 @@ mod policy_set_est_tests { r#"User::"John""#.parse().unwrap() )])) ); - if let Err(_) = policyset + if policyset .get_linked_policies(PolicyId::new("template")) .unwrap() .exactly_one() + .is_err() { panic!("Should have exactly one"); }; @@ -6323,30 +6325,25 @@ mod reserved_keywords_in_policies { } #[track_caller] - fn assert_valid_expression(src: String) { - assert_matches!(Expression::from_str(&src), Ok(_)); + fn assert_valid_expression(src: &str) { + assert_matches!(Expression::from_str(src), Ok(_)); } #[track_caller] - fn assert_invalid_expression(src: String, error: String, underline: String) { - let expected_err = ExpectedErrorMessageBuilder::error(&error) - .exactly_one_underline(&underline) + fn assert_invalid_expression(src: &str, error: &str, underline: &str) { + let expected_err = ExpectedErrorMessageBuilder::error(error) + .exactly_one_underline(underline) .build(); - assert_matches!(Expression::from_str(&src), Err(err) => expect_err(&*src, &Report::new(err), &expected_err)); + assert_matches!(Expression::from_str(src), Err(err) => expect_err(src, &Report::new(err), &expected_err)); } #[track_caller] - fn assert_invalid_expression_with_help( - src: String, - error: String, - underline: String, - help: String, - ) { - let expected_err = ExpectedErrorMessageBuilder::error(&error) - .exactly_one_underline(&underline) - .help(&help) + fn assert_invalid_expression_with_help(src: &str, error: &str, underline: &str, help: &str) { + let expected_err = ExpectedErrorMessageBuilder::error(error) + .exactly_one_underline(underline) + .help(help) .build(); - assert_matches!(Expression::from_str(&src), Err(err) => expect_err(&*src, &Report::new(err), &expected_err)); + assert_matches!(Expression::from_str(src), Err(err) => expect_err(src, &Report::new(err), &expected_err)); } #[test] @@ -6367,16 +6364,16 @@ mod reserved_keywords_in_policies { .chain(RESERVED_NAMESPACE.iter()) .chain(OTHER_SPECIAL_IDENTS.iter()) .for_each(|id| { - assert_valid_expression(format!("{{ \"{id}\": 1 }}")); - assert_valid_expression(format!("principal has \"{id}\"")); - assert_valid_expression(format!("principal[\"{id}\"] == \"foo\"")); + assert_valid_expression(&format!("{{ \"{id}\": 1 }}")); + assert_valid_expression(&format!("principal has \"{id}\"")); + assert_valid_expression(&format!("principal[\"{id}\"] == \"foo\"")); }); // No restrictions on OTHER_SPECIAL_IDENTS for id in &OTHER_SPECIAL_IDENTS { - assert_valid_expression(format!("{{ {id}: 1 }}")); - assert_valid_expression(format!("principal has {id}")); - assert_valid_expression(format!("principal.{id} == \"foo\"")); + assert_valid_expression(&format!("{{ {id}: 1 }}")); + assert_valid_expression(&format!("principal has {id}")); + assert_valid_expression(&format!("principal.{id} == \"foo\"")); } // RESERVED_IDENTS cannot be used as keys without quotes @@ -6385,66 +6382,62 @@ mod reserved_keywords_in_policies { match id { "true" | "false" => { assert_invalid_expression_with_help( - format!("{{ {id}: 1 }}"), - format!("invalid attribute name: {id}"), - id.into(), - "attribute names can either be identifiers or string literals".into(), + &format!("{{ {id}: 1 }}"), + &format!("invalid attribute name: {id}"), + id, + "attribute names can either be identifiers or string literals", ); assert_invalid_expression( - format!("principal has {id}"), - RESERVED_IDENT_MSG(id), - id.to_string(), + &format!("principal has {id}"), + &RESERVED_IDENT_MSG(id), + id, ); } "if" => { assert_invalid_expression( - format!("{{ {id}: 1 }}"), - RESERVED_IDENT_MSG(id), - format!("{id}: 1"), + &format!("{{ {id}: 1 }}"), + &RESERVED_IDENT_MSG(id), + &format!("{id}: 1"), ); assert_invalid_expression( - format!("principal has {id}"), - RESERVED_IDENT_MSG(id), - id.to_string(), + &format!("principal has {id}"), + &RESERVED_IDENT_MSG(id), + id, ); } _ => { assert_invalid_expression( - format!("{{ {id}: 1 }}"), - RESERVED_IDENT_MSG(id), - id.into(), + &format!("{{ {id}: 1 }}"), + &RESERVED_IDENT_MSG(id), + id, ); assert_invalid_expression( - format!("principal has {id}"), - RESERVED_IDENT_MSG(id), - id.into(), + &format!("principal has {id}"), + &RESERVED_IDENT_MSG(id), + id, ); } } // this case leads to a consistent error for all keywords assert_invalid_expression( - format!("principal.{id} == \"foo\""), - RESERVED_IDENT_MSG(id), - id.into(), + &format!("principal.{id} == \"foo\""), + &RESERVED_IDENT_MSG(id), + id, ); } // RESERVED_NAMESPACE cannot be used as keys without quotes for id in RESERVED_NAMESPACE { + assert_invalid_expression(&format!("{{ {id}: 1 }}"), &RESERVED_NAMESPACE_MSG(id), id); assert_invalid_expression( - format!("{{ {id}: 1 }}"), - RESERVED_NAMESPACE_MSG(id), - id.into(), + &format!("principal has {id}"), + &RESERVED_NAMESPACE_MSG(id), + id, ); assert_invalid_expression( - format!("principal has {id}"), - RESERVED_NAMESPACE_MSG(id), - id.into(), - ); - assert_invalid_expression( - format!("principal.{id} == \"foo\""), - RESERVED_NAMESPACE_MSG(id), - id.into(), + &format!("principal.{id} == \"foo\""), + &RESERVED_NAMESPACE_MSG(id), + id, ); } } @@ -6453,35 +6446,31 @@ mod reserved_keywords_in_policies { fn test_reserved_namespace_elements() { // No restrictions on OTHER_SPECIAL_IDENTS for id in &OTHER_SPECIAL_IDENTS { - assert_valid_expression(format!("foo::{id}::\"bar\"")); - assert_valid_expression(format!("principal is {id}::foo")); + assert_valid_expression(&format!("foo::{id}::\"bar\"")); + assert_valid_expression(&format!("principal is {id}::foo")); } // RESERVED_IDENTS cannot be used in namespaces for id in RESERVED_IDENTS { + assert_invalid_expression(&format!("foo::{id}::\"bar\""), &RESERVED_IDENT_MSG(id), id); assert_invalid_expression( - format!("foo::{id}::\"bar\""), - RESERVED_IDENT_MSG(id), - id.into(), - ); - assert_invalid_expression( - format!("principal is {id}::foo"), - RESERVED_IDENT_MSG(id), - id.into(), + &format!("principal is {id}::foo"), + &RESERVED_IDENT_MSG(id), + id, ); } // RESERVED_NAMESPACE cannot be used in namespaces for id in RESERVED_NAMESPACE { assert_invalid_expression( - format!("foo::{id}::\"bar\""), - RESERVED_NAMESPACE_MSG(&format!("foo::{id}")), - format!("foo::{id}"), + &format!("foo::{id}::\"bar\""), + &RESERVED_NAMESPACE_MSG(&format!("foo::{id}")), + &format!("foo::{id}"), ); assert_invalid_expression( - format!("principal is {id}::foo"), - RESERVED_NAMESPACE_MSG(&format!("{id}::foo")), - format!("{id}::foo"), + &format!("principal is {id}::foo"), + &RESERVED_NAMESPACE_MSG(&format!("{id}::foo")), + &format!("{id}::foo"), ); } } @@ -6493,40 +6482,32 @@ mod reserved_keywords_in_policies { for id in RESERVED_IDENTS { assert_invalid_expression( - format!("extension::function::{id}(\"foo\")"), - RESERVED_IDENT_MSG(id), - id.into(), - ); - assert_invalid_expression( - format!("context.{id}(1)"), - RESERVED_IDENT_MSG(id), - id.into(), + &format!("extension::function::{id}(\"foo\")"), + &RESERVED_IDENT_MSG(id), + id, ); + assert_invalid_expression(&format!("context.{id}(1)"), &RESERVED_IDENT_MSG(id), id); } for id in RESERVED_NAMESPACE { assert_invalid_expression( - format!("extension::function::{id}(\"foo\")"), - RESERVED_NAMESPACE_MSG(&format!("extension::function::{id}")), - format!("extension::function::{id}"), - ); - assert_invalid_expression( - format!("context.{id}(1)"), - RESERVED_NAMESPACE_MSG(id), - id.into(), + &format!("extension::function::{id}(\"foo\")"), + &RESERVED_NAMESPACE_MSG(&format!("extension::function::{id}")), + &format!("extension::function::{id}"), ); + assert_invalid_expression(&format!("context.{id}(1)"), &RESERVED_NAMESPACE_MSG(id), id); } for id in OTHER_SPECIAL_IDENTS { assert_invalid_expression( - format!("extension::function::{id}(\"foo\")"), - format!("`extension::function::{id}` is not a valid function"), - format!("extension::function::{id}(\"foo\")"), + &format!("extension::function::{id}(\"foo\")"), + &format!("`extension::function::{id}` is not a valid function"), + &format!("extension::function::{id}(\"foo\")"), ); assert_invalid_expression( - format!("context.{id}(1)"), - format!("`{id}` is not a valid method"), - format!("context.{id}(1)"), + &format!("context.{id}(1)"), + &format!("`{id}` is not a valid method"), + &format!("context.{id}(1)"), ); } }