Skip to content

Commit

Permalink
[Fix rubocop#7537] Fix a false positive for `Layout/SpaceAroundOperat…
Browse files Browse the repository at this point in the history
…ors`

Fixes rubocop#7537.

This PR fixes a false positive for `Layout/SpaceAroundOperators` when
using a Rational literal with `/` (e.g. `2/3r`).
  • Loading branch information
koic committed Dec 4, 2019
1 parent c50e747 commit 16b75b3
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* [#7534](https://github.com/rubocop-hq/rubocop/issues/7534): Fix an incorrect autocorrect for `Style/BlockDelimiters` cop and `Layout/SpaceBeforeBlockBraces` cop with `EnforcedStyle: no_space` when using multiline braces. ([@koic][])
* [#7231](https://github.com/rubocop-hq/rubocop/issues/7231): Fix the exit code to be `2` rather when `0` when the config file contains an unknown cop. ([@jethroo][])
* [#7513](https://github.com/rubocop-hq/rubocop/issues/7513): Fix abrupt error on autocorrecting with `--disable-uncorrectable`. ([@tejasbubane][])
* [#7537](https://github.com/rubocop-hq/rubocop/issues/7537): Fix a false positive for `Layout/SpaceAroundOperators` when using a Rational literal with `/` (e.g. `2/3r`). ([@koic][])

## 0.77.0 (2019-11-27)

Expand Down
1 change: 1 addition & 0 deletions lib/rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
require_relative 'rubocop/cop/mixin/percent_literal'
require_relative 'rubocop/cop/mixin/preceding_following_alignment'
require_relative 'rubocop/cop/mixin/preferred_delimiters'
require_relative 'rubocop/cop/mixin/rational_literal'
require_relative 'rubocop/cop/mixin/rescue_node'
require_relative 'rubocop/cop/mixin/safe_assignment'
require_relative 'rubocop/cop/mixin/space_after_punctuation'
Expand Down
3 changes: 3 additions & 0 deletions lib/rubocop/cop/layout/space_around_operators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module Layout
class SpaceAroundOperators < Cop
include PrecedingFollowingAlignment
include RangeHelp
include RationalLiteral

IRREGULAR_METHODS = %i[[] ! []=].freeze
EXCESSIVE_SPACE = ' '
Expand Down Expand Up @@ -53,6 +54,8 @@ def on_resbody(node)
end

def on_send(node)
return if rational_literal?(node)

if node.setter_method?
on_special_asgn(node)
elsif regular_operator?(node)
Expand Down
18 changes: 18 additions & 0 deletions lib/rubocop/cop/mixin/rational_literal.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

module RuboCop
module Cop
# Common functionality for handling Rational literals.
module RationalLiteral
extend NodePattern::Macros

private

def_node_matcher :rational_literal?, <<~PATTERN
(send
(int _) :/
(rational _))
PATTERN
end
end
end
4 changes: 4 additions & 0 deletions spec/rubocop/cop/layout/space_around_operators_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
expect_no_offenses('a, b = (1..2), (1...3)')
end

it 'accepts rational' do
expect_no_offenses('x = 2/3r')
end

it 'accepts scope operator' do
expect_no_offenses('@io.class == Zlib::GzipWriter')
end
Expand Down

0 comments on commit 16b75b3

Please sign in to comment.