feat(tvix/eval): skip & warn for useless parenthesis
Change-Id: I567ca0682012b9d09f1217e57a104ac5671f8d82 Reviewed-on: https://cl.tvl.fyi/c/depot/+/7771 Tested-by: BuildkiteCI Reviewed-by: raitobezarius <tvl@lahfa.xyz> Reviewed-by: flokli <flokli@flokli.de>
This commit is contained in:
parent
aadf71a6ed
commit
87c80895cd
2 changed files with 32 additions and 0 deletions
|
|
@ -9,6 +9,7 @@ use ast::Expr;
|
|||
pub(super) fn optimise_expr(c: &mut Compiler, slot: LocalIdx, expr: ast::Expr) -> ast::Expr {
|
||||
match expr {
|
||||
Expr::BinOp(_) => optimise_bin_op(c, slot, expr),
|
||||
Expr::Paren(_) => optimise_paren(c, expr),
|
||||
_ => expr.to_owned(),
|
||||
}
|
||||
}
|
||||
|
|
@ -123,3 +124,28 @@ fn optimise_bin_op(c: &mut Compiler, slot: LocalIdx, expr: ast::Expr) -> ast::Ex
|
|||
|
||||
expr
|
||||
}
|
||||
|
||||
/// Detect useless parenthesis around primitive expressions.
|
||||
fn optimise_paren(c: &mut Compiler, expr: ast::Expr) -> ast::Expr {
|
||||
if let Expr::Paren(inner) = &expr {
|
||||
let inner = inner.expr().unwrap();
|
||||
|
||||
if let Expr::Paren(_) = &inner {
|
||||
c.emit_warning(&expr, WarningKind::UselessParens);
|
||||
return optimise_paren(c, inner);
|
||||
}
|
||||
|
||||
if let Expr::Literal(_)
|
||||
| Expr::Str(_)
|
||||
| Expr::Select(_)
|
||||
| Expr::List(_)
|
||||
| Expr::AttrSet(_)
|
||||
| Expr::Ident(_) = &inner
|
||||
{
|
||||
c.emit_warning(&expr, WarningKind::UselessParens);
|
||||
return inner;
|
||||
}
|
||||
}
|
||||
|
||||
expr
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue