diff --git a/CHANGELOG.md b/CHANGELOG.md index 02e91f90df9..621e510d4b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ * [#7809](https://github.com/rubocop-hq/rubocop/pull/7809): Add auto-correction for `Style/EndBlock` cop. ([@tejasbubane][]) * [#7739](https://github.com/rubocop-hq/rubocop/pull/7739): Add `IgnoreNotImplementedMethods` configuration to `Lint/UnusedMethodArgument`. ([@tejasbubane][]) * [#7740](https://github.com/rubocop-hq/rubocop/issues/7740): Add `AllowModifiersOnSymbols` configuration to `Style/AccessModifierDeclarations`. ([@tejasbubane][]) +* [#7812](https://github.com/rubocop-hq/rubocop/pull/7812): Add auto-correction for `Lint/BooleanSymbol` cop. ([@tejasbubane][]) ### Bug fixes diff --git a/config/default.yml b/config/default.yml index 2663f926824..0f085fbc3e0 100644 --- a/config/default.yml +++ b/config/default.yml @@ -1323,6 +1323,7 @@ Lint/BooleanSymbol: Description: 'Check for `:true` and `:false` symbols.' Enabled: true VersionAdded: '0.50' + VersionChanged: '0.81' Lint/CircularArgumentReference: Description: "Default values in optional keyword arguments and optional ordinal arguments should not refer back to the name of the argument." diff --git a/lib/rubocop/cop/lint/boolean_symbol.rb b/lib/rubocop/cop/lint/boolean_symbol.rb index aec8f0beb6d..3ed1df85d5a 100644 --- a/lib/rubocop/cop/lint/boolean_symbol.rb +++ b/lib/rubocop/cop/lint/boolean_symbol.rb @@ -32,6 +32,18 @@ def on_sym(node) add_offense(node, message: format(MSG, boolean: node.value)) end + + def autocorrect(node) + lambda do |corrector| + boolean_literal = node.source.delete(':') + parent = node.parent + if parent&.pair_type? + corrector.remove(parent.loc.operator) + boolean_literal = "#{node.source} =>" + end + corrector.replace(node.loc.expression, boolean_literal) + end + end end end end diff --git a/manual/cops_lint.md b/manual/cops_lint.md index 2049f1a8cfa..21748409a59 100644 --- a/manual/cops_lint.md +++ b/manual/cops_lint.md @@ -166,7 +166,7 @@ BigDecimal(123.456, 3) Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged --- | --- | --- | --- | --- -Enabled | Yes | No | 0.50 | - +Enabled | Yes | Yes | 0.50 | 0.81 This cop checks for `:true` and `:false` symbols. In most cases it would be a typo. diff --git a/spec/rubocop/cop/lint/boolean_symbol_spec.rb b/spec/rubocop/cop/lint/boolean_symbol_spec.rb index 849e4c76b5e..c9eb571a798 100644 --- a/spec/rubocop/cop/lint/boolean_symbol_spec.rb +++ b/spec/rubocop/cop/lint/boolean_symbol_spec.rb @@ -8,6 +8,10 @@ :true ^^^^^ Symbol with a boolean name - you probably meant to use `true`. RUBY + + expect_correction(<<~RUBY) + true + RUBY end it 'registers an offense when using `:false`' do @@ -15,6 +19,10 @@ :false ^^^^^^ Symbol with a boolean name - you probably meant to use `false`. RUBY + + expect_correction(<<~RUBY) + false + RUBY end context 'when using the new hash syntax' do @@ -23,13 +31,21 @@ { true: 'Foo' } ^^^^ Symbol with a boolean name - you probably meant to use `true`. RUBY + + expect_correction(<<~RUBY) + { true => 'Foo' } + RUBY end it 'registers an offense when using `false:`' do expect_offense(<<~RUBY) - { false: 'Bar' } + { false: :bar } ^^^^^ Symbol with a boolean name - you probably meant to use `false`. RUBY + + expect_correction(<<~RUBY) + { false => :bar } + RUBY end end