Skip to content

Commit

Permalink
feat(core): add break unit lint
Browse files Browse the repository at this point in the history
  • Loading branch information
0xLucqs committed Aug 20, 2024
1 parent b9a1161 commit 1f02759
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 1 deletion.
6 changes: 6 additions & 0 deletions crates/cairo-lint-core/src/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,18 @@ impl Fixer {
self.fix_double_parens(db.upcast(), plugin_diag.stable_ptr.lookup(db.upcast()))
}
CairoLintKind::DestructMatch => self.fix_destruct_match(db, plugin_diag.stable_ptr.lookup(db.upcast())),
CairoLintKind::BreakUnit => self.fix_break_unit(db, plugin_diag.stable_ptr.lookup(db.upcast())),

_ => "".to_owned(),
};

Some((semantic_diag.stable_location.syntax_node(db.upcast()), new_text))
}

pub fn fix_break_unit(&self, db: &dyn SyntaxGroup, node: SyntaxNode) -> String {
node.get_text(db).replace(" ()", "").to_string()
}

/// Removes unnecessary double parentheses from a syntax node.
///
/// Simplifies an expression by stripping extra layers of parentheses while preserving
Expand Down
16 changes: 16 additions & 0 deletions crates/cairo-lint-core/src/lints/breaks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use cairo_lang_defs::plugin::PluginDiagnostic;
use cairo_lang_diagnostics::Severity;
use cairo_lang_syntax::node::db::SyntaxGroup;
use cairo_lang_syntax::node::SyntaxNode;

pub const BREAK_UNIT: &str = "unnecessary double parentheses found after break. Consider removing them.";

pub fn check_break(db: &dyn SyntaxGroup, node: SyntaxNode, diagnostics: &mut Vec<PluginDiagnostic>) {
if node.clone().get_text_without_trivia(db).ends_with("();") {
diagnostics.push(PluginDiagnostic {
stable_ptr: node.stable_ptr(),
message: BREAK_UNIT.to_string(),
severity: Severity::Warning,
});
}
}
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,2 +1,3 @@
pub mod breaks;
pub mod double_parens;
pub mod single_match;
5 changes: 4 additions & 1 deletion crates/cairo-lint-core/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use cairo_lang_syntax::node::ast::{Expr, ExprMatch};
use cairo_lang_syntax::node::kind::SyntaxKind;
use cairo_lang_syntax::node::TypedSyntaxNode;

use crate::lints::{double_parens, single_match};
use crate::lints::{breaks, double_parens, single_match};

pub fn cairo_lint_plugin_suite() -> PluginSuite {
let mut suite = PluginSuite::default();
Expand All @@ -22,13 +22,15 @@ pub enum CairoLintKind {
MatchForEquality,
DoubleParens,
Unknown,
BreakUnit,
}

pub fn diagnostic_kind_from_message(message: &str) -> CairoLintKind {
match message {
single_match::DESTRUCT_MATCH => CairoLintKind::DestructMatch,
single_match::MATCH_FOR_EQUALITY => CairoLintKind::MatchForEquality,
double_parens::DOUBLE_PARENS => CairoLintKind::DoubleParens,
breaks::BREAK_UNIT => CairoLintKind::BreakUnit,
_ => CairoLintKind::Unknown,
}
}
Expand Down Expand Up @@ -59,6 +61,7 @@ impl AnalyzerPlugin for CairoLint {
&mut diags,
),
SyntaxKind::ItemExternFunction => (),
SyntaxKind::StatementBreak => breaks::check_break(db.upcast(), descendant, &mut diags),
_ => (),
}
}
Expand Down
21 changes: 21 additions & 0 deletions crates/cairo-lint-core/tests/test_files/breaks/breaks
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//! > Simple break

//! > cairo_code
fn main() {
loop {
break ();
}
}

//! > diagnostics
warning: Plugin diagnostic: unnecessary double parentheses found after break. Consider removing them.
--> lib.cairo:3:9
break ();
^*******^

//! > fixed
fn main() {
loop {
break;
}
}
2 changes: 2 additions & 0 deletions crates/cairo-lint-core/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,5 @@ test_file!(
"double parens in struct field access",
"double parens in match arm"
);

test_file!(breaks, breaks, "Simple break");

0 comments on commit 1f02759

Please sign in to comment.