Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#7077] Cop name standardisation: Rename Unneeded* cops to Redundant #7460

Merged
merged 12 commits into from Oct 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -13,6 +13,7 @@
### Changes

* [#7446](https://github.com/rubocop-hq/rubocop/issues/7446): Add `merge` to list of non-mutating methods. ([@cstyles][])
* [#7077](https://github.com/rubocop-hq/rubocop/issues/7077): Rename `Unneeded*` cops to `Redundant*` (e.g., `Style/UnneededPercentQ` becomes `Style/RedundantPercentQ`). ([@scottmatthewman][])

## 0.75.1 (2019-10-14)

Expand Down
104 changes: 51 additions & 53 deletions config/default.yml
Expand Up @@ -1512,6 +1512,29 @@ Lint/RandOne:
Enabled: true
VersionAdded: '0.36'

Lint/RedundantCopDisableDirective:
Description: >-
Checks for rubocop:disable comments that can be removed.
Note: this cop is not disabled when disabling all cops.
It must be explicitly disabled.
Enabled: true
VersionAdded: '0.76'

Lint/RedundantCopEnableDirective:
Description: Checks for rubocop:enable comments that can be removed.
Enabled: true
VersionAdded: '0.76'

Lint/RedundantRequireStatement:
Description: 'Checks for unnecessary `require` statement.'
Enabled: true
VersionAdded: '0.76'

Lint/RedundantSplatExpansion:
Description: 'Checks for splat unnecessarily being called on literals.'
Enabled: true
VersionChanged: '0.76'

Lint/RedundantWithIndex:
Description: 'Checks for redundant `with_index`.'
Enabled: true
Expand Down Expand Up @@ -1645,30 +1668,6 @@ Lint/UnifiedInteger:
Enabled: true
VersionAdded: '0.43'

Lint/UnneededCopDisableDirective:
Description: >-
Checks for rubocop:disable comments that can be removed.
Note: this cop is not disabled when disabling all cops.
It must be explicitly disabled.
Enabled: true
VersionAdded: '0.53'

Lint/UnneededCopEnableDirective:
Description: Checks for rubocop:enable comments that can be removed.
Enabled: true
VersionAdded: '0.53'

Lint/UnneededRequireStatement:
Description: 'Checks for unnecessary `require` statement.'
Enabled: true
VersionAdded: '0.51'

Lint/UnneededSplatExpansion:
Description: 'Checks for splat unnecessarily being called on literals.'
Enabled: true
VersionAdded: '0.43'
VersionChanged: '0.74'

Lint/UnreachableCode:
Description: 'Unreachable code.'
Enabled: true
Expand Down Expand Up @@ -3402,6 +3401,16 @@ Style/RedundantBegin:
VersionAdded: '0.10'
VersionChanged: '0.21'

Style/RedundantCapitalW:
Description: 'Checks for %W when interpolation is not needed.'
Enabled: true
VersionAdded: '0.76'

Style/RedundantCondition:
Description: 'Checks for unnecessary conditional expressions.'
Enabled: true
VersionAdded: '0.76'

Style/RedundantConditional:
Description: "Don't return true/false from a conditional."
Enabled: true
Expand All @@ -3420,11 +3429,22 @@ Style/RedundantFreeze:
VersionAdded: '0.34'
VersionChanged: '0.66'

Style/RedundantInterpolation:
Description: 'Checks for strings that are just an interpolated expression.'
Enabled: true
VersionAdded: '0.76'

Style/RedundantParentheses:
Description: "Checks for parentheses that seem not to serve any purpose."
Enabled: true
VersionAdded: '0.36'

Style/RedundantPercentQ:
Description: 'Checks for %q/%Q when single quotes or double quotes would do.'
StyleGuide: '#percent-q'
Enabled: true
VersionAdded: '0.76'

Style/RedundantReturn:
Description: "Don't use return where it's not required."
StyleGuide: '#no-explicit-return'
Expand All @@ -3441,6 +3461,13 @@ Style/RedundantSelf:
VersionAdded: '0.10'
VersionChanged: '0.13'

Style/RedundantSort:
Description: >-
Use `min` instead of `sort.first`,
`max_by` instead of `sort_by...last`, etc.
Enabled: true
VersionAdded: '0.76'

Style/RedundantSortBy:
Description: 'Use `sort` instead of `sort_by { |x| x }`.'
Enabled: true
Expand Down Expand Up @@ -3828,35 +3855,6 @@ Style/UnlessElse:
Enabled: true
VersionAdded: '0.9'

Style/UnneededCapitalW:
Description: 'Checks for %W when interpolation is not needed.'
Enabled: true
VersionAdded: '0.21'
VersionChanged: '0.24'

Style/UnneededCondition:
Description: 'Checks for unnecessary conditional expressions.'
Enabled: true
VersionAdded: '0.57'

Style/UnneededInterpolation:
Description: 'Checks for strings that are just an interpolated expression.'
Enabled: true
VersionAdded: '0.36'

Style/UnneededPercentQ:
Description: 'Checks for %q/%Q when single quotes or double quotes would do.'
StyleGuide: '#percent-q'
Enabled: true
VersionAdded: '0.24'

Style/UnneededSort:
Description: >-
Use `min` instead of `sort.first`,
`max_by` instead of `sort_by...last`, etc.
Enabled: true
VersionAdded: '0.55'

Style/UnpackFirst:
Description: >-
Checks for accessing the first element of `String#unpack`
Expand Down
18 changes: 9 additions & 9 deletions lib/rubocop.rb
Expand Up @@ -322,6 +322,10 @@
require_relative 'rubocop/cop/lint/percent_string_array'
require_relative 'rubocop/cop/lint/percent_symbol_array'
require_relative 'rubocop/cop/lint/rand_one'
require_relative 'rubocop/cop/lint/redundant_cop_disable_directive'
require_relative 'rubocop/cop/lint/redundant_cop_enable_directive'
require_relative 'rubocop/cop/lint/redundant_require_statement'
require_relative 'rubocop/cop/lint/redundant_splat_expansion'
require_relative 'rubocop/cop/lint/redundant_with_index'
require_relative 'rubocop/cop/lint/redundant_with_object'
require_relative 'rubocop/cop/lint/regexp_as_condition'
Expand All @@ -342,10 +346,6 @@
require_relative 'rubocop/cop/lint/to_json'
require_relative 'rubocop/cop/lint/underscore_prefixed_variable_name'
require_relative 'rubocop/cop/lint/unified_integer'
require_relative 'rubocop/cop/lint/unneeded_cop_disable_directive'
require_relative 'rubocop/cop/lint/unneeded_cop_enable_directive'
require_relative 'rubocop/cop/lint/unneeded_require_statement'
require_relative 'rubocop/cop/lint/unneeded_splat_expansion'
require_relative 'rubocop/cop/lint/unreachable_code'
require_relative 'rubocop/cop/lint/unused_block_argument'
require_relative 'rubocop/cop/lint/unused_method_argument'
Expand Down Expand Up @@ -503,12 +503,17 @@
require_relative 'rubocop/cop/style/raise_args'
require_relative 'rubocop/cop/style/random_with_offset'
require_relative 'rubocop/cop/style/redundant_begin'
require_relative 'rubocop/cop/style/redundant_capital_w'
require_relative 'rubocop/cop/style/redundant_condition'
require_relative 'rubocop/cop/style/redundant_conditional'
require_relative 'rubocop/cop/style/redundant_exception'
require_relative 'rubocop/cop/style/redundant_freeze'
require_relative 'rubocop/cop/style/redundant_interpolation'
require_relative 'rubocop/cop/style/redundant_parentheses'
require_relative 'rubocop/cop/style/redundant_percent_q'
require_relative 'rubocop/cop/style/redundant_return'
require_relative 'rubocop/cop/style/redundant_self'
require_relative 'rubocop/cop/style/redundant_sort'
require_relative 'rubocop/cop/style/redundant_sort_by'
require_relative 'rubocop/cop/style/regexp_literal'
require_relative 'rubocop/cop/style/rescue_modifier'
Expand Down Expand Up @@ -545,11 +550,6 @@
require_relative 'rubocop/cop/style/trailing_underscore_variable'
require_relative 'rubocop/cop/style/trivial_accessors'
require_relative 'rubocop/cop/style/unless_else'
require_relative 'rubocop/cop/style/unneeded_capital_w'
require_relative 'rubocop/cop/style/unneeded_condition'
require_relative 'rubocop/cop/style/unneeded_interpolation'
require_relative 'rubocop/cop/style/unneeded_percent_q'
require_relative 'rubocop/cop/style/unneeded_sort'
require_relative 'rubocop/cop/style/unpack_first'
require_relative 'rubocop/cop/style/variable_interpolation'
require_relative 'rubocop/cop/style/when_then'
Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/comment_config.rb
Expand Up @@ -4,7 +4,7 @@ module RuboCop
# This class parses the special `rubocop:disable` comments in a source
# and provides a way to check if each cop is enabled at arbitrary line.
class CommentConfig
UNNEEDED_DISABLE = 'Lint/UnneededCopDisableDirective'
REDUNDANT_DISABLE = 'Lint/RedundantCopDisableDirective'

COP_NAME_PATTERN = '([A-Z]\w+/)?(?:[A-Z]\w+)'
COP_NAMES_PATTERN = "(?:#{COP_NAME_PATTERN} , )*#{COP_NAME_PATTERN}"
Expand Down Expand Up @@ -152,7 +152,7 @@ def qualified_cop_name(cop_name)
end

def all_cop_names
@all_cop_names ||= Cop::Cop.registry.names - [UNNEEDED_DISABLE]
@all_cop_names ||= Cop::Cop.registry.names - [REDUNDANT_DISABLE]
end

def comment_only_line?(line_number)
Expand Down
9 changes: 9 additions & 0 deletions lib/rubocop/config_obsoletion.rb
Expand Up @@ -4,10 +4,19 @@ module RuboCop
# This class handles obsolete configuration.
class ConfigObsoletion
RENAMED_COPS = {
'Lint/UnneededCopDisableDirective' => 'Lint/RedundantCopDisableDirective',
'Lint/UnneededCopEnableDirective' => 'Lint/RedundantCopEnableDirective',
'Lint/UnneededRequireStatement' => 'Lint/RedundantRequireStatement',
'Lint/UnneededSplatExpansion' => 'Lint/RedundantSplatExpansion',
'Style/SingleSpaceBeforeFirstArg' => 'Layout/SpaceBeforeFirstArg',
'Style/MethodCallParentheses' => 'Style/MethodCallWithoutArgsParentheses',
'Style/DeprecatedHashMethods' => 'Style/PreferredHashMethods',
'Style/OpMethod' => 'Naming/BinaryOperatorParameterName',
'Style/UnneededCapitalW' => 'Style/RedundantCapitalW',
'Style/UnneededCondition' => 'Style/RedundantCondition',
'Style/UnneededInterpolation' => 'Style/RedundantInterpolation',
'Style/UnneededPercentQ' => 'Style/RedundantPercentQ',
'Style/UnneededSort' => 'Style/RedundantSort',
'Layout/FirstParameterIndentation' => 'Layout/IndentFirstArgument',
'Layout/IndentArray' => 'Layout/IndentFirstArrayElement',
'Layout/IndentHash' => 'Layout/IndentFirstHashElement'
Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/lint/missing_cop_enable_directive.rb
@@ -1,6 +1,6 @@
# frozen_string_literal: true

# rubocop:disable Lint/UnneededCopDisableDirective
# rubocop:disable Lint/RedundantCopDisableDirective
module RuboCop
module Cop
module Lint
Expand Down Expand Up @@ -81,4 +81,4 @@ def message(max_range:, cop:)
end
end
end
# rubocop:enable Lint/UnneededCopDisableDirective, Layout/SpaceAroundOperators
# rubocop:enable Lint/RedundantCopDisableDirective, Layout/SpaceAroundOperators
@@ -1,8 +1,8 @@
# frozen_string_literal: true

# The Lint/UnneededCopDisableDirective cop needs to be disabled so as
# to be able to provide a (bad) example of an unneeded disable.
# rubocop:disable Lint/UnneededCopDisableDirective
# The Lint/RedundantCopDisableDirective cop needs to be disabled so as
# to be able to provide a (bad) example of a redundant disable.
# rubocop:disable Lint/RedundantCopDisableDirective
module RuboCop
module Cop
module Lint
Expand All @@ -25,21 +25,21 @@ module Lint
#
# # good
# x += 1
class UnneededCopDisableDirective < Cop
class RedundantCopDisableDirective < Cop
include NameSimilarity
include RangeHelp

COP_NAME = 'Lint/UnneededCopDisableDirective'
COP_NAME = 'Lint/RedundantCopDisableDirective'

def check(offenses, cop_disabled_line_ranges, comments)
unneeded_cops = Hash.new { |h, k| h[k] = Set.new }
redundant_cops = Hash.new { |h, k| h[k] = Set.new }

each_unneeded_disable(cop_disabled_line_ranges,
offenses, comments) do |comment, unneeded_cop|
unneeded_cops[comment].add(unneeded_cop)
each_redundant_disable(cop_disabled_line_ranges,
offenses, comments) do |comment, redundant_cop|
redundant_cops[comment].add(redundant_cop)
end

add_offenses(unneeded_cops)
add_offenses(redundant_cops)
end

def autocorrect(args)
Expand Down Expand Up @@ -89,8 +89,8 @@ def directive_range_in_list(range, ranges)
newlines: false)
end

def each_unneeded_disable(cop_disabled_line_ranges, offenses, comments,
&block)
def each_redundant_disable(cop_disabled_line_ranges, offenses, comments,
&block)
disabled_ranges = cop_disabled_line_ranges[COP_NAME] || [0..0]

cop_disabled_line_ranges.each do |cop, line_ranges|
Expand All @@ -110,9 +110,9 @@ def each_line_range(line_ranges, disabled_ranges, offenses, comments,
comment = comments.find { |c| c.loc.line == line_range.begin }
next if ignore_offense?(disabled_ranges, line_range)

unneeded_cop = find_unneeded(comment, offenses, cop, line_range,
line_ranges[ix + 1])
yield comment, unneeded_cop if unneeded_cop
redundant_cop = find_redundant(comment, offenses, cop, line_range,
line_ranges[ix + 1])
yield comment, redundant_cop if redundant_cop
end
end

Expand All @@ -123,24 +123,24 @@ def each_already_disabled(line_ranges, disabled_ranges, comments)

# If a cop is disabled in a range that begins on the same line as
# the end of the previous range, it means that the cop was
# already disabled by an earlier comment. So it's unneeded
# already disabled by an earlier comment. So it's redundant
# whether there are offenses or not.
unneeded_comment = comments.find do |c|
redundant_comment = comments.find do |c|
c.loc.line == range.begin &&
# Comments disabling all cops don't count since it's reasonable
# to disable a few select cops first and then all cops further
# down in the code.
!all_disabled?(c)
end
yield unneeded_comment if unneeded_comment
yield redundant_comment if redundant_comment
end
end

def find_unneeded(comment, offenses, cop, line_range, next_line_range)
def find_redundant(comment, offenses, cop, line_range, next_line_range)
if all_disabled?(comment)
# If there's a disable all comment followed by a comment
# specifically disabling `cop`, we don't report the `all`
# comment. If the disable all comment is truly unneeded, we will
# comment. If the disable all comment is truly redundant, we will
# detect that when examining the comments of another cop, and we
# get the full line range for the disable all.
if (next_line_range.nil? ||
Expand Down Expand Up @@ -170,8 +170,8 @@ def directive_count(comment)
cops_string.split(/,\s*/).size
end

def add_offenses(unneeded_cops)
unneeded_cops.each do |comment, cops|
def add_offenses(redundant_cops)
redundant_cops.each do |comment, cops|
if all_disabled?(comment) ||
directive_count(comment) == cops.size
add_offense_for_entire_comment(comment, cops)
Expand Down Expand Up @@ -260,4 +260,4 @@ def ends_its_line?(range)
end
end
end
# rubocop:enable Lint/UnneededCopDisableDirective
# rubocop:enable Lint/RedundantCopDisableDirective
@@ -1,9 +1,9 @@
# frozen_string_literal: true

# The Lint/UnneededCopEnableDirective cop needs to be disabled so as
# The Lint/RedundantCopEnableDirective cop needs to be disabled so as
# to be able to provide a (bad) example of an unneeded enable.

# rubocop:disable Lint/UnneededCopEnableDirective
# rubocop:disable Lint/RedundantCopEnableDirective
module RuboCop
module Cop
module Lint
Expand Down Expand Up @@ -32,7 +32,7 @@ module Lint
# baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaarrrrrrrrrrrrr
# # rubocop:enable all
# baz
class UnneededCopEnableDirective < Cop
class RedundantCopEnableDirective < Cop
include RangeHelp
include SurroundingSpace

Expand Down