Skip to content

Commit

Permalink
Split rule in two
Browse files Browse the repository at this point in the history
  • Loading branch information
iuliadmtru committed Sep 26, 2024
1 parent b692572 commit ca9ec5c
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 14 deletions.
8 changes: 4 additions & 4 deletions rules/lang/best-practice/rand_bool.fixed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ end
rand() < 0.7 # this is fine

x = rand()
#ruleid: rand-bool
if rand(Bool)
#ok: rand-bool
if x < 0.5
do_something()
else
do_something_else()
Expand All @@ -27,8 +27,8 @@ else
do_something_else()
end

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

#ok: rand-bool
flag = rand(Bool)
Expand Down
4 changes: 2 additions & 2 deletions rules/lang/best-practice/rand_bool.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ end
rand() < 0.7 # this is fine

x = rand()
#ruleid: rand-bool
#ok: rand-bool
if x < 0.5
do_something()
else
Expand All @@ -27,7 +27,7 @@ else
do_something_else()
end

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

#ok: rand-bool
Expand Down
9 changes: 1 addition & 8 deletions rules/lang/best-practice/rand_bool.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
rules:
- id: rand-bool
patterns:
- pattern-either:
- pattern: $RAND() < 0.5
- patterns:
- pattern-inside: |
$X = $RAND()
...
<... $X < 0.5 ...>
- pattern: $X < 0.5
- pattern: $RAND() < 0.5
- metavariable-regex:
metavariable: $RAND
regex: '(Base.)?rand'
Expand Down
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

0 comments on commit ca9ec5c

Please sign in to comment.