From fc6580592d526bef13252c6611519d4a8d20fc6c Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Wed, 14 Jun 2023 00:02:39 -0400 Subject: [PATCH] Use Expr::is_* methods at more call sites (#5075) --- .../src/rules/pyflakes/rules/starred_expressions.rs | 2 +- crates/ruff/src/rules/pyflakes/rules/strings.rs | 13 +++++-------- crates/ruff/src/rules/pylint/rules/logging.rs | 2 +- .../rules/pyupgrade/rules/use_pep604_isinstance.rs | 5 ++--- .../src/rules/ruff/rules/pairwise_over_zipped.rs | 2 +- crates/ruff_python_ast/src/helpers.rs | 6 +++--- 6 files changed, 13 insertions(+), 17 deletions(-) diff --git a/crates/ruff/src/rules/pyflakes/rules/starred_expressions.rs b/crates/ruff/src/rules/pyflakes/rules/starred_expressions.rs index d4b3a875fa..723d2dcc31 100644 --- a/crates/ruff/src/rules/pyflakes/rules/starred_expressions.rs +++ b/crates/ruff/src/rules/pyflakes/rules/starred_expressions.rs @@ -59,7 +59,7 @@ pub(crate) fn starred_expressions( let mut has_starred: bool = false; let mut starred_index: Option = None; for (index, elt) in elts.iter().enumerate() { - if matches!(elt, Expr::Starred(_)) { + if elt.is_starred_expr() { if has_starred && check_two_starred_expressions { return Some(Diagnostic::new(MultipleStarredExpressions, location)); } diff --git a/crates/ruff/src/rules/pyflakes/rules/strings.rs b/crates/ruff/src/rules/pyflakes/rules/strings.rs index a89a6b602a..a6abde308a 100644 --- a/crates/ruff/src/rules/pyflakes/rules/strings.rs +++ b/crates/ruff/src/rules/pyflakes/rules/strings.rs @@ -514,14 +514,13 @@ impl Violation for StringDotFormatMixingAutomatic { } fn has_star_star_kwargs(keywords: &[Keyword]) -> bool { - keywords.iter().any(|k| { - let Keyword { arg, .. } = &k; - arg.is_none() - }) + keywords + .iter() + .any(|keyword| matches!(keyword, Keyword { arg: None, .. })) } fn has_star_args(args: &[Expr]) -> bool { - args.iter().any(|arg| matches!(&arg, Expr::Starred(_))) + args.iter().any(Expr::is_starred_expr) } /// F502 @@ -805,9 +804,7 @@ pub(crate) fn string_dot_format_extra_positional_arguments( .iter() .enumerate() .filter(|(i, arg)| { - !(matches!(arg, Expr::Starred(_)) - || summary.autos.contains(i) - || summary.indices.contains(i)) + !(arg.is_starred_expr() || summary.autos.contains(i) || summary.indices.contains(i)) }) .map(|(i, _)| i) .collect(); diff --git a/crates/ruff/src/rules/pylint/rules/logging.rs b/crates/ruff/src/rules/pylint/rules/logging.rs index 2cf9930caa..6ddffccb20 100644 --- a/crates/ruff/src/rules/pylint/rules/logging.rs +++ b/crates/ruff/src/rules/pylint/rules/logging.rs @@ -93,7 +93,7 @@ pub(crate) fn logging_call( keywords: &[Keyword], ) { // If there are any starred arguments, abort. - if args.iter().any(|arg| matches!(arg, Expr::Starred(_))) { + if args.iter().any(Expr::is_starred_expr) { return; } diff --git a/crates/ruff/src/rules/pyupgrade/rules/use_pep604_isinstance.rs b/crates/ruff/src/rules/pyupgrade/rules/use_pep604_isinstance.rs index d0f8baaf92..89254e9145 100644 --- a/crates/ruff/src/rules/pyupgrade/rules/use_pep604_isinstance.rs +++ b/crates/ruff/src/rules/pyupgrade/rules/use_pep604_isinstance.rs @@ -85,14 +85,13 @@ pub(crate) fn use_pep604_isinstance( } // Ex) `(*args,)` - if elts.iter().any(|elt| matches!(elt, Expr::Starred(_))) { + if elts.iter().any(Expr::is_starred_expr) { return; } let mut diagnostic = Diagnostic::new(NonPEP604Isinstance { kind }, expr.range()); if checker.patch(diagnostic.kind.rule()) { - #[allow(deprecated)] - diagnostic.set_fix(Fix::unspecified(Edit::range_replacement( + diagnostic.set_fix(Fix::suggested(Edit::range_replacement( checker.generator().expr(&union(elts)), types.range(), ))); diff --git a/crates/ruff/src/rules/ruff/rules/pairwise_over_zipped.rs b/crates/ruff/src/rules/ruff/rules/pairwise_over_zipped.rs index 99c2286a75..ee3c5b0d0c 100644 --- a/crates/ruff/src/rules/ruff/rules/pairwise_over_zipped.rs +++ b/crates/ruff/src/rules/ruff/rules/pairwise_over_zipped.rs @@ -115,7 +115,7 @@ pub(crate) fn pairwise_over_zipped(checker: &mut Checker, func: &Expr, args: &[E }; // Require second argument to be a `Subscript`. - if !matches!(&args[1], Expr::Subscript(_)) { + if !args[1].is_subscript_expr() { return; } let Some(second_arg_info) = match_slice_info(&args[1]) else { diff --git a/crates/ruff_python_ast/src/helpers.rs b/crates/ruff_python_ast/src/helpers.rs index a205e027e1..8c3e08774a 100644 --- a/crates/ruff_python_ast/src/helpers.rs +++ b/crates/ruff_python_ast/src/helpers.rs @@ -1353,7 +1353,7 @@ impl<'a> SimpleCallArgs<'a> { ) -> Self { let args = args .into_iter() - .take_while(|arg| !matches!(arg, Expr::Starred(_))) + .take_while(|arg| !arg.is_starred_expr()) .collect(); let kwargs = keywords @@ -1404,7 +1404,7 @@ pub fn on_conditional_branch<'a>(parents: &mut impl Iterator) - range: _range, }) = parent { - if matches!(value.as_ref(), Expr::IfExp(_)) { + if value.is_if_exp_expr() { return true; } } @@ -1427,7 +1427,7 @@ pub fn is_unpacking_assignment(parent: &Stmt, child: &Expr) -> bool { match parent { Stmt::With(ast::StmtWith { items, .. }) => items.iter().any(|item| { if let Some(optional_vars) = &item.optional_vars { - if matches!(optional_vars.as_ref(), Expr::Tuple(_)) { + if optional_vars.is_tuple_expr() { if any_over_expr(optional_vars, &|expr| expr == child) { return true; }