Format Attribute Expression (#5259)

This commit is contained in:
Micha Reiser 2023-06-21 23:33:53 +02:00 committed by GitHub
parent 341b12d918
commit ccf34aae8c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 394 additions and 329 deletions

View file

@ -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.