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: Add manual assert #101

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Merge branch 'main' into feat/manual_assert
  • Loading branch information
stevencartavia committed Sep 25, 2024
commit 9c63b5f4aa77964d771f9789107637bc4dbb1dbf
15 changes: 15 additions & 0 deletions crates/cairo-lint-core/src/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use crate::plugin::{diagnostic_kind_from_message, CairoLintKind};

mod import_fixes;
pub use import_fixes::{apply_import_fixes, collect_unused_imports, ImportFix};
mod helper;
use helper::{invert_condition, remove_break_from_block, remove_break_from_else_clause};

/// Represents a fix for a diagnostic, containing the span of code to be replaced
/// and the suggested replacement.
Expand Down Expand Up @@ -183,6 +185,7 @@ impl Fixer {
CairoLintKind::LoopForWhile => self.fix_loop_break(db.upcast(), plugin_diag.stable_ptr.lookup(db.upcast())),
CairoLintKind::ManualOkOr => self.fix_manual_ok_or(db, plugin_diag.stable_ptr.lookup(db.upcast())),
CairoLintKind::ManualAssert => self.fix_if_then_panic(db, plugin_diag.stable_ptr.lookup(db.upcast())),
CairoLintKind::ManualIsSome => self.fix_manual_is_some(db, plugin_diag.stable_ptr.lookup(db.upcast())),
_ => return None,
};
Some((semantic_diag.stable_location.syntax_node(db.upcast()), new_text))
Expand Down Expand Up @@ -489,6 +492,18 @@ impl Fixer {
format!("{option_var_name}.ok_or({none_arm_err})")
}

/// Rewrites a manual implementation of is_some
pub fn fix_manual_is_some(&self, db: &dyn SyntaxGroup, node: SyntaxNode) -> String {
let expr_match = ExprMatch::from_syntax_node(db, node.clone());

let option_var_name = match expr_match.expr(db) {
Expr::Path(path_expr) => path_expr.as_syntax_node().get_text_without_trivia(db),
_ => panic!("Expected a variable or path in match expression"),
};

format!("{option_var_name}.is_some()")
}

pub fn fix_if_then_panic(&self, db: &dyn SyntaxGroup, node: SyntaxNode) -> String {
let if_expr = ExprIf::from_syntax_node(db, node.clone());
let condition_text = if_expr.condition(db).as_syntax_node().get_text_without_trivia(db);
Expand Down
4 changes: 3 additions & 1 deletion crates/cairo-lint-core/src/lints/manual/manual_ok_or.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use cairo_lang_defs::plugin::PluginDiagnostic;
use cairo_lang_diagnostics::Severity;
use cairo_lang_syntax::node::ast::{Expr, ExprMatch, Pattern};
use cairo_lang_syntax::node::ast::ExprMatch;
use cairo_lang_syntax::node::db::SyntaxGroup;
use cairo_lang_syntax::node::TypedSyntaxNode;

use crate::lints::manual::{check_manual, ManualLint};

pub const MANUAL_OK_OR: &str = "Manual match for Option<T> detected. Consider using ok_or instead";

pub fn check_manual_ok_or(db: &dyn SyntaxGroup, expr_match: &ExprMatch, diagnostics: &mut Vec<PluginDiagnostic>) {
Expand Down
1 change: 1 addition & 0 deletions crates/cairo-lint-core/src/lints/manual/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod manual_assert;
pub mod manual_is_some;
pub mod manual_ok_or;

use cairo_lang_syntax::node::ast::{Expr, ExprMatch, Pattern};
Expand Down
6 changes: 5 additions & 1 deletion crates/cairo-lint-core/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use cairo_lang_defs::plugin::PluginDiagnostic;
use cairo_lang_semantic::db::SemanticGroup;
use cairo_lang_semantic::plugin::{AnalyzerPlugin, PluginSuite};
use cairo_lang_semantic::Expr;
use cairo_lang_syntax::node::ast::{ElseClause, Expr as AstExpr, ExprBinary, ExprIf, ExprMatch};
use cairo_lang_syntax::node::ast::{ElseClause, Expr as AstExpr, ExprBinary, ExprIf, ExprLoop, ExprMatch};
use cairo_lang_syntax::node::kind::SyntaxKind;
use cairo_lang_syntax::node::{TypedStablePtr, TypedSyntaxNode};

Expand Down Expand Up @@ -35,6 +35,7 @@ pub enum CairoLintKind {
DuplicateUnderscoreArgs,
LoopMatchPopFront,
ManualAssert,
LoopForWhile,
Unknown,
Panic,
ErasingOperation,
Expand All @@ -57,6 +58,9 @@ pub fn diagnostic_kind_from_message(message: &str) -> CairoLintKind {
duplicate_underscore_args::DUPLICATE_UNDERSCORE_ARGS => CairoLintKind::DuplicateUnderscoreArgs,
loops::LOOP_MATCH_POP_FRONT => CairoLintKind::LoopMatchPopFront,
manual_assert::MANUAL_ASSERT => CairoLintKind::ManualAssert,
panic::PANIC_IN_CODE => CairoLintKind::Panic,
loop_for_while::LOOP_FOR_WHILE => CairoLintKind::LoopForWhile,
erasing_op::ERASING_OPERATION => CairoLintKind::ErasingOperation,
manual_ok_or::MANUAL_OK_OR => CairoLintKind::ManualOkOr,
manual_is_some::MANUAL_IS_SOME => CairoLintKind::ManualIsSome,
_ => CairoLintKind::Unknown,
Expand Down
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 @@ -197,4 +197,6 @@ test_file!(
"test with comment in Some"
);

test_file!(manual, manual_is_some, "test basic is some", "test with comment in Some", "test with comment in None");

test_file!(manual, manual_assert, "check-if-then", "check-if-then2");
You are viewing a condensed version of this merge commit. You can view the full changes here.