Skip to content

Commit

Permalink
[Fix #7595] Make Style/NumericPredicate aware of ignored methods
Browse files Browse the repository at this point in the history
Fixes #7595.

This PR makes `Style/NumericPredicate` aware of ignored methods when
specifying ignored methods.

The following is a reproduction procedure.

```console
% cat .rubocop.yml
# .rubocop.yml
Style/NumericPredicate:
  EnforcedStyle: comparison
  IgnoredMethods: 'zero?'

% cat example.rb
if foo.zero?
  puts 'hello'
end

% bundle exec rubocop --only Style/NumericPredicate --cache false example.rb
Inspecting 1 file
C

Offenses:

example.rb:1:4: C: Style/NumericPredicate: Use foo == 0 instead of
foo.zero?.
if foo.zero?
   ^^^^^^^^^

1 file inspected, 1 offense detected
```

This PR resolves the issue caused by the callback node itself not
being checked.
  • Loading branch information
koic authored and bbatsov committed Dec 30, 2019
1 parent 25810c4 commit f84dd83
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* [#7590](https://github.com/rubocop-hq/rubocop/issues/7590): Fix an error for `Layout/SpaceBeforeBlockBraces` when using with `EnforcedStyle: line_count_based` of `Style/BlockDelimiters` cop. ([@koic][])
* [#7569](https://github.com/rubocop-hq/rubocop/issues/7569): Make `Style/YodaCondition` accept `__FILE__ == $0`. ([@koic][])
* [#7576](https://github.com/rubocop-hq/rubocop/issues/7576): Fix an error for `Gemspec/OrderedDependencies` when using a local variable in an argument of dependent gem. ([@koic][])
* [#7595](https://github.com/rubocop-hq/rubocop/issues/7595): Make `Style/NumericPredicate` aware of ignored methods when specifying ignored methods. ([@koic][])

## 0.78.0 (2019-12-18)

Expand Down
7 changes: 4 additions & 3 deletions lib/rubocop/cop/style/numeric_predicate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ class NumericPredicate < Cop
}.freeze

def on_send(node)
return if node.each_ancestor(:send, :block).any? do |ancestor|
ignored_method?(ancestor.method_name)
end
return if ignored_method?(node.method_name) ||
node.each_ancestor(:send, :block).any? do |ancestor|
ignored_method?(ancestor.method_name)
end

numeric, replacement = check(node)

Expand Down
36 changes: 36 additions & 0 deletions spec/rubocop/cop/style/numeric_predicate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,42 @@
}
end

context 'simple method call' do
context '`EnforcedStyle` is `predicate`' do
let(:cop_config) do
{
'EnforcedStyle' => 'predicate',
'IgnoredMethods' => %w[==]
}
end

context 'when checking if a number is zero' do
it_behaves_like 'code without offense', <<~RUBY
if number == 0
puts 'hello'
end
RUBY
end
end

context '`EnforcedStyle` is `comparison`' do
let(:cop_config) do
{
'EnforcedStyle' => 'comparison',
'IgnoredMethods' => %w[zero?]
}
end

context 'when checking if a number is zero' do
it_behaves_like 'code without offense', <<~RUBY
if number.zero?
puts 'hello'
end
RUBY
end
end
end

context 'in argument' do
context 'ignored method' do
context 'when checking if a number is positive' do
Expand Down

0 comments on commit f84dd83

Please sign in to comment.