diff --git a/compiler/fmt/src/annotation.rs b/compiler/fmt/src/annotation.rs index 7fb5b13043..0d9e7d8ff4 100644 --- a/compiler/fmt/src/annotation.rs +++ b/compiler/fmt/src/annotation.rs @@ -18,7 +18,7 @@ use roc_region::all::Located; /// Just (Just a) /// List (List a) /// reverse (reverse l) -#[derive(PartialEq, Eq, Clone, Copy)] +#[derive(PartialEq, Eq, Clone, Copy, Debug)] pub enum Parens { NotNeeded, InFunctionType, diff --git a/compiler/fmt/src/expr.rs b/compiler/fmt/src/expr.rs index 44501f09e4..9160188699 100644 --- a/compiler/fmt/src/expr.rs +++ b/compiler/fmt/src/expr.rs @@ -133,9 +133,13 @@ impl<'a> Formattable<'a> for Expr<'a> { } } ParensAround(sub_expr) => { - buf.push('('); - sub_expr.format_with_options(buf, Parens::NotNeeded, Newlines::Yes, indent); - buf.push(')'); + if parens == Parens::NotNeeded { + sub_expr.format_with_options(buf, Parens::NotNeeded, Newlines::Yes, indent); + } else { + buf.push('('); + sub_expr.format_with_options(buf, Parens::NotNeeded, Newlines::Yes, indent); + buf.push(')'); + } } Str(literal) => { use roc_parse::ast::StrLiteral::*; diff --git a/compiler/fmt/tests/test_fmt.rs b/compiler/fmt/tests/test_fmt.rs index edf60c1696..10dbb6b90a 100644 --- a/compiler/fmt/tests/test_fmt.rs +++ b/compiler/fmt/tests/test_fmt.rs @@ -362,6 +362,31 @@ mod test_fmt { ); } + #[test] + fn excess_parens() { + expr_formats_to( + indoc!( + r#" + x = (5) + + + y = ((10)) + + 42 + "# + ), + indoc!( + r#" + x = 5 + + y = 10 + + 42 + "# + ), + ); + } + // #[test] // fn defs_with_defs() { // expr_formats_to(