Skip to content

Commit

Permalink
Use Expr::is_* methods in more matches (#7714)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Sep 29, 2023
1 parent bb65fb8 commit b42a897
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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()));
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
13 changes: 7 additions & 6 deletions crates/ruff_linter/src/rules/pandas_vet/rules/attr.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_python_formatter/src/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit b42a897

Please sign in to comment.