Skip to content

Commit

Permalink
[Fix rubocop#3460] Fix false positives in Style/InlineComment cop
Browse files Browse the repository at this point in the history
This cop's check was based on a misunderstanding of how
`Parser::Source::Comment#inline?` works, and was missing a test
case for the negative case.

This change fixes the cop and adds a test for the future.
  • Loading branch information
Drenmi committed Sep 17, 2016
1 parent 57273d9 commit b9da390
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
* [#3439](https://github.com/bbatsov/rubocop/issues/3439): Fix variable assignment check not working properly when a block is used in `Rails/SaveBang`. ([@QuinnHarris][])
* [#3401](https://github.com/bbatsov/rubocop/issues/3401): Read file contents in binary mode so `Style/EndOfLine` works on Windows. ([@jonas054][])
* [#3450](https://github.com/bbatsov/rubocop/issues/3450): Prevent `Style/TernaryParentheses` cop from making unsafe corrections. ([@drenmi][])
* [#3460](https://github.com/bbatsov/rubocop/issues/3460): Fix false positives in `Style/InlineComment` cop. ([@drenmi][])

### Changes

Expand Down
2 changes: 1 addition & 1 deletion config/disabled.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Style/ImplicitRuntimeError:
Enabled: false

Style/InlineComment:
Description: 'Avoid inline comments.'
Description: 'Avoid trailing inline comments.'
Enabled: false

Style/MethodCalledOnDoEndBlock:
Expand Down
19 changes: 16 additions & 3 deletions lib/rubocop/cop/style/inline_comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,26 @@
module RuboCop
module Cop
module Style
# This cop checks for inline comments.
# This cop checks for trailing inline comments.
#
# @example
#
# # good
# foo.each do |f|
# # Standalone comment
# f.bar
# end
#
# # bad
# foo.each do |f|
# f.bar # Trailing inline comment
# end
class InlineComment < Cop
MSG = 'Avoid inline comments.'.freeze
MSG = 'Avoid trailing inline comments.'.freeze

def investigate(processed_source)
processed_source.comments.each do |comment|
next unless comment.inline?
next if comment_line?(processed_source[comment.loc.line - 1])
add_offense(comment, :expression)
end
end
Expand Down
15 changes: 11 additions & 4 deletions spec/rubocop/cop/style/inline_comment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@
describe RuboCop::Cop::Style::InlineComment do
subject(:cop) { described_class.new }

it 'registers an offense for a inline comment' do
inspect_source(cop, 'two = 1 + 1 # An inline comment')
it 'registers an offense for a trailing inline comment' do
inspect_source(cop, 'two = 1 + 1 # A trailing inline comment')

expect(cop.messages).to eq(['Avoid inline comments.'])
expect(cop.highlights).to eq(['# An inline comment'])
expect(cop.offenses.size).to eq(1)
expect(cop.messages).to eq(['Avoid trailing inline comments.'])
expect(cop.highlights).to eq(['# A trailing inline comment'])
end

it 'does not register an offense for a standalone comment' do
inspect_source(cop, '# A standalone comment')

expect(cop.offenses).to be_empty
end
end

0 comments on commit b9da390

Please sign in to comment.