From 5e2237a83ecd3b0277ec9e1ef96ee257af6681d5 Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Thu, 22 Aug 2024 16:55:52 +0200 Subject: [PATCH] Fix an error for `Rails/Validation` when passing no arguments --- changelog/fix_error_rails_validation.md | 1 + lib/rubocop/cop/rails/validation.rb | 2 +- spec/rubocop/cop/rails/validation_spec.rb | 6 ++++++ 3 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_error_rails_validation.md diff --git a/changelog/fix_error_rails_validation.md b/changelog/fix_error_rails_validation.md new file mode 100644 index 0000000000..024d4bf8b9 --- /dev/null +++ b/changelog/fix_error_rails_validation.md @@ -0,0 +1 @@ +* [#1337](https://github.com/rubocop/rubocop-rails/pull/1337): Fix an error for `Rails/Validation` when passing no arguments. ([@earlopain][]) diff --git a/lib/rubocop/cop/rails/validation.rb b/lib/rubocop/cop/rails/validation.rb index beaa8f2b6e..bc78b01625 100644 --- a/lib/rubocop/cop/rails/validation.rb +++ b/lib/rubocop/cop/rails/validation.rb @@ -59,11 +59,11 @@ class Validation < Base def on_send(node) return if node.receiver + return unless (last_argument = node.last_argument) range = node.loc.selector add_offense(range, message: message(node)) do |corrector| - last_argument = node.last_argument return if !last_argument.literal? && !last_argument.splat_type? && !frozen_array_argument?(last_argument) corrector.replace(range, 'validates') diff --git a/spec/rubocop/cop/rails/validation_spec.rb b/spec/rubocop/cop/rails/validation_spec.rb index 3aa2b891f9..c178b371d3 100644 --- a/spec/rubocop/cop/rails/validation_spec.rb +++ b/spec/rubocop/cop/rails/validation_spec.rb @@ -171,4 +171,10 @@ validates_numericality_of :a, b RUBY end + + it 'registers no offense when no arguments are passed' do + expect_no_offenses(<<~RUBY) + validates_numericality_of + RUBY + end end