-
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.
* feat(lint): implement double parentheses removal with extensive test coverage * fix: improve return datatype in tests :wq * feat: add more tests for double_parens lint * fix: format applied * fix: rebase applied, changes made and it is working * fix: cargo clippy happy again and format applied * fix: rebase applied and fix documented * feat: add refactor improvement for lint
- Loading branch information
Showing
7 changed files
with
320 additions
and
3 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
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,23 @@ | ||
use cairo_lang_defs::plugin::PluginDiagnostic; | ||
use cairo_lang_diagnostics::Severity; | ||
use cairo_lang_syntax::node::ast::Expr; | ||
use cairo_lang_syntax::node::db::SyntaxGroup; | ||
use cairo_lang_syntax::node::{TypedStablePtr, TypedSyntaxNode}; | ||
|
||
pub const DOUBLE_PARENS: &str = "unnecessary double parentheses found. Consider removing them."; | ||
|
||
pub fn check_double_parens(db: &dyn SyntaxGroup, expr: &Expr, diagnostics: &mut Vec<PluginDiagnostic>) { | ||
let is_double_parens = if let Expr::Parenthesized(parenthesized_expr) = expr { | ||
matches!(parenthesized_expr.expr(db), Expr::Parenthesized(_) | Expr::Tuple(_)) | ||
} else { | ||
false | ||
}; | ||
|
||
if is_double_parens { | ||
diagnostics.push(PluginDiagnostic { | ||
stable_ptr: expr.stable_ptr().untyped(), | ||
message: DOUBLE_PARENS.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod double_parens; | ||
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
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
234 changes: 234 additions & 0 deletions
234
crates/cairo-lint-core/tests/test_files/double_parens/double_parens
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,234 @@ | ||
//! > assert expressions | ||
|
||
//! > cairo_code | ||
fn main() { | ||
assert_eq!(((4)), 4); | ||
} | ||
|
||
//! > diagnostics | ||
warning: Plugin diagnostic: unnecessary double parentheses found. Consider removing them. | ||
--> lib.cairo:2:16 | ||
assert_eq!(((4)), 4); | ||
^***^ | ||
|
||
//! > fixed | ||
fn main() { | ||
assert_eq!(4, 4); | ||
} | ||
|
||
//! > ========================================================================== | ||
|
||
//! > double parens in let statement | ||
|
||
//! > cairo_code | ||
fn main() { | ||
let x = ((10 * 2)); | ||
} | ||
|
||
//! > diagnostics | ||
warning: Plugin diagnostic: unnecessary double parentheses found. Consider removing them. | ||
--> lib.cairo:2:13 | ||
let x = ((10 * 2)); | ||
^********^ | ||
|
||
warning[E0001]: Unused variable. Consider ignoring by prefixing with `_`. | ||
--> lib.cairo:2:9 | ||
let x = ((10 * 2)); | ||
^ | ||
|
||
//! > fixed | ||
fn main() { | ||
let _x = 10 * 2; | ||
} | ||
|
||
//! > ========================================================================== | ||
|
||
//! > double parens in match arm | ||
|
||
//! > cairo_code | ||
fn main() -> felt252 { | ||
let x = 5; | ||
match x { | ||
1 => ((10)), | ||
5 => ((20)), | ||
_ => ((30)), | ||
} | ||
} | ||
|
||
//! > diagnostics | ||
warning: Plugin diagnostic: unnecessary double parentheses found. Consider removing them. | ||
--> lib.cairo:4:14 | ||
1 => ((10)), | ||
^****^ | ||
|
||
warning: Plugin diagnostic: unnecessary double parentheses found. Consider removing them. | ||
--> lib.cairo:5:14 | ||
5 => ((20)), | ||
^****^ | ||
|
||
warning: Plugin diagnostic: unnecessary double parentheses found. Consider removing them. | ||
--> lib.cairo:6:14 | ||
_ => ((30)), | ||
^****^ | ||
|
||
//! > fixed | ||
fn main() -> felt252 { | ||
let x = 5; | ||
match x { | ||
1 => 10, | ||
5 => 20, | ||
_ => 30, | ||
} | ||
} | ||
|
||
//! > ========================================================================== | ||
|
||
//! > double parens in struct field access | ||
|
||
//! > cairo_code | ||
struct MyStruct { | ||
x: felt252, | ||
y: felt252, | ||
} | ||
|
||
fn main() -> felt252 { | ||
let my_struct = MyStruct { x: 10, y: 20 }; | ||
return ((my_struct.y)); | ||
} | ||
|
||
//! > diagnostics | ||
warning: Plugin diagnostic: unnecessary double parentheses found. Consider removing them. | ||
--> lib.cairo:8:12 | ||
return ((my_struct.y)); | ||
^*************^ | ||
|
||
//! > fixed | ||
struct MyStruct { | ||
x: felt252, | ||
y: felt252, | ||
} | ||
|
||
fn main() -> felt252 { | ||
let my_struct = MyStruct { x: 10, y: 20 }; | ||
return my_struct.y; | ||
} | ||
|
||
//! > ========================================================================== | ||
|
||
//! > double parens with function call | ||
|
||
//! > cairo_code | ||
fn foo(x: felt252) -> felt252 { | ||
x * 2 | ||
} | ||
|
||
fn main() -> felt252 { | ||
((foo(10))) | ||
} | ||
|
||
//! > diagnostics | ||
warning: Plugin diagnostic: unnecessary double parentheses found. Consider removing them. | ||
--> lib.cairo:6:5 | ||
((foo(10))) | ||
^*********^ | ||
|
||
//! > fixed | ||
fn foo(x: felt252) -> felt252 { | ||
x * 2 | ||
} | ||
|
||
fn main() -> felt252 { | ||
foo(10)} | ||
|
||
//! > ========================================================================== | ||
|
||
//! > double parens with return | ||
|
||
//! > cairo_code | ||
fn main() -> felt252 { | ||
return ((5 + 7)); | ||
} | ||
|
||
//! > diagnostics | ||
warning: Plugin diagnostic: unnecessary double parentheses found. Consider removing them. | ||
--> lib.cairo:2:12 | ||
return ((5 + 7)); | ||
^*******^ | ||
|
||
//! > fixed | ||
fn main() -> felt252 { | ||
return 5 + 7; | ||
} | ||
|
||
//! > ========================================================================== | ||
|
||
//! > necessary parentheses in arithmetic expression | ||
|
||
//! > cairo_code | ||
fn main() -> u32 { | ||
2 * (3 + 5) | ||
} | ||
|
||
//! > diagnostics | ||
|
||
//! > fixed | ||
fn main() -> u32 { | ||
2 * (3 + 5) | ||
} | ||
|
||
//! > ========================================================================== | ||
|
||
//! > simple double parens | ||
|
||
//! > cairo_code | ||
fn main() -> u32 { | ||
((0)) | ||
} | ||
|
||
//! > diagnostics | ||
warning: Plugin diagnostic: unnecessary double parentheses found. Consider removing them. | ||
--> lib.cairo:2:5 | ||
((0)) | ||
^***^ | ||
|
||
//! > fixed | ||
fn main() -> u32 { | ||
0} | ||
|
||
//! > ========================================================================== | ||
|
||
//! > tuple double parens | ||
|
||
//! > cairo_code | ||
fn main() -> (felt252, felt252) { | ||
((1, 2)) | ||
} | ||
|
||
//! > diagnostics | ||
warning: Plugin diagnostic: unnecessary double parentheses found. Consider removing them. | ||
--> lib.cairo:2:5 | ||
((1, 2)) | ||
^******^ | ||
|
||
//! > fixed | ||
fn main() -> (felt252, felt252) { | ||
(1, 2)} | ||
|
||
//! > ========================================================================== | ||
|
||
//! > unnecessary parentheses in arithmetic expression | ||
|
||
//! > cairo_code | ||
fn main() -> u32 { | ||
((3 + 5)) | ||
} | ||
|
||
//! > diagnostics | ||
warning: Plugin diagnostic: unnecessary double parentheses found. Consider removing them. | ||
--> lib.cairo:2:5 | ||
((3 + 5)) | ||
^*******^ | ||
|
||
//! > fixed | ||
fn main() -> u32 { | ||
3 + 5} |
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