Skip to content

Commit

Permalink
fix rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
0xLucqs committed Aug 19, 2024
1 parent 11c32f9 commit 0ef2efd
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 15 deletions.
1 change: 1 addition & 0 deletions crates/cairo-lint-core/src/lints/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod operator;
pub mod single_match;
28 changes: 28 additions & 0 deletions crates/cairo-lint-core/src/lints/operator.rs
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")
}
7 changes: 3 additions & 4 deletions crates/cairo-lint-core/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use cairo_lang_syntax::node::ast::{BinaryOperator, ExprBinary, ExprMatch};
use cairo_lang_syntax::node::kind::SyntaxKind;
use cairo_lang_syntax::node::{Terminal, TypedStablePtr, TypedSyntaxNode};

use crate::erasing_op::EraseOp;
use crate::lints::single_match;
use crate::lints::{operator, single_match};

pub fn cairo_lint_plugin_suite() -> PluginSuite {
let mut suite = PluginSuite::default();
Expand All @@ -30,7 +29,7 @@ pub fn diagnostic_kind_from_message(message: &str) -> CairoLintKind {
single_match::DESTRUCT_MATCH => CairoLintKind::DestructMatch,
single_match::MATCH_FOR_EQUALITY => CairoLintKind::MatchForEquality,
"operation can be simplifield to zero" => CairoLintKind::EraseOp,
CairoLint::ERASE_OP => CairoLintKind::EraseOp,
operator::ERASE_OP => CairoLintKind::EraseOp,
_ => CairoLintKind::Unknown,
}
}
Expand All @@ -57,7 +56,7 @@ impl AnalyzerPlugin for CairoLint {
),
SyntaxKind::ExprBinary => {
let binary_expr = ExprBinary::from_syntax_node(db.upcast(), descendant);
if let Some(diagnostic) = self.check_expr(db.upcast(), &binary_expr) {
if let Some(diagnostic) = operator::check_expr(db.upcast(), &binary_expr) {
diags.push(diagnostic);
}
}
Expand Down
10 changes: 0 additions & 10 deletions crates/cairo-lint-core/tests/test_files/erasing_op

This file was deleted.

33 changes: 33 additions & 0 deletions crates/cairo-lint-core/tests/test_files/operator/erasing_op
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;
}
2 changes: 1 addition & 1 deletion crates/cairo-lint-core/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ test_file!(
"simple destructuring match with comment in scope"
);

test_file!(erasing_op, "always in zero" );
test_file!(operator, erasing_op, "always in zero");

0 comments on commit 0ef2efd

Please sign in to comment.