Skip to content

Commit

Permalink
Format and fix clippy warnings for Rust version 1.72 (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
shaobo-he-aws authored Aug 28, 2023
1 parent 86d3be5 commit 8459a4a
Show file tree
Hide file tree
Showing 19 changed files with 190 additions and 167 deletions.
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ members = [
"cedar-policy-cli",
]

resolver = "2"

# Enable global integer overflow detection for the release profile
[profile.release]
overflow-checks = true



2 changes: 1 addition & 1 deletion cedar-policy-core/src/ast/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ impl Entity {
}

/// Iterate over this entity's attributes
pub fn attrs<'s>(&'s self) -> impl Iterator<Item = (&'s str, BorrowedRestrictedExpr<'s>)> {
pub fn attrs(&self) -> impl Iterator<Item = (&str, BorrowedRestrictedExpr<'_>)> {
self.attrs
.iter()
.map(|(k, v)| (k.as_str(), v.as_borrowed()))
Expand Down
2 changes: 1 addition & 1 deletion cedar-policy-core/src/ast/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ pub mod test {

// Patterns that do not match "*"
assert!(!string_map("\u{0000}").wildcard_match("*"));
assert!(!string_map(r#"\u{0000}"#).wildcard_match("*"));
assert!(!string_map(r"\u{0000}").wildcard_match("*"));
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion cedar-policy-core/src/ast/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl EntityUIDEntry {
/// Get the UID of the entry, or `None` if it is unknown (partial evaluation)
pub fn uid(&self) -> Option<&EntityUID> {
match self {
Self::Concrete(euid) => Some(&euid),
Self::Concrete(euid) => Some(euid),
Self::Unknown => None,
}
}
Expand Down
12 changes: 3 additions & 9 deletions cedar-policy-core/src/authorizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,12 @@ impl Authorizer {
match self.error_handling {
ErrorHandling::Deny => Response::new(
Decision::Deny,
idset
.chain(partial.diagnostics.reason.into_iter())
.collect(),
idset.chain(partial.diagnostics.reason).collect(),
errors,
),
ErrorHandling::Forbid => Response::new(
Decision::Deny,
idset
.chain(partial.diagnostics.reason.into_iter())
.collect(),
idset.chain(partial.diagnostics.reason).collect(),
errors,
),
ErrorHandling::Skip => {
Expand All @@ -148,9 +144,7 @@ impl Authorizer {
} else {
Response::new(
Decision::Deny,
idset
.chain(partial.diagnostics.reason.into_iter())
.collect(),
idset.chain(partial.diagnostics.reason).collect(),
errors,
)
}
Expand Down
26 changes: 19 additions & 7 deletions cedar-policy-core/src/entities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1109,7 +1109,7 @@ mod json_parsing_tests {
[
("foo".into(), RestrictedExpr::val(false)),
("bar".into(), RestrictedExpr::val(-234)),
("ham".into(), RestrictedExpr::val(r#"a b c * / ? \"#)),
("ham".into(), RestrictedExpr::val(r"a b c * / ? \")),
(
"123".into(),
RestrictedExpr::val(EntityUID::with_eid("mom")),
Expand Down Expand Up @@ -1496,14 +1496,18 @@ mod schema_based_parsing_tests {
.expect("hr_contacts attr should exist");
assert!(matches!(hr_contacts.expr_kind(), &ExprKind::Set(_)));
let contact = {
let ExprKind::Set(set) = hr_contacts.expr_kind() else { panic!("already checked it was Set") };
let ExprKind::Set(set) = hr_contacts.expr_kind() else {
panic!("already checked it was Set")
};
set.iter().next().expect("should be at least one contact")
};
assert!(matches!(contact.expr_kind(), &ExprKind::Record { .. }));
let json_blob = parsed
.get("json_blob")
.expect("json_blob attr should exist");
let ExprKind::Record { pairs } = json_blob.expr_kind() else { panic!("expected json_blob to be a Record") };
let ExprKind::Record { pairs } = json_blob.expr_kind() else {
panic!("expected json_blob to be a Record")
};
let (_, inner1) = pairs
.iter()
.find(|(k, _)| k == "inner1")
Expand All @@ -1517,7 +1521,9 @@ mod schema_based_parsing_tests {
.find(|(k, _)| k == "inner3")
.expect("inner3 attr should exist");
assert!(matches!(inner3.expr_kind(), &ExprKind::Record { .. }));
let ExprKind::Record { pairs: innerpairs } = inner3.expr_kind() else { panic!("already checked it was Record") };
let ExprKind::Record { pairs: innerpairs } = inner3.expr_kind() else {
panic!("already checked it was Record")
};
let (_, innerinner) = innerpairs
.iter()
.find(|(k, _)| k == "innerinner")
Expand Down Expand Up @@ -1571,7 +1577,9 @@ mod schema_based_parsing_tests {
.expect("hr_contacts attr should exist");
assert!(matches!(hr_contacts.expr_kind(), &ExprKind::Set(_)));
let contact = {
let ExprKind::Set(set) = hr_contacts.expr_kind() else { panic!("already checked it was Set") };
let ExprKind::Set(set) = hr_contacts.expr_kind() else {
panic!("already checked it was Set")
};
set.iter().next().expect("should be at least one contact")
};
assert!(matches!(
Expand All @@ -1581,7 +1589,9 @@ mod schema_based_parsing_tests {
let json_blob = parsed
.get("json_blob")
.expect("json_blob attr should exist");
let ExprKind::Record { pairs } = json_blob.expr_kind() else { panic!("expected json_blob to be a Record") };
let ExprKind::Record { pairs } = json_blob.expr_kind() else {
panic!("expected json_blob to be a Record")
};
let (_, inner1) = pairs
.iter()
.find(|(k, _)| k == "inner1")
Expand All @@ -1595,7 +1605,9 @@ mod schema_based_parsing_tests {
.find(|(k, _)| k == "inner3")
.expect("inner3 attr should exist");
assert!(matches!(inner3.expr_kind(), &ExprKind::Record { .. }));
let ExprKind::Record { pairs: innerpairs } = inner3.expr_kind() else { panic!("already checked it was Record") };
let ExprKind::Record { pairs: innerpairs } = inner3.expr_kind() else {
panic!("already checked it was Record")
};
let (_, innerinner) = innerpairs
.iter()
.find(|(k, _)| k == "innerinner")
Expand Down
2 changes: 1 addition & 1 deletion cedar-policy-core/src/est.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ impl TryFrom<cst::Cond> for Clause {
let expr = match expr {
Ok(expr) => Some(expr),
Err(expr_errs) => {
errs.extend(expr_errs.0.into_iter());
errs.extend(expr_errs.0);
None
}
};
Expand Down
2 changes: 1 addition & 1 deletion cedar-policy-core/src/extensions/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl Decimal {
// check that the string matches the regex
// PANIC SAFETY: This regex does parse
#[allow(clippy::unwrap_used)]
let re = Regex::new(r#"^(-?\d+)\.(\d+)$"#).unwrap();
let re = Regex::new(r"^(-?\d+)\.(\d+)$").unwrap();
if !re.is_match(str.as_ref()) {
return Err(Error::FailedParse(str.as_ref().to_owned()));
}
Expand Down
56 changes: 41 additions & 15 deletions cedar-policy-core/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ use crate::est;
pub fn parse_policyset(text: &str) -> Result<ast::PolicySet, err::ParseErrors> {
let mut errs = err::ParseErrors::new();
let cst = text_to_cst::parse_policies(text)?;
let Some(ast) = cst.to_policyset(&mut errs) else { return Err(errs); };
let Some(ast) = cst.to_policyset(&mut errs) else {
return Err(errs);
};
if errs.is_empty() {
Ok(ast)
} else {
Expand All @@ -62,7 +64,9 @@ pub fn parse_policyset_and_also_return_policy_text(
) -> Result<(HashMap<ast::PolicyID, &str>, ast::PolicySet), err::ParseErrors> {
let mut errs = err::ParseErrors::new();
let cst = text_to_cst::parse_policies(text)?;
let Some(pset) = cst.to_policyset(&mut errs) else { return Err(errs); };
let Some(pset) = cst.to_policyset(&mut errs) else {
return Err(errs);
};
if errs.is_empty() {
// PANIC SAFETY Shouldn't be `none` since `parse_policies()` and `to_policyset()` didn't return `Err`
#[allow(clippy::expect_used)]
Expand Down Expand Up @@ -92,7 +96,9 @@ pub fn parse_policyset_to_ests_and_pset(
) -> Result<(HashMap<ast::PolicyID, est::Policy>, ast::PolicySet), err::ParseErrors> {
let mut errs = err::ParseErrors::new();
let cst = text_to_cst::parse_policies(text)?;
let Some(pset) = cst.to_policyset(&mut errs) else { return Err(errs); };
let Some(pset) = cst.to_policyset(&mut errs) else {
return Err(errs);
};
if errs.is_empty() {
// PANIC SAFETY Shouldn't be `None` since `parse_policies()` and `to_policyset()` didn't return `Err`
#[allow(clippy::expect_used)]
Expand Down Expand Up @@ -123,7 +129,9 @@ pub fn parse_policy_template(
None => ast::PolicyID::from_string("policy0"),
};
let cst = text_to_cst::parse_policy(text)?;
let Some(ast) = cst.to_policy_template(id, &mut errs) else { return Err(errs); };
let Some(ast) = cst.to_policy_template(id, &mut errs) else {
return Err(errs);
};
if errs.is_empty() {
Ok(ast)
} else {
Expand All @@ -144,7 +152,9 @@ pub fn parse_policy_template_to_est_and_ast(
None => ast::PolicyID::from_string("policy0"),
};
let cst = text_to_cst::parse_policy(text)?;
let (Some(ast), Some(cst_node)) = (cst.to_policy_template(id, &mut errs), cst.node) else { return Err(errs); };
let (Some(ast), Some(cst_node)) = (cst.to_policy_template(id, &mut errs), cst.node) else {
return Err(errs);
};
if errs.is_empty() {
let est = cst_node.try_into()?;
Ok((est, ast))
Expand All @@ -163,7 +173,9 @@ pub fn parse_policy(id: Option<String>, text: &str) -> Result<ast::StaticPolicy,
None => ast::PolicyID::from_string("policy0"),
};
let cst = text_to_cst::parse_policy(text)?;
let Some(ast) = cst.to_policy(id, &mut errs) else { return Err(errs); };
let Some(ast) = cst.to_policy(id, &mut errs) else {
return Err(errs);
};
if errs.is_empty() {
Ok(ast)
} else {
Expand All @@ -184,7 +196,9 @@ pub fn parse_policy_to_est_and_ast(
None => ast::PolicyID::from_string("policy0"),
};
let cst = text_to_cst::parse_policy(text)?;
let (Some(ast), Some(cst_node)) = (cst.to_policy(id, &mut errs), cst.node) else { return Err(errs); };
let (Some(ast), Some(cst_node)) = (cst.to_policy(id, &mut errs), cst.node) else {
return Err(errs);
};
if errs.is_empty() {
let est = cst_node.try_into()?;
Ok((est, ast))
Expand All @@ -209,7 +223,9 @@ pub fn parse_policy_or_template_to_est(text: &str) -> Result<est::Policy, err::P
pub(crate) fn parse_expr(ptext: &str) -> Result<ast::Expr, err::ParseErrors> {
let mut errs = err::ParseErrors::new();
let cst = text_to_cst::parse_expr(ptext)?;
let Some(ast) = cst.to_expr(&mut errs) else { return Err(errs); };
let Some(ast) = cst.to_expr(&mut errs) else {
return Err(errs);
};
if errs.is_empty() {
Ok(ast)
} else {
Expand All @@ -235,7 +251,9 @@ pub(crate) fn parse_restrictedexpr(
pub(crate) fn parse_euid(euid: &str) -> Result<ast::EntityUID, err::ParseErrors> {
let mut errs = err::ParseErrors::new();
let cst = text_to_cst::parse_ref(euid)?;
let Some(ast) = cst.to_ref(&mut errs) else { return Err(errs); };
let Some(ast) = cst.to_ref(&mut errs) else {
return Err(errs);
};
if errs.is_empty() {
Ok(ast)
} else {
Expand All @@ -250,7 +268,9 @@ pub(crate) fn parse_euid(euid: &str) -> Result<ast::EntityUID, err::ParseErrors>
pub(crate) fn parse_name(name: &str) -> Result<ast::Name, err::ParseErrors> {
let mut errs = err::ParseErrors::new();
let cst = text_to_cst::parse_name(name)?;
let Some(ast) = cst.to_name(&mut errs) else { return Err(errs); };
let Some(ast) = cst.to_name(&mut errs) else {
return Err(errs);
};
if errs.is_empty() {
Ok(ast)
} else {
Expand All @@ -265,7 +285,9 @@ pub(crate) fn parse_name(name: &str) -> Result<ast::Name, err::ParseErrors> {
pub(crate) fn parse_literal(val: &str) -> Result<ast::Literal, err::ParseErrors> {
let mut errs = err::ParseErrors::new();
let cst = text_to_cst::parse_primary(val)?;
let Some(ast) = cst.to_expr(&mut errs) else { return Err(errs); };
let Some(ast) = cst.to_expr(&mut errs) else {
return Err(errs);
};
if errs.is_empty() {
match ast.into_expr_kind() {
ast::ExprKind::Lit(v) => Ok(v),
Expand Down Expand Up @@ -295,7 +317,9 @@ pub fn parse_internal_string(val: &str) -> Result<SmolStr, err::ParseErrors> {
let mut errs = err::ParseErrors::new();
// we need to add quotes for this to be a valid string literal
let cst = text_to_cst::parse_primary(&format!(r#""{val}""#))?;
let Some(ast) = cst.to_string_literal(&mut errs) else { return Err(errs); };
let Some(ast) = cst.to_string_literal(&mut errs) else {
return Err(errs);
};
if errs.is_empty() {
Ok(ast)
} else {
Expand All @@ -310,7 +334,9 @@ pub fn parse_internal_string(val: &str) -> Result<SmolStr, err::ParseErrors> {
pub(crate) fn parse_ident(id: &str) -> Result<ast::Id, err::ParseErrors> {
let mut errs = err::ParseErrors::new();
let cst = text_to_cst::parse_ident(id)?;
let Some(ast) = cst.to_valid_ident(&mut errs) else { return Err(errs); };
let Some(ast) = cst.to_valid_ident(&mut errs) else {
return Err(errs);
};
if errs.is_empty() {
Ok(ast)
} else {
Expand Down Expand Up @@ -622,9 +648,9 @@ mod parse_tests {
fn test_parse_string() {
// test idempotence
assert_eq!(
ast::Eid::new(parse_internal_string(r#"a\nblock\nid"#).expect("should parse"))
ast::Eid::new(parse_internal_string(r"a\nblock\nid").expect("should parse"))
.to_string(),
r#"a\nblock\nid"#,
r"a\nblock\nid",
);
parse_internal_string(r#"oh, no, a '! "#).expect("single quote should be fine");
parse_internal_string(r#"oh, no, a "! "#).expect_err("double quote not allowed");
Expand Down
8 changes: 4 additions & 4 deletions cedar-policy-core/src/parser/cst_to_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2809,7 +2809,7 @@ mod tests {
.expect("failed convert");
match expr.expr_kind() {
ast::ExprKind::Like { pattern, .. } => {
assert_eq!(pattern.to_string(), r#"string\\with\\backslashes"#);
assert_eq!(pattern.to_string(), r"string\\with\\backslashes");
}
_ => panic!("should be a like expr"),
}
Expand All @@ -2824,7 +2824,7 @@ mod tests {
.expect("failed convert");
match expr.expr_kind() {
ast::ExprKind::Like { pattern, .. } => {
assert_eq!(pattern.to_string(), r#"string\*with\*backslashes"#);
assert_eq!(pattern.to_string(), r"string\*with\*backslashes");
}
_ => panic!("should be a like expr"),
}
Expand Down Expand Up @@ -2869,7 +2869,7 @@ mod tests {
ast::ExprKind::Like { pattern, .. } => {
assert_eq!(
pattern.to_string(),
r#"string\\\*with\\\*backslashes\\\*and\\\*stars"#
r"string\\\*with\\\*backslashes\\\*and\\\*stars"
);
}
_ => panic!("should be a like expr"),
Expand Down Expand Up @@ -3277,7 +3277,7 @@ mod tests {
// invalid escape `\p`
test_invalid("\\\\aa\\p", 1);
// invalid escape `\a` and empty unicode escape
test_invalid(r#"\aaa\u{}"#, 2);
test_invalid(r"\aaa\u{}", 2);
}

fn expect_action_error(test: &str, euid_strs: Vec<&str>) {
Expand Down
Loading

0 comments on commit 8459a4a

Please sign in to comment.