From fc32a37d8b219c6efcaaaa397b77a9ad157baa17 Mon Sep 17 00:00:00 2001 From: Marcos Grzesiak Date: Wed, 16 Oct 2024 20:34:44 -0400 Subject: [PATCH] add anti math style warnings --- src/compile/modifier.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/compile/modifier.rs b/src/compile/modifier.rs index 12323029b..024cf6532 100644 --- a/src/compile/modifier.rs +++ b/src/compile/modifier.rs @@ -932,6 +932,34 @@ impl Compiler { ); } + // Anti math diagnostic + if let [Instr::Prim(prim, _)] = new_func.instrs.as_slice() { + for (a, b) in [ + (Primitive::Add, Primitive::Sub), + (Primitive::Mul, Primitive::Div), + ] { + if *prim == a { + self.emit_diagnostic( + format!( + "Prefer `{b}` over `{}{prim}` for clarity", + Primitive::Anti + ), + DiagnosticKind::Style, + modified.modifier.span.clone().merge(span.clone()), + ); + } else if *prim == b { + self.emit_diagnostic( + format!( + "Prefer `{a}` over `{}{prim}` for clarity", + Primitive::Anti + ), + DiagnosticKind::Style, + modified.modifier.span.clone().merge(span.clone()), + ); + } + } + } + match anti_instrs(&new_func.instrs, self) { Ok(inverted) => { let sig = self.sig_of(&inverted, &span)?;