From b42a8972bf102aaedd32d1ae52119dc82ae7b836 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Fri, 29 Sep 2023 13:28:50 -0400 Subject: [PATCH] Use `Expr::is_*` methods in more matches (#7714) --- .../flake8_bugbear/rules/useless_comparison.rs | 5 ++--- .../flake8_bugbear/rules/useless_expression.rs | 7 +++---- .../src/rules/flake8_simplify/rules/ast_bool_op.rs | 2 +- .../ruff_linter/src/rules/pandas_vet/rules/attr.rs | 13 +++++++------ .../pyflakes/rules/f_string_missing_placeholders.rs | 6 +----- crates/ruff_python_formatter/src/expression/mod.rs | 2 +- 6 files changed, 15 insertions(+), 20 deletions(-) diff --git a/crates/ruff_linter/src/rules/flake8_bugbear/rules/useless_comparison.rs b/crates/ruff_linter/src/rules/flake8_bugbear/rules/useless_comparison.rs index 698f94e6f5..5b41054e4a 100644 --- a/crates/ruff_linter/src/rules/flake8_bugbear/rules/useless_comparison.rs +++ b/crates/ruff_linter/src/rules/flake8_bugbear/rules/useless_comparison.rs @@ -1,7 +1,6 @@ -use ruff_python_ast::Expr; - use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; +use ruff_python_ast::Expr; use ruff_text_size::Ranged; use crate::checkers::ast::Checker; @@ -41,7 +40,7 @@ impl Violation for UselessComparison { /// B015 pub(crate) fn useless_comparison(checker: &mut Checker, expr: &Expr) { - if matches!(expr, Expr::Compare(_)) { + if expr.is_compare_expr() { checker .diagnostics .push(Diagnostic::new(UselessComparison, expr.range())); diff --git a/crates/ruff_linter/src/rules/flake8_bugbear/rules/useless_expression.rs b/crates/ruff_linter/src/rules/flake8_bugbear/rules/useless_expression.rs index 5b98107290..57521418ea 100644 --- a/crates/ruff_linter/src/rules/flake8_bugbear/rules/useless_expression.rs +++ b/crates/ruff_linter/src/rules/flake8_bugbear/rules/useless_expression.rs @@ -1,8 +1,7 @@ -use ruff_python_ast::{self as ast, Constant, Expr}; - use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::helpers::contains_effect; +use ruff_python_ast::{self as ast, Constant, Expr}; use ruff_text_size::Ranged; use crate::checkers::ast::Checker; @@ -48,7 +47,7 @@ impl Violation for UselessExpression { /// B018 pub(crate) fn useless_expression(checker: &mut Checker, value: &Expr) { // Ignore comparisons, as they're handled by `useless_comparison`. - if matches!(value, Expr::Compare(_)) { + if value.is_compare_expr() { return; } @@ -68,7 +67,7 @@ pub(crate) fn useless_expression(checker: &mut Checker, value: &Expr) { if contains_effect(value, |id| checker.semantic().is_builtin(id)) { // Flag attributes as useless expressions, even if they're attached to calls or other // expressions. - if matches!(value, Expr::Attribute(_)) { + if value.is_attribute_expr() { checker.diagnostics.push(Diagnostic::new( UselessExpression { kind: Kind::Attribute, diff --git a/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_bool_op.rs b/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_bool_op.rs index b559813b7f..4d59f96b72 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_bool_op.rs +++ b/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_bool_op.rs @@ -476,7 +476,7 @@ fn match_eq_target(expr: &Expr) -> Option<(&str, &Expr)> { let [comparator] = comparators.as_slice() else { return None; }; - if !matches!(&comparator, Expr::Name(_)) { + if !comparator.is_name_expr() { return None; } Some((id, comparator)) diff --git a/crates/ruff_linter/src/rules/pandas_vet/rules/attr.rs b/crates/ruff_linter/src/rules/pandas_vet/rules/attr.rs index 9d54b58cd7..e59b8228a5 100644 --- a/crates/ruff_linter/src/rules/pandas_vet/rules/attr.rs +++ b/crates/ruff_linter/src/rules/pandas_vet/rules/attr.rs @@ -1,8 +1,7 @@ -use ruff_python_ast::{self as ast, Expr, ExprContext}; - use ruff_diagnostics::Violation; use ruff_diagnostics::{Diagnostic, DiagnosticKind}; use ruff_macros::{derive_message_formats, violation}; +use ruff_python_ast::{self as ast, Expr, ExprContext}; use ruff_text_size::Ranged; use crate::checkers::ast::Checker; @@ -60,10 +59,12 @@ pub(crate) fn attr(checker: &mut Checker, attribute: &ast::ExprAttribute) { }; // Avoid flagging on function calls (e.g., `df.values()`). - if let Some(parent) = checker.semantic().current_expression_parent() { - if matches!(parent, Expr::Call(_)) { - return; - } + if checker + .semantic() + .current_expression_parent() + .is_some_and(Expr::is_call_expr) + { + return; } // Avoid flagging on non-DataFrames (e.g., `{"a": 1}.values`), and on irrelevant bindings diff --git a/crates/ruff_linter/src/rules/pyflakes/rules/f_string_missing_placeholders.rs b/crates/ruff_linter/src/rules/pyflakes/rules/f_string_missing_placeholders.rs index f3806283e1..4614cbe4f9 100644 --- a/crates/ruff_linter/src/rules/pyflakes/rules/f_string_missing_placeholders.rs +++ b/crates/ruff_linter/src/rules/pyflakes/rules/f_string_missing_placeholders.rs @@ -103,11 +103,7 @@ fn fstring_prefix_and_tok_range<'a>( /// F541 pub(crate) fn f_string_missing_placeholders(fstring: &ast::ExprFString, checker: &mut Checker) { - if !fstring - .values - .iter() - .any(|value| matches!(value, Expr::FormattedValue(_))) - { + if !fstring.values.iter().any(Expr::is_formatted_value_expr) { for (prefix_range, tok_range) in fstring_prefix_and_tok_range(fstring, checker.locator(), checker.source_type) { diff --git a/crates/ruff_python_formatter/src/expression/mod.rs b/crates/ruff_python_formatter/src/expression/mod.rs index f079159054..4a8f01f6a2 100644 --- a/crates/ruff_python_formatter/src/expression/mod.rs +++ b/crates/ruff_python_formatter/src/expression/mod.rs @@ -361,7 +361,7 @@ fn can_omit_optional_parentheses(expr: &Expr, context: &PyFormatContext) -> bool } else { fn is_parenthesized(expr: &Expr, context: &PyFormatContext) -> bool { // Don't break subscripts except in parenthesized context. It looks weird. - !matches!(expr, Expr::Subscript(_)) + !expr.is_subscript_expr() && has_parentheses(expr, context).is_some_and(OwnParentheses::is_non_empty) }