Skip to content

Commit

Permalink
Merge pull request #965 from koic/fix_a_false_negative_for_rails_output
Browse files Browse the repository at this point in the history
[Fix #934] Fix a false negative for `Rails/Output`
  • Loading branch information
koic authored Mar 30, 2023
2 parents 968331c + cd3506a commit 7131e98
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog/fix_a_false_negative_for_rails_output.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#934](https://github.com/rubocop/rubocop-rails/issues/934): Fix a false negative for `Rails/Output` when print methods without arguments. ([@koic][])
3 changes: 2 additions & 1 deletion lib/rubocop/cop/rails/output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ class Output < Base
PATTERN

def on_send(node)
return unless (output?(node) || io_output?(node)) && node.arguments?
return if node.parent&.call_type?
return unless output?(node) || io_output?(node)

range = offense_range(node)

Expand Down
22 changes: 20 additions & 2 deletions spec/rubocop/cop/rails/output_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,31 @@
RUBY
end

it 'does not record an offense for methods without arguments' do
expect_no_offenses(<<~RUBY)
it 'registers an offense for methods without arguments' do
expect_offense(<<~RUBY)
print
^^^^^ Do not write to stdout. Use Rails's logger if you want to log.
pp
^^ Do not write to stdout. Use Rails's logger if you want to log.
puts
^^^^ Do not write to stdout. Use Rails's logger if you want to log.
$stdout.write
^^^^^^^^^^^^^ Do not write to stdout. Use Rails's logger if you want to log.
STDERR.write
^^^^^^^^^^^^ Do not write to stdout. Use Rails's logger if you want to log.
RUBY
end

it 'does not register an offense when a method is called to a local variable with the same name as a print method' do
expect_no_offenses(<<~RUBY)
p.do_something
RUBY
end

it 'does not register an offense when a method is ' \
'safe navigation called to a local variable with the same name as a print method' do
expect_no_offenses(<<~RUBY)
p&.do_something
RUBY
end

Expand Down

0 comments on commit 7131e98

Please sign in to comment.