mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-27 12:29:28 +00:00
Cover Black's is_aritmetic_like
formatting (#5738)
This commit is contained in:
parent
513de13c46
commit
8187bf9f7e
14 changed files with 159 additions and 96 deletions
|
@ -1,4 +1,5 @@
|
|||
use rustpython_parser::ast::StmtExpr;
|
||||
use rustpython_parser::ast;
|
||||
use rustpython_parser::ast::{Expr, Operator, StmtExpr};
|
||||
|
||||
use crate::expression::maybe_parenthesize_expression;
|
||||
use crate::expression::parentheses::Parenthesize;
|
||||
|
@ -12,6 +13,25 @@ impl FormatNodeRule<StmtExpr> for FormatStmtExpr {
|
|||
fn fmt_fields(&self, item: &StmtExpr, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
let StmtExpr { value, .. } = item;
|
||||
|
||||
maybe_parenthesize_expression(value, item, Parenthesize::Optional).fmt(f)
|
||||
if is_arithmetic_like(value) {
|
||||
maybe_parenthesize_expression(value, item, Parenthesize::Optional).fmt(f)
|
||||
} else {
|
||||
value.format().fmt(f)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const fn is_arithmetic_like(expression: &Expr) -> bool {
|
||||
matches!(
|
||||
expression,
|
||||
Expr::BinOp(ast::ExprBinOp {
|
||||
op: Operator::BitOr
|
||||
| Operator::BitXor
|
||||
| Operator::LShift
|
||||
| Operator::RShift
|
||||
| Operator::Add
|
||||
| Operator::Sub,
|
||||
..
|
||||
})
|
||||
)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue