Skip to content

Commit e66a93a

Browse files
koicbbatsov
authored andcommitted
Add "Bitwise Predicate Methods" rule
Follow up rubocop/rubocop#13050. This PR adds "Bitwise Predicate Methods" rule. Prefer the use of bitwise predicate methods to bitwise operations involving comparisons. ```ruby # bad - checks any set bits (variable & flags).positive? # good variable.anybits?(flags) # bad - checks all set bits (variable & flags) == flags # good variable.allbits?(flags) # bad - checks no set bits (variable & flags).zero? (variable & flags) == 0 # good variable.nobits?(flags) ```
1 parent 79ad25a commit e66a93a

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

README.adoc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5808,6 +5808,32 @@ if x == 0
58085808
end
58095809
----
58105810

5811+
=== Bitwise Predicate Methods [[bitwise-predicate-methods]]
5812+
5813+
Prefer bitwise predicate methods over direct comparison operations.
5814+
5815+
[source,ruby]
5816+
----
5817+
# bad - checks any set bits
5818+
(variable & flags).positive?
5819+
5820+
# good
5821+
variable.anybits?(flags)
5822+
5823+
# bad - checks all set bits
5824+
(variable & flags) == flags
5825+
5826+
# good
5827+
variable.allbits?(flags)
5828+
5829+
# bad - checks no set bits
5830+
(variable & flags).zero?
5831+
(variable & flags) == 0
5832+
5833+
# good
5834+
variable.nobits?(flags)
5835+
----
5836+
58115837
=== No Cryptic Perlisms [[no-cryptic-perlisms]]
58125838

58135839
Avoid using Perl-style special variables (like `$:`, `$;`, etc).

0 commit comments

Comments
 (0)