Skip to content

Commit

Permalink
refacto(core): switch to semantic query and remove custom db (#39)
Browse files Browse the repository at this point in the history
* refacto(core): switch to semantic query and remove custom db

* chore: make clippy happy

* chore: remove useless deps
  • Loading branch information
0xLucqs authored Aug 22, 2024
1 parent b9a1161 commit cb73ae1
Show file tree
Hide file tree
Showing 17 changed files with 326 additions and 911 deletions.
816 changes: 117 additions & 699 deletions Cargo.lock

Large diffs are not rendered by default.

11 changes: 10 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
[workspace]
resolver = "2"
members = ["crates/cairo-lint-cli", "crates/cairo-lint-core", "crates/cairo-lint-dev", "crates/cairo-lint-test-utils"]
members = [
"crates/cairo-lint-cli",
"crates/cairo-lint-core",
"crates/cairo-lint-dev",
"crates/cairo-lint-test-utils",
]

[workspace.package]
version = "0.1.0"
Expand Down Expand Up @@ -30,3 +35,7 @@ ctor = "0.2.8"
paste = "1.0.15"
itertools = "0.13.0"
log = "0.4.22"
clap = "4.5.16"
anyhow = "1.0.86"
smol_str = "0.2.2"
annotate-snippets = "0.11.4"
1 change: 0 additions & 1 deletion crates/cairo-lint-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,4 @@ cairo-lang-test-plugin.workspace = true
cairo-lang-lowering.workspace = true
cairo-lang-syntax.workspace = true
cairo-lang-defs.workspace = true
cairo-lang-language-server.workspace = true
salsa.workspace = true
1 change: 0 additions & 1 deletion crates/cairo-lint-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ cairo-lang-test-plugin.workspace = true
cairo-lang-lowering.workspace = true
cairo-lang-syntax.workspace = true
cairo-lang-defs.workspace = true
cairo-lang-language-server.workspace = true
salsa.workspace = true
log.workspace = true

Expand Down
103 changes: 0 additions & 103 deletions crates/cairo-lint-core/src/db.rs

This file was deleted.

24 changes: 10 additions & 14 deletions crates/cairo-lint-core/src/fix.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use cairo_lang_compiler::db::RootDatabase;
use cairo_lang_defs::ids::UseId;
use cairo_lang_defs::plugin::PluginDiagnostic;
use cairo_lang_filesystem::span::TextSpan;
Expand All @@ -10,7 +11,6 @@ use cairo_lang_syntax::node::{SyntaxNode, TypedStablePtr, TypedSyntaxNode};
use cairo_lang_utils::Upcast;
use log::{debug, warn};

use crate::db::AnalysisDatabase;
use crate::lints::single_match::is_expr_unit;
use crate::plugin::{diagnostic_kind_from_message, CairoLintKind};

Expand All @@ -29,15 +29,15 @@ pub struct Fix {
///
/// # Arguments
///
/// * `db` - A reference to the AnalysisDatabase
/// * `db` - A reference to the RootDatabase
/// * `diag` - A reference to the SemanticDiagnostic to be fixed
///
/// # Returns
///
/// An `Option<(SyntaxNode, String)>` where the `SyntaxNode` represents the node to be
/// replaced, and the `String` is the suggested replacement. Returns `None` if no fix
/// is available for the given diagnostic.
pub fn fix_semantic_diagnostic(db: &AnalysisDatabase, diag: &SemanticDiagnostic) -> Option<(SyntaxNode, String)> {
pub fn fix_semantic_diagnostic(db: &RootDatabase, diag: &SemanticDiagnostic) -> Option<(SyntaxNode, String)> {
match diag.kind {
SemanticDiagnosticKind::UnusedVariable => Fixer.fix_unused_variable(db, diag),
SemanticDiagnosticKind::PluginDiagnostic(ref plugin_diag) => Fixer.fix_plugin_diagnostic(db, diag, plugin_diag),
Expand All @@ -56,18 +56,14 @@ impl Fixer {
///
/// # Arguments
///
/// * `db` - A reference to the AnalysisDatabase
/// * `db` - A reference to the RootDatabase
/// * `diag` - A reference to the SemanticDiagnostic for the unused variable
///
/// # Returns
///
/// An `Option<(SyntaxNode, String)>` containing the node to be replaced and the
/// suggested replacement (the variable name prefixed with an underscore).
pub fn fix_unused_variable(
&self,
db: &AnalysisDatabase,
diag: &SemanticDiagnostic,
) -> Option<(SyntaxNode, String)> {
pub fn fix_unused_variable(&self, db: &RootDatabase, diag: &SemanticDiagnostic) -> Option<(SyntaxNode, String)> {
let node = diag.stable_location.syntax_node(db.upcast());
let suggestion = format!("_{}", node.get_text(db.upcast()));
Some((node, suggestion))
Expand Down Expand Up @@ -127,7 +123,7 @@ impl Fixer {
///
/// # Arguments
///
/// * `db` - A reference to the AnalysisDatabase
/// * `db` - A reference to the RootDatabase
/// * `diag` - A reference to the SemanticDiagnostic
/// * `plugin_diag` - A reference to the PluginDiagnostic
///
Expand All @@ -137,7 +133,7 @@ impl Fixer {
/// suggested replacement.
pub fn fix_plugin_diagnostic(
&self,
db: &AnalysisDatabase,
db: &RootDatabase,
semantic_diag: &SemanticDiagnostic,
plugin_diag: &PluginDiagnostic,
) -> Option<(SyntaxNode, String)> {
Expand All @@ -146,7 +142,7 @@ 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())),
_ => "".to_owned(),
_ => return None,
};

Some((semantic_diag.stable_location.syntax_node(db.upcast()), new_text))
Expand Down Expand Up @@ -191,15 +187,15 @@ impl Fixer {
///
/// # Arguments
///
/// * `db` - A reference to the AnalysisDatabase
/// * `db` - A reference to the RootDatabase
/// * `diag` - A reference to the SemanticDiagnostic
/// * `id` - A reference to the UseId of the unused import
///
/// # Returns
///
/// An `Option<(SyntaxNode, String)>` containing the node to be removed and an empty string
/// (indicating removal). Returns `None` for multi-import paths.
pub fn fix_unused_import(&self, db: &AnalysisDatabase, id: &UseId) -> Option<(SyntaxNode, String)> {
pub fn fix_unused_import(&self, db: &RootDatabase, id: &UseId) -> Option<(SyntaxNode, String)> {
let mut current_node = id.stable_ptr(db).lookup(db.upcast()).as_syntax_node();
let mut path_to_remove = vec![current_node.clone()];
let mut remove_entire_statement = true;
Expand Down
1 change: 0 additions & 1 deletion crates/cairo-lint-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![feature(let_chains)]
pub mod db;
pub mod fix;
pub mod lints;
pub mod plugin;
48 changes: 48 additions & 0 deletions crates/cairo-lint-core/src/lints/loops.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use cairo_lang_defs::plugin::PluginDiagnostic;
use cairo_lang_diagnostics::Severity;
use cairo_lang_semantic::db::SemanticGroup;
use cairo_lang_semantic::{Arenas, Expr, ExprLoop, Statement};

pub const LOOP_MATCH_POP_FRONT: &str =
"you seem to be trying to use `loop` for iterating over a span. Consider using `for in`";

const SPAN_MATCH_POP_FRONT: &str = "\"SpanImpl::pop_front\"";

pub fn check_loop_match_pop_front(
db: &dyn SemanticGroup,
loop_expr: &ExprLoop,
diagnostics: &mut Vec<PluginDiagnostic>,
arenas: &Arenas,
) {
let Expr::Block(expr_block) = &arenas.exprs[loop_expr.body] else {
return;
};
if let Some(tail) = &expr_block.tail
&& let Expr::Match(expr_match) = &arenas.exprs[*tail]
&& let Expr::FunctionCall(func_call) = &arenas.exprs[expr_match.matched_expr]
&& func_call.function.name(db) == SPAN_MATCH_POP_FRONT
{
diagnostics.push(PluginDiagnostic {
stable_ptr: loop_expr.stable_ptr.into(),
message: LOOP_MATCH_POP_FRONT.to_owned(),
severity: Severity::Warning,
});
return;
}
for statement in &expr_block.statements {
if let Statement::Expr(stmt_expr) = &arenas.statements[*statement]
&& let Expr::Match(expr_match) = &arenas.exprs[stmt_expr.expr]
{
let Expr::FunctionCall(func_call) = &arenas.exprs[expr_match.matched_expr] else {
continue;
};
if func_call.function.name(db) == SPAN_MATCH_POP_FRONT {
diagnostics.push(PluginDiagnostic {
stable_ptr: loop_expr.stable_ptr.into(),
message: LOOP_MATCH_POP_FRONT.to_owned(),
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 double_parens;
pub mod loops;
pub mod single_match;
Loading

0 comments on commit cb73ae1

Please sign in to comment.