diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a0dc2eebd1..99e53e2389d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ * [#7439](https://github.com/rubocop-hq/rubocop/issues/7439): Make `Style/FormatStringToken` ignore percent escapes (`%%`). ([@buehmann][]) * [#7438](https://github.com/rubocop-hq/rubocop/issues/7438): Fix assignment edge-cases in `Layout/MultilineAssignmentLayout`. ([@gsamokovarov][]) +* [#7449](https://github.com/rubocop-hq/rubocop/pull/7449): Make `Style/IfUnlessModifier` respect `rubocop:disable` comments for `Metrics/LineLength`. ([@jonas054][]) ## 0.75.1 (2019-10-14) diff --git a/lib/rubocop/cop/style/if_unless_modifier.rb b/lib/rubocop/cop/style/if_unless_modifier.rb index f2c0befd546..29e2b326531 100644 --- a/lib/rubocop/cop/style/if_unless_modifier.rb +++ b/lib/rubocop/cop/style/if_unless_modifier.rb @@ -70,8 +70,15 @@ def too_long_single_line?(node) return false unless max_line_length range = node.source_range - range.first_line == range.last_line && - range.last_column > max_line_length + return false unless range.first_line == range.last_line + return false unless line_length_enabled_at_line?(range.first_line) + + range.last_column > max_line_length + end + + def line_length_enabled_at_line?(line) + processed_source.comment_config + .cop_enabled_at_line?('Metrics/LineLength', line) end def named_capture_in_condition?(node) diff --git a/spec/rubocop/cop/style/if_unless_modifier_spec.rb b/spec/rubocop/cop/style/if_unless_modifier_spec.rb index 73fb65c014d..2bdac1735a1 100644 --- a/spec/rubocop/cop/style/if_unless_modifier_spec.rb +++ b/spec/rubocop/cop/style/if_unless_modifier_spec.rb @@ -36,7 +36,7 @@ def f end end - context 'when Metrics/LineLength is disabled' do + context 'when Metrics/LineLength is disabled in configuration' do let(:line_length_config) { { 'Enabled' => false, 'Max' => 80 } } it 'accepts' do @@ -47,6 +47,18 @@ def f RUBY end end + + context 'when Metrics/LineLength is disabled with a comment' do + it 'accepts' do + expect_no_offenses(<<~RUBY) + def f + # rubocop:disable Metrics/LineLength + #{source} + # rubocop:enable Metrics/LineLength + end + RUBY + end + end end context 'multiline if that fits on one line' do