mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 11:59:10 +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_diagnostics::{Diagnostic, Violation};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
|
use ruff_python_ast::Expr;
|
||||||
use ruff_text_size::Ranged;
|
use ruff_text_size::Ranged;
|
||||||
|
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
|
@ -41,7 +40,7 @@ impl Violation for UselessComparison {
|
||||||
|
|
||||||
/// B015
|
/// B015
|
||||||
pub(crate) fn useless_comparison(checker: &mut Checker, expr: &Expr) {
|
pub(crate) fn useless_comparison(checker: &mut Checker, expr: &Expr) {
|
||||||
if matches!(expr, Expr::Compare(_)) {
|
if expr.is_compare_expr() {
|
||||||
checker
|
checker
|
||||||
.diagnostics
|
.diagnostics
|
||||||
.push(Diagnostic::new(UselessComparison, expr.range()));
|
.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_diagnostics::{Diagnostic, Violation};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_ast::helpers::contains_effect;
|
use ruff_python_ast::helpers::contains_effect;
|
||||||
|
use ruff_python_ast::{self as ast, Constant, Expr};
|
||||||
use ruff_text_size::Ranged;
|
use ruff_text_size::Ranged;
|
||||||
|
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
|
@ -48,7 +47,7 @@ impl Violation for UselessExpression {
|
||||||
/// B018
|
/// B018
|
||||||
pub(crate) fn useless_expression(checker: &mut Checker, value: &Expr) {
|
pub(crate) fn useless_expression(checker: &mut Checker, value: &Expr) {
|
||||||
// Ignore comparisons, as they're handled by `useless_comparison`.
|
// Ignore comparisons, as they're handled by `useless_comparison`.
|
||||||
if matches!(value, Expr::Compare(_)) {
|
if value.is_compare_expr() {
|
||||||
return;
|
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)) {
|
if contains_effect(value, |id| checker.semantic().is_builtin(id)) {
|
||||||
// Flag attributes as useless expressions, even if they're attached to calls or other
|
// Flag attributes as useless expressions, even if they're attached to calls or other
|
||||||
// expressions.
|
// expressions.
|
||||||
if matches!(value, Expr::Attribute(_)) {
|
if value.is_attribute_expr() {
|
||||||
checker.diagnostics.push(Diagnostic::new(
|
checker.diagnostics.push(Diagnostic::new(
|
||||||
UselessExpression {
|
UselessExpression {
|
||||||
kind: Kind::Attribute,
|
kind: Kind::Attribute,
|
||||||
|
|
|
@ -476,7 +476,7 @@ fn match_eq_target(expr: &Expr) -> Option<(&str, &Expr)> {
|
||||||
let [comparator] = comparators.as_slice() else {
|
let [comparator] = comparators.as_slice() else {
|
||||||
return None;
|
return None;
|
||||||
};
|
};
|
||||||
if !matches!(&comparator, Expr::Name(_)) {
|
if !comparator.is_name_expr() {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
Some((id, comparator))
|
Some((id, comparator))
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
use ruff_python_ast::{self as ast, Expr, ExprContext};
|
|
||||||
|
|
||||||
use ruff_diagnostics::Violation;
|
use ruff_diagnostics::Violation;
|
||||||
use ruff_diagnostics::{Diagnostic, DiagnosticKind};
|
use ruff_diagnostics::{Diagnostic, DiagnosticKind};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
|
use ruff_python_ast::{self as ast, Expr, ExprContext};
|
||||||
use ruff_text_size::Ranged;
|
use ruff_text_size::Ranged;
|
||||||
|
|
||||||
use crate::checkers::ast::Checker;
|
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()`).
|
// Avoid flagging on function calls (e.g., `df.values()`).
|
||||||
if let Some(parent) = checker.semantic().current_expression_parent() {
|
if checker
|
||||||
if matches!(parent, Expr::Call(_)) {
|
.semantic()
|
||||||
|
.current_expression_parent()
|
||||||
|
.is_some_and(Expr::is_call_expr)
|
||||||
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Avoid flagging on non-DataFrames (e.g., `{"a": 1}.values`), and on irrelevant bindings
|
// Avoid flagging on non-DataFrames (e.g., `{"a": 1}.values`), and on irrelevant bindings
|
||||||
// (like imports).
|
// (like imports).
|
||||||
|
|
|
@ -103,11 +103,7 @@ fn fstring_prefix_and_tok_range<'a>(
|
||||||
|
|
||||||
/// F541
|
/// F541
|
||||||
pub(crate) fn f_string_missing_placeholders(fstring: &ast::ExprFString, checker: &mut Checker) {
|
pub(crate) fn f_string_missing_placeholders(fstring: &ast::ExprFString, checker: &mut Checker) {
|
||||||
if !fstring
|
if !fstring.values.iter().any(Expr::is_formatted_value_expr) {
|
||||||
.values
|
|
||||||
.iter()
|
|
||||||
.any(|value| matches!(value, Expr::FormattedValue(_)))
|
|
||||||
{
|
|
||||||
for (prefix_range, tok_range) in
|
for (prefix_range, tok_range) in
|
||||||
fstring_prefix_and_tok_range(fstring, checker.locator(), checker.source_type)
|
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 {
|
} else {
|
||||||
fn is_parenthesized(expr: &Expr, context: &PyFormatContext) -> bool {
|
fn is_parenthesized(expr: &Expr, context: &PyFormatContext) -> bool {
|
||||||
// Don't break subscripts except in parenthesized context. It looks weird.
|
// 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)
|
&& has_parentheses(expr, context).is_some_and(OwnParentheses::is_non_empty)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue