-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat: Add manual assert #101
Closed
stevencartavia
wants to merge
8
commits into
software-mansion:main
from
stevencartavia:feat/manual_assert
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fb6329d
manual assert
stevencartavia 54ecae4
fmt
stevencartavia 4cb2e0c
fix fn update
stevencartavia bb6d930
update
stevencartavia 9c63b5f
Merge branch 'main' into feat/manual_assert
stevencartavia 569aa05
fix
stevencartavia 0910c05
Merge branch 'main' into feat/manual_assert
stevencartavia 35948a9
more tests
stevencartavia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,34 @@ | ||
use cairo_lang_defs::plugin::PluginDiagnostic; | ||
use cairo_lang_diagnostics::Severity; | ||
use cairo_lang_syntax::node::ast::{Condition, Expr, ExprBlock, ExprIf, Statement}; | ||
use cairo_lang_syntax::node::db::SyntaxGroup; | ||
use cairo_lang_syntax::node::{TypedStablePtr, TypedSyntaxNode}; | ||
|
||
pub const MANUAL_ASSERT: &str = "`assert!` is simpler than `if`-then-`panic!`"; | ||
|
||
pub fn contains_panic(db: &dyn SyntaxGroup, block_expr: &ExprBlock) -> bool { | ||
let statements = block_expr.statements(db).elements(db); | ||
if let Statement::Expr(statement_expr) = &statements[0] { | ||
if let Expr::InlineMacro(inline_macro) = &statement_expr.expr(db) { | ||
let macro_name: String = inline_macro.path(db).node.clone().get_text_without_trivia(db); | ||
if macro_name == "panic" { | ||
return true; | ||
} | ||
} | ||
} | ||
false | ||
} | ||
|
||
pub fn check_if_then_panic(db: &dyn SyntaxGroup, expr: &ExprIf, diagnostics: &mut Vec<PluginDiagnostic>) { | ||
let condition = expr.condition(db); | ||
let block_expr = expr.if_block(db); | ||
if let Condition::Expr(_condition_expr) = condition | ||
&& contains_panic(db, &block_expr) | ||
{ | ||
diagnostics.push(PluginDiagnostic { | ||
stable_ptr: expr.stable_ptr().untyped(), | ||
message: MANUAL_ASSERT.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,3 +1,4 @@ | ||
pub mod manual_assert; | ||
pub mod manual_expect; | ||
pub mod manual_is_none; | ||
pub mod manual_is_some; | ||
|
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
194 changes: 194 additions & 0 deletions
194
crates/cairo-lint-core/tests/test_files/manual/manual_assert
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,194 @@ | ||
//! > check if or if | ||
|
||
//! > cairo_code | ||
fn main() { | ||
let sad_people: Array<u32> = array![]; | ||
let happy_people: Array<u32> = array![]; | ||
if !sad_people.is_empty() || !happy_people.is_empty() { | ||
panic!("panic"); | ||
} | ||
} | ||
|
||
//! > diagnostics | ||
warning: Plugin diagnostic: Leaving `panic!` in the code is discouraged. | ||
--> lib.cairo:8:9 | ||
| | ||
8 | panic!("panic"); | ||
| ----- | ||
| | ||
warning: Plugin diagnostic: Leaving `panic!` in the code is discouraged. | ||
--> lib.cairo:8:9 | ||
| | ||
8 | panic!("panic"); | ||
| ----- | ||
| | ||
warning: Plugin diagnostic: `assert!` is simpler than `if`-then-`panic!` | ||
--> lib.cairo:6:5 | ||
| | ||
6 | if !sad_people.is_empty() || !happy_people.is_empty() { | ||
| _____- | ||
7 | | panic!("panic"); | ||
8 | | } | ||
| |_____- | ||
| | ||
|
||
//! > fixed | ||
fn main() { | ||
let sad_people: Array<u32> = array![]; | ||
let happy_people: Array<u32> = array![]; | ||
assert!(!sad_people.is_empty() || !happy_people.is_empty(), "panic: {:?}", sad_people); | ||
} | ||
|
||
//! > ========================================================================== | ||
|
||
//! > check if then else | ||
|
||
//! > cairo_code | ||
fn main() { | ||
let sad_people: Array<u32> = array![1, 2, 3]; | ||
if !sad_people.is_empty() { | ||
let _ = 0; | ||
} else if sad_people.len() > 1 { | ||
panic!("there are sad people"); | ||
} else if sad_people.len() > 2 { | ||
panic!("there are more sad people"); | ||
} | ||
} | ||
|
||
//! > diagnostics | ||
warning: Plugin diagnostic: Leaving `panic!` in the code is discouraged. | ||
--> lib.cairo:10:9 | ||
| | ||
10 | panic!("there are sad people"); | ||
| ----- | ||
| | ||
warning: Plugin diagnostic: Leaving `panic!` in the code is discouraged. | ||
--> lib.cairo:10:9 | ||
| | ||
10 | panic!("there are sad people"); | ||
| ----- | ||
| | ||
warning: Plugin diagnostic: Leaving `panic!` in the code is discouraged. | ||
--> lib.cairo:14:9 | ||
| | ||
14 | panic!("there are more sad people"); | ||
| ----- | ||
| | ||
warning: Plugin diagnostic: Leaving `panic!` in the code is discouraged. | ||
--> lib.cairo:14:9 | ||
| | ||
14 | panic!("there are more sad people"); | ||
| ----- | ||
| | ||
warning: Plugin diagnostic: `assert!` is simpler than `if`-then-`panic!` | ||
--> lib.cairo:8:12 | ||
| | ||
8 | } else if sad_people.len() > 1 { | ||
| ____________- | ||
9 | | panic!("there are sad people"); | ||
10 | | } else if sad_people.len() > 2 { | ||
11 | | panic!("there are more sad people"); | ||
12 | | } | ||
| |_____- | ||
| | ||
warning: Plugin diagnostic: `assert!` is simpler than `if`-then-`panic!` | ||
--> lib.cairo:12:12 | ||
| | ||
12 | } else if sad_people.len() > 2 { | ||
| ____________- | ||
13 | | panic!("there are more sad people"); | ||
14 | | } | ||
| |_____- | ||
| | ||
|
||
//! > fixed | ||
fn main() { | ||
let sad_people: Array<u32> = array![1, 2, 3]; | ||
if !sad_people.is_empty() { | ||
let _ = 0; | ||
} else assert!(sad_people.len() > 1, "there are sad people: {:?}", sad_people); | ||
assert!(sad_people.len() > 2, "there are more sad people: {:?}", sad_people); | ||
|
||
} | ||
|
||
//! > ========================================================================== | ||
|
||
//! > check-if-then | ||
|
||
//! > cairo_code | ||
fn main() { | ||
let sad_people: Array<u32> = array![]; | ||
if !sad_people.is_empty() { | ||
panic!("there are sad people"); | ||
} | ||
} | ||
|
||
//! > diagnostics | ||
warning: Plugin diagnostic: Leaving `panic!` in the code is discouraged. | ||
--> lib.cairo:6:9 | ||
| | ||
6 | panic!("there are sad people"); | ||
| ----- | ||
| | ||
warning: Plugin diagnostic: Leaving `panic!` in the code is discouraged. | ||
--> lib.cairo:6:9 | ||
| | ||
6 | panic!("there are sad people"); | ||
| ----- | ||
| | ||
warning: Plugin diagnostic: `assert!` is simpler than `if`-then-`panic!` | ||
--> lib.cairo:4:5 | ||
| | ||
4 | if !sad_people.is_empty() { | ||
| _____- | ||
5 | | panic!("there are sad people"); | ||
6 | | } | ||
| |_____- | ||
| | ||
|
||
//! > fixed | ||
fn main() { | ||
let sad_people: Array<u32> = array![]; | ||
assert!(!sad_people.is_empty(), "there are sad people: {:?}", sad_people); | ||
} | ||
|
||
//! > ========================================================================== | ||
|
||
//! > check-if-then2 | ||
|
||
//! > cairo_code | ||
fn main() { | ||
let sad_people: Array<u32> = array![]; | ||
if sad_people.len() > 2 { | ||
panic!("there are sad people"); | ||
} | ||
} | ||
|
||
//! > diagnostics | ||
warning: Plugin diagnostic: Leaving `panic!` in the code is discouraged. | ||
--> lib.cairo:6:9 | ||
| | ||
6 | panic!("there are sad people"); | ||
| ----- | ||
| | ||
warning: Plugin diagnostic: Leaving `panic!` in the code is discouraged. | ||
--> lib.cairo:6:9 | ||
| | ||
6 | panic!("there are sad people"); | ||
| ----- | ||
| | ||
warning: Plugin diagnostic: `assert!` is simpler than `if`-then-`panic!` | ||
--> lib.cairo:4:5 | ||
| | ||
4 | if sad_people.len() > 2 { | ||
| _____- | ||
5 | | panic!("there are sad people"); | ||
6 | | } | ||
| |_____- | ||
| | ||
|
||
//! > fixed | ||
fn main() { | ||
let sad_people: Array<u32> = array![]; | ||
assert!(sad_people.len() > 2, "there are sad people: {:?}", sad_people); | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add more tests for example one with
if else
don't only check the happy path you need to think of edge cases