-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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
[Linter] Unnecessary while loop #16876
[Linter] Unnecessary while loop #16876
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
db48e48
to
725865f
Compare
@tx-tomcat is attempting to deploy a commit to the Mysten Labs Team on Vercel. A member of the Team first needs to authorize it. |
725865f
to
7767cd3
Compare
7767cd3
to
24aec0d
Compare
24aec0d
to
0c69463
Compare
fn is_condition_always_true(condition: &UnannotatedExp_) -> bool { | ||
if let UnannotatedExp_::Value(val) = condition { | ||
if let Value_::Bool(b) = &val.value { | ||
return *b; | ||
} | ||
} | ||
false | ||
} | ||
fn report_while_true_to_loop(env: &mut CompilationEnv, loc: Loc) { |
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.
breaking things up into functions like this is typically not preferred when they called exactly once
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.
refactored.Only is_condition_always_true
remain
external-crates/move/crates/move-compiler/src/linters/unnecessary_while_loop.rs
Outdated
Show resolved
Hide resolved
external-crates/move/crates/move-compiler/src/linters/unnecessary_while_loop.rs
Outdated
Show resolved
Hide resolved
@@ -0,0 +1,54 @@ | |||
module 0x42::loop_test { |
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.
Please split this file as previously discussed
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.
refactored
external-crates/move/crates/move-compiler/tests/linter/incorrect_unnecessary_while_loop.exp
Outdated
Show resolved
Hide resolved
0c69463
to
e583142
Compare
e583142
to
c6308a6
Compare
# Description This linter encourages replacing `while(true)` loops with the more idiomatic loop construct. Here's a breakdown of how it works: It checks each expression in the AST. If the expression is a While loop, it examines the condition. If the condition is always true (using the `is_condition_always_true` function), it reports a diagnostic suggesting to use loop instead. The `is_condition_always_true` function checks if the condition is a boolean literal with the value true. ## Test plan Added more use case including false positive, false negative case ## Release notes - [ ] Protocol: - [ ] Nodes (Validators and Full nodes): - [ ] Indexer: - [ ] JSON-RPC: - [ ] GraphQL: - [X] CLI: Move will now lint against `while (true)`, which should be replaced by `loop` - [ ] Rust SDK: --------- Co-authored-by: jamedzung <[email protected]> Co-authored-by: Todd Nowacki <[email protected]>
Description
This linter encourages replacing
while(true)
loops with the more idiomatic loop construct. Here's a breakdown of how it works:It checks each expression in the AST.
If the expression is a While loop, it examines the condition.
If the condition is always true (using the
is_condition_always_true
function), it reports a diagnostic suggesting to use loop instead.The
is_condition_always_true
function checks if the condition is a boolean literal with the value true.Test plan
Added more use case including false positive, false negative case
Release notes
while (true)
, which should be replaced byloop