Omit tuple parentheses in for statements except when absolutely necessary (#6765)

This commit is contained in:
Micha Reiser 2023-08-22 12:18:59 +02:00 committed by GitHub
parent fec6fc2fab
commit b52cc84df6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 96 additions and 3 deletions

View file

@ -8,7 +8,7 @@ use ruff_text_size::TextRange;
use crate::builders::parenthesize_if_expands;
use crate::comments::SourceComment;
use crate::expression::parentheses::{
empty_parenthesized, parenthesized, NeedsParentheses, OptionalParentheses,
empty_parenthesized, optional_parentheses, parenthesized, NeedsParentheses, OptionalParentheses,
};
use crate::prelude::*;
@ -138,7 +138,7 @@ impl FormatNodeRule<ExprTuple> for FormatExprTuple {
}
},
// If the tuple has parentheses, we generally want to keep them. The exception are for
// loops, see `TupleParentheses::StripInsideForLoop` doc comment.
// loops, see `TupleParentheses::NeverPreserve` doc comment.
//
// Unlike other expression parentheses, tuple parentheses are part of the range of the
// tuple itself.
@ -159,7 +159,12 @@ impl FormatNodeRule<ExprTuple> for FormatExprTuple {
.finish()
}
TupleParentheses::Preserve => group(&ExprSequence::new(item)).fmt(f),
_ => parenthesize_if_expands(&ExprSequence::new(item)).fmt(f),
TupleParentheses::NeverPreserve => {
optional_parentheses(&ExprSequence::new(item)).fmt(f)
}
TupleParentheses::Default => {
parenthesize_if_expands(&ExprSequence::new(item)).fmt(f)
}
},
}
}