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 "Bitwise Predicate Methods" rule #944

Merged
merged 1 commit into from
Jul 18, 2024

Conversation

koic
Copy link
Member

@koic koic commented Jul 18, 2024

Follow up rubocop/rubocop#13050.

This PR adds "Bitwise Predicate Methods" rule.

Prefer the use of bitwise predicate methods to bitwise operations involving comparisons.

# 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)

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)
```
koic added a commit to koic/rubocop that referenced this pull request Jul 18, 2024
Closes rubocop#13050.

This PR adds new `Style/BitwisePredicates` cop, which checks for usage of `&` operator
in 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)
```

I've opened a proposal in the style guide at rubocop/ruby-style-guide#944.
koic added a commit to koic/rubocop that referenced this pull request Jul 18, 2024
Closes rubocop#13050.

This PR adds new `Style/BitwisePredicates` cop, which checks for usage of `&` operator
in 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)
```

I've opened a proposal in the style guide at rubocop/ruby-style-guide#944.
koic added a commit to koic/rubocop that referenced this pull request Jul 18, 2024
Closes rubocop#13050.

This PR adds new `Style/BitwisePredicates` cop, which checks for usage of `&` operator
in 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)
```

I've opened a proposal in the style guide at rubocop/ruby-style-guide#944.
koic added a commit to koic/rubocop that referenced this pull request Jul 18, 2024
Closes rubocop#13050.

This PR adds new `Style/BitwisePredicates` cop, which checks for usage of `&` operator
in 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)
```

I've opened a proposal in the style guide at rubocop/ruby-style-guide#944.
koic added a commit to koic/rubocop that referenced this pull request Jul 18, 2024
Closes rubocop#13050.

This PR adds new `Style/BitwisePredicates` cop, which checks for usage of `&` operator
in 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)
```

I've opened a proposal in the style guide at rubocop/ruby-style-guide#944.
Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@bbatsov bbatsov merged commit e66a93a into rubocop:master Jul 18, 2024
3 checks passed
@bbatsov
Copy link
Collaborator

bbatsov commented Jul 18, 2024

Nice addition!

@koic koic deleted the add_bitwise_predicate_rule branch July 18, 2024 06:29
koic added a commit to koic/rubocop that referenced this pull request Jul 18, 2024
Closes rubocop#13050.

This PR adds new `Style/BitwisePredicates` cop, which checks for usage of `&` operator
in 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)
```

I've opened a proposal in the style guide at rubocop/ruby-style-guide#944.
koic added a commit to koic/rubocop that referenced this pull request Jul 20, 2024
Closes rubocop#13050.

This PR adds new `Style/BitwisePredicates` cop, which checks for usage of `&` operator
in 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)
```

I've opened a proposal in the style guide at rubocop/ruby-style-guide#944.
koic added a commit to koic/rubocop that referenced this pull request Jul 20, 2024
Closes rubocop#13050.

This PR adds new `Style/BitwisePredicates` cop, which checks for usage of `&` operator
in 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)
```

I've opened a proposal in the style guide at rubocop/ruby-style-guide#944.
bbatsov pushed a commit to rubocop/rubocop that referenced this pull request Oct 19, 2024
Closes #13050.

This PR adds new `Style/BitwisePredicates` cop, which checks for usage of `&` operator
in 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)
```

I've opened a proposal in the style guide at rubocop/ruby-style-guide#944.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants