From 335ddd90db70458ecda523395dfef0132aa3b46c Mon Sep 17 00:00:00 2001 From: sakuraya Date: Wed, 10 Apr 2019 03:25:27 +0900 Subject: [PATCH] [Fix #6914] Fix an error for `Rails/RedundantAllowNil` when with interpolations --- CHANGELOG.md | 5 +++++ lib/rubocop/cop/rails/redundant_allow_nil.rb | 6 +++--- spec/rubocop/cop/rails/redundant_allow_nil_spec.rb | 8 ++++++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5159ed5a16c..552299ee801 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## master (unreleased) +### Bug fixes + +* [#6914](https://github.com/rubocop-hq/rubocop/issues/6914): [Fix #6914] Fix an error for `Rails/RedundantAllowNil` when with interpolations. ([@Blue-Pix][]) + ### Changes * [#5977](https://github.com/rubocop-hq/rubocop/issues/5977): Remove Performance cops. ([@koic][]) @@ -3929,3 +3933,4 @@ [@anuja-joshi]: https://github.com/anuja-joshi [@XrXr]: https://github.com/XrXr [@thomthom]: https://github.com/thomthom +[@Blue-Pix]: https://github.com/Blue-Pix diff --git a/lib/rubocop/cop/rails/redundant_allow_nil.rb b/lib/rubocop/cop/rails/redundant_allow_nil.rb index be75c159246..2f5934b5649 100644 --- a/lib/rubocop/cop/rails/redundant_allow_nil.rb +++ b/lib/rubocop/cop/rails/redundant_allow_nil.rb @@ -79,10 +79,10 @@ def find_allow_nil_and_allow_blank(node) node.each_descendant do |descendant| next unless descendant.pair_type? - key = descendant.children.first.value + key = descendant.children.first.source - allow_nil = descendant if key == :allow_nil - allow_blank = descendant if key == :allow_blank + allow_nil = descendant if key == 'allow_nil' + allow_blank = descendant if key == 'allow_blank' break if allow_nil && allow_blank end diff --git a/spec/rubocop/cop/rails/redundant_allow_nil_spec.rb b/spec/rubocop/cop/rails/redundant_allow_nil_spec.rb index 0ce9ffd5a9e..760661d4a0b 100644 --- a/spec/rubocop/cop/rails/redundant_allow_nil_spec.rb +++ b/spec/rubocop/cop/rails/redundant_allow_nil_spec.rb @@ -73,4 +73,12 @@ RUBY end end + + context 'when using string interpolation' do + it 'registers no offense' do + expect_no_offenses(<<-'RUBY'.strip_indent) + validates :details, "path_to_dynamic_validation/#{with_interpolation}": true + RUBY + end + end end