Format if … else if … without superfluous braces (#2573)

This commit is contained in:
Casey Rodarmor 2025-01-13 17:52:30 -08:00 committed by GitHub
parent 2a3018a529
commit abec307f69
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 30 additions and 7 deletions

View file

@ -72,7 +72,13 @@ impl Display for Expression<'_> {
condition,
then,
otherwise,
} => write!(f, "if {condition} {{ {then} }} else {{ {otherwise} }}"),
} => {
if let Self::Conditional { .. } = **otherwise {
write!(f, "if {condition} {{ {then} }} else {otherwise}")
} else {
write!(f, "if {condition} {{ {then} }} else {{ {otherwise} }}")
}
}
Self::Group { contents } => write!(f, "({contents})"),
Self::Join { lhs: None, rhs } => write!(f, "/ {rhs}"),
Self::Join {

View file

@ -259,7 +259,7 @@ impl<'src> Lexer<'src> {
/// True if `text` could be an identifier
pub(crate) fn is_identifier(text: &str) -> bool {
if !text.chars().next().map_or(false, Self::is_identifier_start) {
if !text.chars().next().is_some_and(Self::is_identifier_start) {
return false;
}

View file

@ -942,7 +942,7 @@ impl<'run, 'src> Parser<'run, 'src> {
let body = self.parse_body()?;
let shebang = body.first().map_or(false, Line::is_shebang);
let shebang = body.first().is_some_and(Line::is_shebang);
let script = attributes.contains(AttributeDiscriminant::Script);
if shebang && script {
@ -1041,7 +1041,7 @@ impl<'run, 'src> Parser<'run, 'src> {
}
}
while lines.last().map_or(false, Line::is_empty) {
while lines.last().is_some_and(Line::is_empty) {
lines.pop();
}

View file

@ -204,11 +204,11 @@ impl<'src, D> Recipe<'src, D> {
}
let mut evaluated = String::new();
let mut continued = false;
let quiet_line = lines.peek().map_or(false, |line| line.is_quiet());
let infallible_line = lines.peek().map_or(false, |line| line.is_infallible());
let quiet_line = lines.peek().is_some_and(|line| line.is_quiet());
let infallible_line = lines.peek().is_some_and(|line| line.is_infallible());
let comment_line = context.module.settings.ignore_comments
&& lines.peek().map_or(false, |line| line.is_comment());
&& lines.peek().is_some_and(|line| line.is_comment());
loop {
if lines.peek().is_none() {

View file

@ -1138,3 +1138,20 @@ fn unchanged_justfiles_are_not_written_to_disk() {
.args(["--fmt", "--unstable"])
.run();
}
#[test]
fn if_else() {
Test::new()
.justfile(
"
x := if '' == '' { '' } else if '' == '' { '' } else { '' }
",
)
.arg("--dump")
.stdout(
"
x := if '' == '' { '' } else if '' == '' { '' } else { '' }
",
)
.run();
}