Skip to content

Cop idea: Style/BitPredicates #13050

@zverok

Description

@zverok

Is your feature request related to a problem? Please describe.

Ruby 2.5 (!) introduced Integer#anybits?/#allbits?/#nobits? predicates. Accordingly, it would be cool if Rubocop suggested those corrections:

# 1. anybits?
# bad
(variable & MY_FLAGS).positive?
# good
variable.anybits?(MY_FLAGS)

# 2. allbits?
# bad
(variable & MY_FLAGS) == MY_FLAGS
# good
variable.allbits?(MY_FLAGS)

# 3. nobits?
# bad
(variable & MY_FLAGS).zero?
# good
variable.nobits?(MY_FLAGS)

I’ve seen a lot of codebases that use the bit-math version, and in many cases, predicates version seems to be clearer. It also has an educational value: I constantly see people writing bit-math to check flags seven years after methods introduction (because they simply don’t remember methods exist).

Haven’t yet time enough to work on the cop myself (well, I happen to have a draft branch, and I feel guilty for never finishing it, maybe I’ll do... but it is what it is) 😢

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions