From f0ad8ee9e1db5c722192ce8e7f218d677d36c5fa Mon Sep 17 00:00:00 2001 From: kek kek kek Date: Fri, 13 Oct 2023 15:35:04 -0700 Subject: [PATCH] chore(fmt): resolve unary operator formatting issue (#3157) --- tooling/nargo_fmt/src/visitor/expr.rs | 17 +++++++++++++++-- .../nargo_fmt/tests/expected/unary_operators.nr | 4 ++++ .../nargo_fmt/tests/input/unary_operators.nr | 4 ++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/tooling/nargo_fmt/src/visitor/expr.rs b/tooling/nargo_fmt/src/visitor/expr.rs index d8bea6c7530..0c28bf46a51 100644 --- a/tooling/nargo_fmt/src/visitor/expr.rs +++ b/tooling/nargo_fmt/src/visitor/expr.rs @@ -1,6 +1,6 @@ use noirc_frontend::{ hir::resolution::errors::Span, lexer::Lexer, token::Token, ArrayLiteral, BlockExpression, - Expression, ExpressionKind, Literal, Statement, + Expression, ExpressionKind, Literal, Statement, UnaryOp, }; use super::FmtVisitor; @@ -27,7 +27,20 @@ impl FmtVisitor<'_> { visitor.buffer } ExpressionKind::Prefix(prefix) => { - format!("{}{}", prefix.operator, self.format_expr(prefix.rhs)) + let op = match prefix.operator { + UnaryOp::Minus => "-", + UnaryOp::Not => "!", + UnaryOp::MutableReference => "&mut ", + UnaryOp::Dereference { implicitly_added } => { + if implicitly_added { + "" + } else { + "*" + } + } + }; + + format!("{op}{}", self.format_expr(prefix.rhs)) } ExpressionKind::Cast(cast) => { format!("{} as {}", self.format_expr(cast.lhs), cast.r#type) diff --git a/tooling/nargo_fmt/tests/expected/unary_operators.nr b/tooling/nargo_fmt/tests/expected/unary_operators.nr index e65690cf482..1dd5e4ab945 100644 --- a/tooling/nargo_fmt/tests/expected/unary_operators.nr +++ b/tooling/nargo_fmt/tests/expected/unary_operators.nr @@ -2,4 +2,8 @@ fn main() { -1; -/*test*/1; -/*test*/1; + + &mut 1; + *v; + ***v; } diff --git a/tooling/nargo_fmt/tests/input/unary_operators.nr b/tooling/nargo_fmt/tests/input/unary_operators.nr index 62e4a1c4d27..e6d42456ef2 100644 --- a/tooling/nargo_fmt/tests/input/unary_operators.nr +++ b/tooling/nargo_fmt/tests/input/unary_operators.nr @@ -2,4 +2,8 @@ fn main() { -1; -/*test*/1; -/*test*/1; + + & mut 1; + * v; + * * * v; } \ No newline at end of file