-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
66 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod operator; | ||
pub mod single_match; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use cairo_lang_defs::plugin::PluginDiagnostic; | ||
use cairo_lang_diagnostics::Severity; | ||
use cairo_lang_syntax::node::ast::{BinaryOperator, Expr, ExprBinary}; | ||
use cairo_lang_syntax::node::db::SyntaxGroup; | ||
use cairo_lang_syntax::node::{Terminal, TypedStablePtr, TypedSyntaxNode}; | ||
|
||
pub const ERASE_OP: &str = "This operation will always result in zero and can be simplified."; | ||
|
||
pub fn check_expr(db: &dyn SyntaxGroup, expr: &ExprBinary) -> Option<PluginDiagnostic> { | ||
let lhs = expr.lhs(db); | ||
let rhs = expr.rhs(db); | ||
|
||
if is_zero_literal(db, &lhs) || is_zero_literal(db, &rhs) { | ||
let op = expr.op(db); | ||
if matches!(op, BinaryOperator::Mul(_) | BinaryOperator::Div(_) | BinaryOperator::And(_)) { | ||
return Some(PluginDiagnostic { | ||
stable_ptr: expr.stable_ptr().untyped(), | ||
message: ERASE_OP.to_string(), | ||
severity: Severity::Error, | ||
}); | ||
} | ||
} | ||
None | ||
} | ||
|
||
fn is_zero_literal(db: &dyn SyntaxGroup, expr: &Expr) -> bool { | ||
matches!(expr, Expr::Literal(lit) if lit.text(db) == "0") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
33 changes: 33 additions & 0 deletions
33
crates/cairo-lint-core/tests/test_files/operator/erasing_op
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
//! > always in zero | ||
|
||
//! > cairo_code | ||
fn main() { | ||
let x: u32 = 1; | ||
let _result = 0 * x; | ||
let _result = 0 / x; | ||
let _result = x & 0; | ||
} | ||
|
||
//! > diagnostics | ||
warning: Plugin diagnostic: This operation will always result in zero and can be simplified. | ||
--> lib.cairo:3:19 | ||
let _result = 0 * x; | ||
^***^ | ||
|
||
warning: Plugin diagnostic: This operation will always result in zero and can be simplified. | ||
--> lib.cairo:4:19 | ||
let _result = 0 / x; | ||
^***^ | ||
|
||
warning: Plugin diagnostic: This operation will always result in zero and can be simplified. | ||
--> lib.cairo:5:19 | ||
let _result = x & 0; | ||
^***^ | ||
|
||
//! > fixed | ||
fn main() { | ||
let x: u32 = 1; | ||
let _result = 0 * x; | ||
let _result = 0 / x; | ||
let _result = x & 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters