Skip to content
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

Add rule for rand(Bool) instead of rand() < 0.5 #14

Merged
merged 2 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions rules/lang/best-practice/rand_bool.fixed.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#ruleid: rand-bool
rand(Bool)

function some_rand_function(x)
#ruleid: rand-bool
if rand(Bool)
println("Random")
end
end

#ok: rand-bool
rand() < 0.7 # this is fine

x = rand()
#ok: rand-bool
if x < 0.5
do_something()
else
do_something_else()
end

#ok: rand-bool
y = ok()
if y < 0.5
do_something()
else
do_something_else()
end

#ok: rand-bool
x < 0.5 && action()

#ok: rand-bool
flag = rand(Bool)

#ruleid: rand-bool
if some_flag && rand(Bool) || other_flag
println("Random")
end

40 changes: 40 additions & 0 deletions rules/lang/best-practice/rand_bool.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#ruleid: rand-bool
rand() < 0.5

function some_rand_function(x)
#ruleid: rand-bool
if rand() < 0.5
println("Random")
end
end

#ok: rand-bool
rand() < 0.7 # this is fine

x = rand()
#ok: rand-bool
if x < 0.5
do_something()
else
do_something_else()
end

#ok: rand-bool
y = ok()
if y < 0.5
do_something()
else
do_something_else()
end

#ok: rand-bool
x < 0.5 && action()

#ok: rand-bool
flag = rand(Bool)

#ruleid: rand-bool
if some_flag && rand() < 0.5 || other_flag
println("Random")
end

16 changes: 16 additions & 0 deletions rules/lang/best-practice/rand_bool.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
rules:
- id: rand-bool
patterns:
- pattern: $RAND() < 0.5
- metavariable-regex:
metavariable: $RAND
regex: '(Base.)?rand'
fix: rand(Bool)
message: To get a random Boolean, use `rand(Bool)`.
languages:
- julia
severity: WARNING
metadata:
category: best-practice
license: LGPL

40 changes: 40 additions & 0 deletions rules/lang/best-practice/rand_bool_variable.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#ok: rand-bool-variable
rand() < 0.5

function some_rand_function(x)
#ok: rand-bool-variable
if rand() < 0.5
println("Random")
end
end

#ok: rand-bool-variable
rand() < 0.7 # this is fine

x = rand()
#ruleid: rand-bool-variable
if x < 0.5
do_something()
else
do_something_else()
end

#ok: rand-bool-variable
y = ok()
if y < 0.5
do_something()
else
do_something_else()
end

#ruleid: rand-bool-variable
x < 0.5 && action()

#ok: rand-bool-variable
flag = rand(Bool)

#ok: rand-bool-variable
if some_flag && rand() < 0.5 || other_flag
println("Random")
end

20 changes: 20 additions & 0 deletions rules/lang/best-practice/rand_bool_variable.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
rules:
- id: rand-bool-variable
patterns:
- patterns:
- pattern-inside: |
$X = $RAND()
...
<... $X < 0.5 ...>
- pattern: $X < 0.5
- metavariable-regex:
metavariable: $RAND
regex: '(Base.)?rand'
message: To get a random Boolean, use `rand(Bool)`.
languages:
- julia
severity: WARNING
metadata:
category: best-practice
license: LGPL