Skip to content

Commit a4df1b9

Browse files
authored
Merge pull request #1141 from koic/fix_a_false_positive_for_rails_output
[Fix #1041] Fix a false positive for `Rails/Output`
2 parents e773322 + 6f72227 commit a4df1b9

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1041](https://github.com/rubocop/rubocop-rails/issues/1041): Fix a false positive for `Rails/Output` when output method is called with block argument. ([@koic][])

lib/rubocop/cop/rails/output.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Output < Base
2323

2424
MSG = "Do not write to stdout. Use Rails's logger if you want to log."
2525
RESTRICT_ON_SEND = %i[ap p pp pretty_print print puts binwrite syswrite write write_nonblock].freeze
26+
ALLOWED_TYPES = %i[send csend block numblock].freeze
2627

2728
def_node_matcher :output?, <<~PATTERN
2829
(send nil? {:ap :p :pp :pretty_print :print :puts} ...)
@@ -39,8 +40,8 @@ class Output < Base
3940
PATTERN
4041

4142
def on_send(node)
42-
return if node.parent&.call_type?
43-
return unless output?(node) || io_output?(node)
43+
return if ALLOWED_TYPES.include?(node.parent&.type)
44+
return if !output?(node) && !io_output?(node)
4445

4546
range = offense_range(node)
4647

spec/rubocop/cop/rails/output_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,27 @@
140140
RUBY
141141
end
142142

143+
it 'does not register an offense when the `p` method is called with block argument' do
144+
expect_no_offenses(<<~RUBY)
145+
# phlex-rails gem.
146+
div do
147+
p { 'Some text' }
148+
end
149+
RUBY
150+
end
151+
152+
it 'does not register an offense when io method is called with block argument' do
153+
expect_no_offenses(<<~RUBY)
154+
obj.write { do_somethig }
155+
RUBY
156+
end
157+
158+
it 'does not register an offense when io method is called with numbered block argument' do
159+
expect_no_offenses(<<~RUBY)
160+
obj.write { do_something(_1) }
161+
RUBY
162+
end
163+
143164
it 'does not register an offense when a method is ' \
144165
'safe navigation called to a local variable with the same name as a print method' do
145166
expect_no_offenses(<<~RUBY)

0 commit comments

Comments
 (0)