mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-03 07:04:53 +00:00
Format Attribute Expression (#5259)
This commit is contained in:
parent
341b12d918
commit
ccf34aae8c
20 changed files with 394 additions and 329 deletions
|
@ -23,8 +23,12 @@ pub(super) fn default_expression_needs_parentheses(
|
|||
"Should only be called for expressions"
|
||||
);
|
||||
|
||||
#[allow(clippy::if_same_then_else)]
|
||||
if parenthesize.is_always() {
|
||||
Parentheses::Always
|
||||
}
|
||||
// `Optional` or `Preserve` and expression has parentheses in source code.
|
||||
if !parenthesize.is_if_breaks() && is_expression_parenthesized(node, source) {
|
||||
else if !parenthesize.is_if_breaks() && is_expression_parenthesized(node, source) {
|
||||
Parentheses::Always
|
||||
}
|
||||
// `Optional` or `IfBreaks`: Add parentheses if the expression doesn't fit on a line but enforce
|
||||
|
@ -53,9 +57,15 @@ pub enum Parenthesize {
|
|||
|
||||
/// Parenthesizes the expression only if it doesn't fit on a line.
|
||||
IfBreaks,
|
||||
|
||||
Always,
|
||||
}
|
||||
|
||||
impl Parenthesize {
|
||||
pub(crate) const fn is_always(self) -> bool {
|
||||
matches!(self, Parenthesize::Always)
|
||||
}
|
||||
|
||||
pub(crate) const fn is_if_breaks(self) -> bool {
|
||||
matches!(self, Parenthesize::IfBreaks)
|
||||
}
|
||||
|
@ -70,7 +80,8 @@ impl Parenthesize {
|
|||
/// whether there are parentheses in the source code or not.
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
||||
pub enum Parentheses {
|
||||
/// Always create parentheses
|
||||
/// Always set parentheses regardless if the expression breaks or if they were
|
||||
/// present in the source.
|
||||
Always,
|
||||
|
||||
/// Only add parentheses when necessary because the expression breaks over multiple lines.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue