-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
898dd2d
commit 7c922ef
Showing
6 changed files
with
84 additions
and
1 deletion.
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
29 changes: 29 additions & 0 deletions
29
crates/cairo-lint-core/src/lints/duplicate_underscore_args.rs
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,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); | ||
} | ||
} | ||
} |
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
37 changes: 37 additions & 0 deletions
37
crates/cairo-lint-core/tests/test_files/duplicate_underscore_args/duplicate_underscore_args
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,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) {} |
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