Skip to content

Commit

Permalink
fix duplicate underscore args
Browse files Browse the repository at this point in the history
  • Loading branch information
stevencartavia committed Sep 6, 2024
1 parent 898dd2d commit 7c922ef
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 1 deletion.
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 @@ -156,6 +156,7 @@ impl Fixer {
db,
ExprBinary::from_syntax_node(db.upcast(), plugin_diag.stable_ptr.lookup(db.upcast())),
),
CairoLintKind::DuplicateUnderscoreArgs => self.fix_duplicate_underscore_args(db, plugin_diag.stable_ptr.lookup(db.upcast())),
_ => return None,
};

Expand All @@ -166,6 +167,10 @@ impl Fixer {
node.get_text(db).replace("break ();", "break;").to_string()
}

pub fn fix_duplicate_underscore_args(&self, _db: &dyn SyntaxGroup, _node: SyntaxNode) -> String {
return String::from("_diff");
}

pub fn fix_bool_comparison(&self, db: &dyn SyntaxGroup, node: ExprBinary) -> String {
let lhs = node.lhs(db).as_syntax_node().get_text(db);
let rhs = node.rhs(db).as_syntax_node().get_text(db);
Expand Down
29 changes: 29 additions & 0 deletions crates/cairo-lint-core/src/lints/duplicate_underscore_args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use cairo_lang_defs::plugin::PluginDiagnostic;
use cairo_lang_semantic::Parameter;
use cairo_lang_diagnostics::Severity;
use std::collections::HashMap;
use cairo_lang_syntax::node::ids::SyntaxStablePtrId;

pub const DUPLICATE_UNDERSCORE_ARGS: &str = "duplicate arguments, having another argument having almost the same name makes code comprehension and documentation more difficult";

pub fn check_duplicate_underscore_args(params: Vec<Parameter>, diagnostics: &mut Vec<PluginDiagnostic>) {
let mut registered_names: HashMap<String, SyntaxStablePtrId> = HashMap::default();

for param in params {
let param_name = param.name.to_string();

if let Some(stripped_name) = param_name.strip_prefix('_') {
if let Some(_) = registered_names.get(stripped_name) {
diagnostics.push(PluginDiagnostic {
stable_ptr: param.stable_ptr.0,
message: DUPLICATE_UNDERSCORE_ARGS.to_string(),
severity: Severity::Warning,
});
} else {
registered_names.insert(param_name, param.stable_ptr.0);
}
} else {
registered_names.insert(param_name.clone(), param.stable_ptr.0);
}
}
}
1 change: 1 addition & 0 deletions crates/cairo-lint-core/src/lints/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ pub mod double_comparison;
pub mod double_parens;
pub mod loops;
pub mod single_match;
pub mod duplicate_underscore_args;
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 @@ -7,7 +7,8 @@ use cairo_lang_syntax::node::ast::{Expr as AstExpr, ExprBinary};
use cairo_lang_syntax::node::kind::SyntaxKind;
use cairo_lang_syntax::node::{TypedStablePtr, TypedSyntaxNode};

use crate::lints::{bool_comparison, breaks, double_comparison, double_parens, loops, single_match};
use crate::lints::{bool_comparison, breaks, double_comparison, double_parens, loops, single_match, duplicate_underscore_args};
use crate::plugin::duplicate_underscore_args::check_duplicate_underscore_args;

pub fn cairo_lint_plugin_suite() -> PluginSuite {
let mut suite = PluginSuite::default();
Expand All @@ -26,6 +27,7 @@ pub enum CairoLintKind {
Unknown,
BreakUnit,
BoolComparison,
DuplicateUnderscoreArgs
}

pub fn diagnostic_kind_from_message(message: &str) -> CairoLintKind {
Expand All @@ -38,6 +40,7 @@ pub fn diagnostic_kind_from_message(message: &str) -> CairoLintKind {
double_comparison::CONTRADICTORY_COMPARISON => CairoLintKind::DoubleComparison,
breaks::BREAK_UNIT => CairoLintKind::BreakUnit,
bool_comparison::BOOL_COMPARISON => CairoLintKind::BoolComparison,
duplicate_underscore_args::DUPLICATE_UNDERSCORE_ARGS => CairoLintKind::DuplicateUnderscoreArgs,
_ => CairoLintKind::Unknown,
}
}
Expand All @@ -49,6 +52,7 @@ impl AnalyzerPlugin for CairoLint {
return diags;
};
for free_func_id in free_functions_ids.iter() {
check_duplicate_underscore_args(db.function_with_body_signature(FunctionWithBodyId::Free(*free_func_id)).unwrap().params, &mut diags);
let Ok(function_body) = db.function_body(FunctionWithBodyId::Free(*free_func_id)) else {
return diags;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//! > duplicate underscore args

//! > cairo_code
fn foo(a: u32, _a: u32) {}

fn main() {}

//! > diagnostics
warning: Plugin diagnostic: duplicate arguments, having another argument having almost the same name makes code comprehension and documentation more difficult
--> lib.cairo:0:16
|
0 | fn foo(a: u32, _a: u32) {}
| --
|

//! > fixed
fn foo(a: u32, _diff: u32) {}

fn main() {}

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

//! > duplicate underscore args2

//! > cairo_code
fn foo(c: u32, _c: u32) {}

//! > diagnostics
warning: Plugin diagnostic: duplicate arguments, having another argument having almost the same name makes code comprehension and documentation more difficult
--> lib.cairo:0:16
|
0 | fn foo(c: u32, _c: u32) {}
| --
|

//! > fixed
fn foo(c: u32, _diff: u32) {}
7 changes: 7 additions & 0 deletions crates/cairo-lint-core/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,10 @@ test_file!(
"Negated comparison with false",
"Negated comparison with false on LHS"
);

test_file!(
duplicate_underscore_args,
duplicate_underscore_args,
"duplicate underscore args",
"duplicate underscore args2"
);

0 comments on commit 7c922ef

Please sign in to comment.