Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): add break unit lint #35

Merged
merged 2 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions crates/cairo-lint-core/src/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,17 @@ 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())),
_ => return None,
};

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("break ();", "break;").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,3 +1,4 @@
pub mod breaks;
pub mod double_parens;
pub mod loops;
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 @@ -7,7 +7,7 @@ use cairo_lang_syntax::node::ast::Expr as AstExpr;
use cairo_lang_syntax::node::kind::SyntaxKind;
use cairo_lang_syntax::node::{TypedStablePtr, TypedSyntaxNode};

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

pub fn cairo_lint_plugin_suite() -> PluginSuite {
let mut suite = PluginSuite::default();
Expand All @@ -23,13 +23,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 @@ -79,6 +81,7 @@ impl AnalyzerPlugin for CairoLint {
&AstExpr::from_syntax_node(db.upcast(), node),
&mut diags,
),
SyntaxKind::StatementBreak => breaks::check_break(db.upcast(), node, &mut diags),
_ => continue,
}
}
Expand Down
95 changes: 95 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,95 @@
//! > Break inside of if

//! > cairo_code
fn main() {
let mut a = 1_u32;
loop {
if a == 10 {
break ();
}
a += 1;
}
}

//! > diagnostics
warning: Plugin diagnostic: unnecessary double parentheses found after break. Consider removing them.
--> lib.cairo:8:13
|
8 | break ();
| ---------
|

//! > fixed
fn main() {
let mut a = 1_u32;
loop {
if a == 10 {
break;
}
a += 1;
}
}

//! > ==========================================================================

//! > Break inside of if with comment

//! > cairo_code
fn main() {
let mut a = 1_u32;
loop {
if a == 10 {
// this is a break
break ();
// this was a break
}
a += 1;
}
}

//! > diagnostics
warning: Plugin diagnostic: unnecessary double parentheses found after break. Consider removing them.
--> lib.cairo:10:13
|
10 | break ();
| ---------
|

//! > fixed
fn main() {
let mut a = 1_u32;
loop {
if a == 10 {
// this is a break
break;
// this was a break
}
a += 1;
}
}

//! > ==========================================================================

//! > Simple break

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

//! > diagnostics
warning: Plugin diagnostic: unnecessary double parentheses found after break. Consider removing them.
--> lib.cairo:4:8
|
4 | 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 @@ -71,3 +71,5 @@ test_file!(
"double parens in match arm"
);
test_file!(loops, loop_match_pop_front, "simple loop match pop front");

test_file!(breaks, breaks, "Simple break", "Break inside of if", "Break inside of if with comment");
Loading