From 94c17b839821116249f0941844531755e8a501d7 Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Sun, 10 Mar 2024 17:37:33 +0100 Subject: [PATCH] Fix an error for `Style/ClassVars` when calling `class_variable_set` without arguments --- changelog/fix_an_error_for_style_class_vars.md | 1 + lib/rubocop/cop/style/class_vars.rb | 6 +++--- spec/rubocop/cop/style/class_vars_spec.rb | 4 ++++ 3 files changed, 8 insertions(+), 3 deletions(-) create mode 100644 changelog/fix_an_error_for_style_class_vars.md diff --git a/changelog/fix_an_error_for_style_class_vars.md b/changelog/fix_an_error_for_style_class_vars.md new file mode 100644 index 00000000000..e0652cba4ec --- /dev/null +++ b/changelog/fix_an_error_for_style_class_vars.md @@ -0,0 +1 @@ +* [#12772](https://github.com/rubocop/rubocop/pull/12772): Fix an error for `Style/ClassVars` when calling `class_variable_set` without arguments. ([@earlopain][]) diff --git a/lib/rubocop/cop/style/class_vars.rb b/lib/rubocop/cop/style/class_vars.rb index 9896ed0df53..39f9e789a85 100644 --- a/lib/rubocop/cop/style/class_vars.rb +++ b/lib/rubocop/cop/style/class_vars.rb @@ -54,9 +54,9 @@ def on_cvasgn(node) end def on_send(node) - add_offense( - node.first_argument, message: format(MSG, class_var: node.first_argument.source) - ) + return unless (first_argument = node.first_argument) + + add_offense(first_argument, message: format(MSG, class_var: first_argument.source)) end end end diff --git a/spec/rubocop/cop/style/class_vars_spec.rb b/spec/rubocop/cop/style/class_vars_spec.rb index ff0167d28bb..b597c75f060 100644 --- a/spec/rubocop/cop/style/class_vars_spec.rb +++ b/spec/rubocop/cop/style/class_vars_spec.rb @@ -28,4 +28,8 @@ class TestClass; end it 'does not register an offense for class variable usage' do expect_no_offenses('@@test.test(20)') end + + it 'registers no offense for class variable set without arguments' do + expect_no_offenses('class_variable_set') + end end