Skip to content

Commit

Permalink
chore(fmt): resolve unary operator formatting issue (noir-lang#3157)
Browse files Browse the repository at this point in the history
  • Loading branch information
kek kek kek authored and Sakapoi committed Oct 19, 2023
1 parent 949e56b commit f0ad8ee
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
17 changes: 15 additions & 2 deletions tooling/nargo_fmt/src/visitor/expr.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions tooling/nargo_fmt/tests/expected/unary_operators.nr
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ fn main() {
-1;
-/*test*/1;
-/*test*/1;

&mut 1;
*v;
***v;
}
4 changes: 4 additions & 0 deletions tooling/nargo_fmt/tests/input/unary_operators.nr
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ fn main() {
-1;
-/*test*/1;
-/*test*/1;

& mut 1;
* v;
* * * v;
}

0 comments on commit f0ad8ee

Please sign in to comment.