From f34cc0e1a65c06b35c5523ae7048c72c41286b08 Mon Sep 17 00:00:00 2001 From: Ryan Rosenblum Date: Sun, 31 May 2020 05:38:19 -0400 Subject: [PATCH] [Fix #7971] Fix --disable-uncorrectable not updating uncorrected code (#8054) Co-authored-by: Bozhidar Batsov --- .rubocop_todo.yml | 2 +- CHANGELOG.md | 1 + lib/rubocop/cop/cop.rb | 15 +++++--- .../cli/cli_disable_uncorrectable_spec.rb | 36 +++++++++++++++++++ 4 files changed, 49 insertions(+), 5 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 5a5aaeec95c..61e1f3b8470 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -13,7 +13,7 @@ InternalAffairs/NodeDestructuring: # Offense count: 48 # Configuration parameters: CountComments. Metrics/ClassLength: - Max: 186 + Max: 190 # Offense count: 198 # Configuration parameters: CountComments, ExcludedMethods. diff --git a/CHANGELOG.md b/CHANGELOG.md index 5309d5bc5da..fae20108e65 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ * [#8017](https://github.com/rubocop-hq/rubocop/pull/8017): Fix a false positive for `Lint/SuppressedException` when empty rescue with comment in `def`. ([@koic][]) * [#7990](https://github.com/rubocop-hq/rubocop/issues/7990): Fix resolving `inherit_gem` in remote configs. ([@CvX][]) * [#8035](https://github.com/rubocop-hq/rubocop/issues/8035): Fix a false positive for `Lint/DeprecatedOpenSSLConstant` when using double quoted string argument. ([@koic][]) +* [#7971](https://github.com/rubocop-hq/rubocop/issues/7971): Fix an issue where `--disable-uncorrectable` would not update uncorrected code with `rubocop:todo`. ([@rrosenblum][]) * [#8035](https://github.com/rubocop-hq/rubocop/issues/8035): Fix a false positive for `Lint/DeprecatedOpenSSLConstant` when argument is a variable, method, or consntant. ([@koic][]) ### Changes diff --git a/lib/rubocop/cop/cop.rb b/lib/rubocop/cop/cop.rb index 31bc3be6547..d5e5f2b526a 100644 --- a/lib/rubocop/cop/cop.rb +++ b/lib/rubocop/cop/cop.rb @@ -146,17 +146,24 @@ def duplicate_location?(location) @offenses.any? { |o| o.location == location } end - def correct(node) + def correct(node) # rubocop:disable Metrics/PerceivedComplexity, Metrics/MethodLength reason = reason_to_not_correct(node) return reason if reason @corrected_nodes[node] = true + if support_autocorrect? correction = autocorrect(node) - return :uncorrected unless correction - @corrections << Correction.new(correction, node, self) - :corrected + if correction + @corrections << Correction.new(correction, node, self) + :corrected + elsif disable_uncorrectable? + disable_uncorrectable(node) + :corrected_with_todo + else + :uncorrected + end elsif disable_uncorrectable? disable_uncorrectable(node) :corrected_with_todo diff --git a/spec/rubocop/cli/cli_disable_uncorrectable_spec.rb b/spec/rubocop/cli/cli_disable_uncorrectable_spec.rb index 5dfc1f55b83..53bfae6d945 100644 --- a/spec/rubocop/cli/cli_disable_uncorrectable_spec.rb +++ b/spec/rubocop/cli/cli_disable_uncorrectable_spec.rb @@ -64,6 +64,42 @@ RUBY end + it 'adds it when the cop supports autocorrect but does not correct the offense' do + create_file('example.rb', <<~RUBY) + def ordinary_method(some_arg) + puts 'Ignoring args' + end + + def method_with_keyword_arg(some_keyword_arg:) + puts 'Ignoring args' + end + RUBY + + expect(exit_code).to eq(0) + expect($stderr.string).to eq('') + expect($stdout.string).to eq(<<~OUTPUT) + == example.rb == + C: 1: 1: [Corrected] Style/FrozenStringLiteralComment: Missing frozen string literal comment. + W: 1: 21: [Corrected] Lint/UnusedMethodArgument: Unused method argument - some_arg. If it's necessary, use _ or _some_arg as an argument name to indicate that it won't be used. You can also write as ordinary_method(*) if you want the method to accept any arguments but don't care about them. + C: 2: 1: [Corrected] Layout/EmptyLineAfterMagicComment: Add an empty line after magic comments. + W: 5: 29: [Todo] Lint/UnusedMethodArgument: Unused method argument - some_keyword_arg. You can also write as method_with_keyword_arg(*) if you want the method to accept any arguments but don't care about them. + + 1 file inspected, 4 offenses detected, 4 offenses corrected + OUTPUT + + expect(IO.read('example.rb')).to eq(<<~RUBY) + # frozen_string_literal: true + + def ordinary_method(_some_arg) + puts 'Ignoring args' + end + + def method_with_keyword_arg(some_keyword_arg:) # rubocop:todo Lint/UnusedMethodArgument + puts 'Ignoring args' + end + RUBY + end + context 'and there are two offenses of the same kind on one line' do it 'adds a single one-line disable statement' do create_file('.rubocop.yml', <<~YAML)