diff --git a/CHANGELOG.md b/CHANGELOG.md index d6c9e308eb15..63313ab0891f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5350,3 +5350,4 @@ [@agargiulo]: https://github.com/agargiulo [@muirdm]: https://github.com/muirdm [@noon-ng]: https://github.com/noon-ng +[@ohbarye]: https://github.com/ohbarye diff --git a/changelog/fix_faulty_calculation_in_uncommunicative_name.md b/changelog/fix_faulty_calculation_in_uncommunicative_name.md new file mode 100644 index 000000000000..af556b002fb9 --- /dev/null +++ b/changelog/fix_faulty_calculation_in_uncommunicative_name.md @@ -0,0 +1 @@ +* [#8229](https://github.com/rubocop-hq/rubocop/issues/8229): Fix faulty calculation in UncommunicativeName. ([@ohbarye][]) diff --git a/lib/rubocop/cop/mixin/uncommunicative_name.rb b/lib/rubocop/cop/mixin/uncommunicative_name.rb index 20db9ae6d675..83ebca232788 100644 --- a/lib/rubocop/cop/mixin/uncommunicative_name.rb +++ b/lib/rubocop/cop/mixin/uncommunicative_name.rb @@ -24,7 +24,11 @@ def check(node, args) name = full_name.gsub(/\A(_+)/, '') next if allowed_names.include?(name) - range = arg_range(arg, name.size) + length = full_name.size + length += 1 if arg.restarg_type? + length += 2 if arg.kwrestarg_type? + + range = arg_range(arg, length) issue_offenses(node, range, name) end end diff --git a/spec/rubocop/cop/naming/block_parameter_name_spec.rb b/spec/rubocop/cop/naming/block_parameter_name_spec.rb index c2da63c39a3c..44beff8c80b7 100644 --- a/spec/rubocop/cop/naming/block_parameter_name_spec.rb +++ b/spec/rubocop/cop/naming/block_parameter_name_spec.rb @@ -44,6 +44,18 @@ RUBY end + it 'registers offense when param with prefix is less than minimum length' do + expect_offense(<<~RUBY) + something do |_a, __b, *c, **__d| + ^^ Block parameter must be at least 2 characters long. + ^^^ Block parameter must be at least 2 characters long. + ^^ Block parameter must be at least 2 characters long. + ^^^^^ Block parameter must be at least 2 characters long. + do_stuff + end + RUBY + end + it 'registers offense when param contains uppercase characters' do expect_offense(<<~RUBY) something { |number_One| do_stuff } diff --git a/spec/rubocop/cop/naming/method_parameter_name_spec.rb b/spec/rubocop/cop/naming/method_parameter_name_spec.rb index d675094a9fd9..647d610fde40 100644 --- a/spec/rubocop/cop/naming/method_parameter_name_spec.rb +++ b/spec/rubocop/cop/naming/method_parameter_name_spec.rb @@ -99,6 +99,18 @@ def something(ab) RUBY end + it 'registers offense when parameter with prefix is less than minimum length' do + expect_offense(<<~RUBY) + def something(_a, __b, *c, **__d) + ^^ Method parameter must be at least 3 characters long. + ^^^ Method parameter must be at least 3 characters long. + ^^ Method parameter must be at least 3 characters long. + ^^^^^ Method parameter must be at least 3 characters long. + do_stuff + end + RUBY + end + it 'registers offense when parameter contains uppercase characters' do expect_offense(<<~RUBY) def something(number_One)