Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
0xLucqs committed Aug 20, 2024
1 parent 855b77b commit 86b4a3d
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 2 deletions.
67 changes: 67 additions & 0 deletions crates/cairo-lint-core/src/lints/loops.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use cairo_lang_defs::ids::{FileIndex, ModuleFileId, ModuleId, NamedLanguageElementId};
use cairo_lang_defs::plugin::PluginDiagnostic;
use cairo_lang_diagnostics::DiagnosticsBuilder;
use cairo_lang_semantic::db::SemanticGroup;
use cairo_lang_semantic::diagnostic::NotFoundItemType;
use cairo_lang_semantic::expr::inference::InferenceId;
use cairo_lang_semantic::resolve::{ResolvedConcreteItem, ResolvedGenericItem, Resolver};
use cairo_lang_semantic::{ExprFunctionCall, ExprMatch};
use cairo_lang_syntax::node::ast::{Expr, ExprLoop, Statement, TerminalIdentifier};
use cairo_lang_syntax::node::TypedSyntaxNode;
use cairo_lang_utils::try_extract_matches;

pub fn check_loop_match_pop_front(
db: &dyn SemanticGroup,
loop_expr: &ExprLoop,
diagnostics: &mut Vec<PluginDiagnostic>,
module_id: &ModuleId,
) {
let statements = loop_expr.body(db.upcast()).statements(db.upcast()).elements(db.upcast());
for statement in statements {
if let Statement::Expr(expr) = statement
&& let Expr::Match(expr_match) = expr.expr(db.upcast())
{
let res = match expr_match.expr(db.upcast()) {
Expr::FunctionCall(func_call) => "".to_owned(),
Expr::Binary(binary_expr) => {
if let Expr::FunctionCall(func_call) =
Expr::from_syntax_node(db.upcast(), binary_expr.rhs(db.upcast()).as_syntax_node())
{
let mut diagnostics = DiagnosticsBuilder::default();
if let Expr::Path(pat) =
Expr::from_syntax_node(db.upcast(), binary_expr.lhs(db.upcast()).as_syntax_node())
{
let identifier = TerminalIdentifier::from_syntax_node(
db.upcast(),
pat.as_syntax_node().parent().unwrap(),
);
db.upcast().find_lookup_item(pat.as_syntax_node());
let item =
db.lookup_resolved_generic_item_by_ptr(lookup_item_id, identifier.stable_ptr())?;
Some(format!("`{}`", item.full_path(db)));
println!("Path {}", pat.node.get_text(db.upcast()));
let item =
Resolver::new(db, ModuleFileId(*module_id, FileIndex(0)), InferenceId::NoContext)
.resolve_generic_path(
&mut diagnostics,
&func_call.path(db.upcast()),
NotFoundItemType::Identifier,
)
.unwrap();
let generic_variant = try_extract_matches!(item, ResolvedGenericItem::Variable).unwrap();
println!("Func {:?}", generic_variant)
};
};
format!(
"Right {}",
binary_expr.rhs(db.upcast()).as_syntax_node().get_text_without_trivia(db.upcast())
)
}
_ => "".to_owned(),
};

println!("{}", res);
}
// println!("Statement: {:?}", statement);
}
}
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 loops;
pub mod single_match;
10 changes: 8 additions & 2 deletions crates/cairo-lint-core/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use cairo_lang_defs::ids::{ModuleId, ModuleItemId};
use cairo_lang_defs::plugin::PluginDiagnostic;
use cairo_lang_semantic::db::SemanticGroup;
use cairo_lang_semantic::plugin::{AnalyzerPlugin, PluginSuite};
use cairo_lang_syntax::node::ast::ExprMatch;
use cairo_lang_syntax::node::ast::{ExprLoop, ExprMatch};
use cairo_lang_syntax::node::kind::SyntaxKind;
use cairo_lang_syntax::node::TypedSyntaxNode;

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

pub fn cairo_lint_plugin_suite() -> PluginSuite {
let mut suite = PluginSuite::default();
Expand Down Expand Up @@ -51,6 +51,12 @@ impl AnalyzerPlugin for CairoLint {
&mut diags,
&module_id,
),
SyntaxKind::ExprLoop => loops::check_loop_match_pop_front(
db,
&ExprLoop::from_syntax_node(db.upcast(), descendant),
&mut diags,
&module_id,
),
SyntaxKind::ItemExternFunction => (),
_ => (),
}
Expand Down
11 changes: 11 additions & 0 deletions crates/cairo-lint-core/tests/test_files/loops/loop_match_pop_front
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//! > simple loop match pop front
//! > cairo_code
fn main() {
let a: Span<felt252> = array![1, 2, 3, 4, 5].span();
loop {
match a.pop_front() {
Option::Some(val) => println!("{val}"),
Option::None => 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 @@ -33,3 +33,5 @@ test_file!(
"simple destructuring match with unit and comment in scope",
"simple destructuring match with comment in scope"
);

test_file!(loops, loop_match_pop_front, "simple loop match pop front");

0 comments on commit 86b4a3d

Please sign in to comment.