Skip to content

Commit

Permalink
Add rule for rand(Bool) instead of rand() < 0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
iuliadmtru committed Sep 1, 2024
1 parent 9322f67 commit 43137c8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
21 changes: 21 additions & 0 deletions rules/lang/best-practice/rand_bool.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#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

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

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

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

0 comments on commit 43137c8

Please sign in to comment.