From 43597520d465d8bcab1340796ee08ec75918d0bc Mon Sep 17 00:00:00 2001 From: Thomas Thomassen Date: Tue, 2 Apr 2019 15:27:41 +0200 Subject: [PATCH] [Fix #6761] Make UncommunicativeMethodParamName account for underscores. --- CHANGELOG.md | 2 ++ lib/rubocop/cop/mixin/uncommunicative_name.rb | 7 ++++++- .../uncommunicative_method_param_name_spec.rb | 16 ++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b034b41660..3d0d0319b62 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### Bug fixes +* [#6761](https://github.com/rubocop-hq/rubocop/issues/6761): Make `Naming/UncommunicativeMethodParamName` account for param names prefixed with underscores. ([@thomthom][]) * [#6855](https://github.com/rubocop-hq/rubocop/pull/6855): Fix an exception in `Rails/RedundantReceiverInWithOptions` when the body is empty. ([@ericsullivan][]) * [#6856](https://github.com/rubocop-hq/rubocop/pull/6856): Fix auto-correction for `Style/BlockComments` when the file is missing a trailing blank line. ([@ericsullivan][]) * [#6858](https://github.com/rubocop-hq/rubocop/issues/6858): Fix an incorrect auto-correct for `Lint/ToJSON` when there are no `to_json` arguments. ([@koic][]) @@ -3895,3 +3896,4 @@ [@ericsullivan]: https://github.com/ericsullivan [@aeroastro]: https://github.com/aeroastro [@anuja-joshi]: https://github.com/anuja-joshi +[@thomthom]: https://github.com/thomthom diff --git a/lib/rubocop/cop/mixin/uncommunicative_name.rb b/lib/rubocop/cop/mixin/uncommunicative_name.rb index 927b9d6973e..e26444da026 100644 --- a/lib/rubocop/cop/mixin/uncommunicative_name.rb +++ b/lib/rubocop/cop/mixin/uncommunicative_name.rb @@ -13,7 +13,12 @@ module UncommunicativeName def check(node, args) args.each do |arg| - name = arg.children.first.to_s + # Argument names might be "_" or prefixed with "_" to indicate they + # are unused. Trim away this prefix and only analyse the basename. + full_name = arg.children.first.to_s + next if full_name == '_' + + name = full_name.gsub(/\A([_]+)/, '') next if (arg.restarg_type? || arg.kwrestarg_type?) && name.empty? next if allowed_names.include?(name) diff --git a/spec/rubocop/cop/naming/uncommunicative_method_param_name_spec.rb b/spec/rubocop/cop/naming/uncommunicative_method_param_name_spec.rb index ab7be78cdcb..da0b0754c60 100644 --- a/spec/rubocop/cop/naming/uncommunicative_method_param_name_spec.rb +++ b/spec/rubocop/cop/naming/uncommunicative_method_param_name_spec.rb @@ -147,6 +147,22 @@ def quux(foo1, foo2) RUBY end + it 'accepts param names prefixed with underscore' do + expect_no_offenses(<<-RUBY.strip_indent) + def quux(_foo1, _foo2) + do_stuff + end + RUBY + end + + it 'accepts underscore param names' do + expect_no_offenses(<<-RUBY.strip_indent) + def quux(_) + do_stuff + end + RUBY + end + it 'registers unlisted offensive names' do expect_offense(<<-RUBY.strip_indent) def quux(bar, bar1)