mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 18:28:56 +00:00
Use Expr::is_*
methods in more matches (#7714)
This commit is contained in:
parent
bb65fb8486
commit
b42a8972bf
6 changed files with 15 additions and 20 deletions
|
@ -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()));
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -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,11 +59,13 @@ 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(_)) {
|
||||
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
|
||||
// (like imports).
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue