diff --git a/changelog/fix_rails_file_path_cop_error_with_rescued_rails_root.md b/changelog/fix_rails_file_path_cop_error_with_rescued_rails_root.md new file mode 100644 index 0000000000..e5fe0b299e --- /dev/null +++ b/changelog/fix_rails_file_path_cop_error_with_rescued_rails_root.md @@ -0,0 +1 @@ +* [#1392](https://github.com/rubocop/rubocop-rails/pull/1392): Fix `Rails/FilePath` cop error with rescued `Rails.root`. ([@viralpraxis][]) diff --git a/lib/rubocop/cop/rails/file_path.rb b/lib/rubocop/cop/rails/file_path.rb index 02459031a9..95aece36be 100644 --- a/lib/rubocop/cop/rails/file_path.rb +++ b/lib/rubocop/cop/rails/file_path.rb @@ -76,6 +76,7 @@ def check_for_slash_after_rails_root_in_dstr(node) rails_root_index = find_rails_root_index(node) slash_node = node.children[rails_root_index + 1] return unless slash_node&.str_type? && slash_node.source.start_with?(File::SEPARATOR) + return if node.children[rails_root_index].each_descendant(:rescue).any? register_offense(node, require_to_s: false) do |corrector| autocorrect_slash_after_rails_root_in_dstr(corrector, node, rails_root_index) diff --git a/spec/rubocop/cop/rails/file_path_spec.rb b/spec/rubocop/cop/rails/file_path_spec.rb index 3bec9ded2c..2a21a5d0e9 100644 --- a/spec/rubocop/cop/rails/file_path_spec.rb +++ b/spec/rubocop/cop/rails/file_path_spec.rb @@ -406,5 +406,13 @@ RUBY end end + + context 'when rescued concat Rails.root' do + it 'does not register an offense' do + expect_no_offenses(<<~'RUBY') + "#{Rails.root rescue '.'}/config" + RUBY + end + end end end