-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refacto(core): change file structure for lints
- Loading branch information
Showing
5 changed files
with
81 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
#![feature(let_chains)] | ||
pub mod db; | ||
pub mod fix; | ||
pub mod lints; | ||
pub mod plugin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod single_match; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use cairo_lang_defs::plugin::PluginDiagnostic; | ||
use cairo_lang_diagnostics::Severity; | ||
use cairo_lang_syntax::node::ast::{Expr, ExprBlock, ExprListParenthesized, ExprMatch, Pattern, Statement}; | ||
use cairo_lang_syntax::node::db::SyntaxGroup; | ||
use cairo_lang_syntax::node::{TypedStablePtr, TypedSyntaxNode}; | ||
|
||
pub const DESTRUCT_MATCH: &str = | ||
"you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`"; | ||
pub const MATCH_FOR_EQUALITY: &str = "you seem to be trying to use `match` for an equality check. Consider using `if`"; | ||
|
||
fn tuple_expr_in_block_expr( | ||
db: &dyn SyntaxGroup, | ||
block_expr: &ExprBlock, | ||
is_single_armed: &mut bool, | ||
) -> Option<ExprListParenthesized> { | ||
let statements = block_expr.statements(db).elements(db); | ||
if statements.is_empty() { | ||
*is_single_armed = true; | ||
} | ||
if statements.len() == 1 | ||
&& let Statement::Expr(statement_expr) = &statements[0] | ||
&& let Expr::Tuple(tuple_expr) = statement_expr.expr(db) | ||
{ | ||
Some(tuple_expr) | ||
} else { | ||
None | ||
} | ||
} | ||
pub fn check_destruct_match(db: &dyn SyntaxGroup, match_expr: &ExprMatch, diagnostics: &mut Vec<PluginDiagnostic>) { | ||
let arms = match_expr.arms(db).elements(db); | ||
let mut is_single_armed = false; | ||
let mut is_destructuring = false; | ||
if arms.len() == 2 { | ||
for arm in arms { | ||
let patterns = arm.patterns(db).elements(db); | ||
match &patterns[0] { | ||
Pattern::Underscore(_) => { | ||
let tuple_expr = match arm.expression(db) { | ||
Expr::Block(block_expr) => tuple_expr_in_block_expr(db, &block_expr, &mut is_single_armed), | ||
Expr::Tuple(tuple_expr) => Some(tuple_expr), | ||
_ => None, | ||
}; | ||
is_single_armed = | ||
tuple_expr.is_some_and(|list| list.expressions(db).elements(db).is_empty()) || is_single_armed; | ||
} | ||
|
||
Pattern::Enum(pat) => { | ||
is_destructuring = !pat.pattern(db).as_syntax_node().get_text(db).is_empty(); | ||
} | ||
Pattern::Struct(pat) => { | ||
is_destructuring = !pat.as_syntax_node().get_text(db).is_empty(); | ||
} | ||
_ => (), | ||
}; | ||
} | ||
}; | ||
match (is_single_armed, is_destructuring) { | ||
(true, false) => diagnostics.push(PluginDiagnostic { | ||
stable_ptr: match_expr.stable_ptr().untyped(), | ||
message: MATCH_FOR_EQUALITY.to_string(), | ||
severity: Severity::Warning, | ||
}), | ||
(true, true) => diagnostics.push(PluginDiagnostic { | ||
stable_ptr: match_expr.stable_ptr().untyped(), | ||
message: DESTRUCT_MATCH.to_string(), | ||
severity: Severity::Warning, | ||
}), | ||
(_, _) => (), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
[toolchain] | ||
channel = "1.80.0" | ||
channel = "nightly" | ||
components = ["rustfmt", "clippy"] | ||
profile = "minimal" |