KEMBAR78
Fix `Style/ConditionalAssignment` crash on multi-line regex in conditional by martinemde · Pull Request #14599 · rubocop/rubocop · GitHub
Skip to content

Conversation

@martinemde
Copy link
Contributor

@martinemde martinemde commented Oct 11, 2025

Multi-line regexes caused a crash when trying to apply conditional assignment assign_in_condition.

An error occurred while Style/ConditionalAssignment cop was inspecting path/to/file.rb

The culprit is multi-line regex. The original had an interpolation, but it still crashes without the interpolation. I chose to keep both tests.

Unfortunately, I had to bypass PerceivedComplexity, but other metrics were already skipped. Not sure if it's worth refactoring.


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

@martinemde martinemde force-pushed the martinemde/multi-line-regex-conditional-in branch 2 times, most recently from 1a2e504 to cebd9d6 Compare October 11, 2025 04:21
@martinemde martinemde changed the title Fix crash on multi-line regex in conditional Fix Style/ConditionalAssignment crash on multi-line regex in conditional Oct 11, 2025

white_space = white_space_range(child, column)
corrector.remove(white_space) if white_space.source.strip.empty?
corrector.remove(white_space) if white_space && white_space.source.strip.empty?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's just my two cents. Can you refactor to the following?

diff --git a/lib/rubocop/cop/style/conditional_assignment.rb b/lib/rubocop/cop/style/conditional_assignment.rb
index af9478704..f7433967e 100644
--- a/lib/rubocop/cop/style/conditional_assignment.rb
+++ b/lib/rubocop/cop/style/conditional_assignment.rb
@@ -442,9 +442,9 @@ module RuboCop
           branch.each_node do |child|
             next if child.source_range.nil?
             next if child.parent.dstr_type?
+            next unless (white_space = white_space_range(child, column))
 
-            white_space = white_space_range(child, column)
-            corrector.remove(white_space) if white_space && white_space.source.strip.empty?
+            corrector.remove(white_space) if white_space.source.strip.empty?
           end
 
           if condition.loc.else && !same_line?(condition.else_branch, condition)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I found an even better option, no complexity increase and the white_space_range code makes a bit more sense to me now. We only return non-nil when the range is actually whitespace, instead of checking at the caller location.

Fix `Style/ConditionalAssignment` multi-line regex crash

Multi-line regexes caused a crash on when trying to
check or fix conditional assignment with enforced style assign_inside_conditional.
@martinemde martinemde force-pushed the martinemde/multi-line-regex-conditional-in branch from cebd9d6 to bd03b85 Compare October 11, 2025 15:44
@martinemde martinemde requested a review from koic October 11, 2025 15:46
Comment on lines 446 to +447
white_space = white_space_range(child, column)
corrector.remove(white_space) if white_space.source.strip.empty?
corrector.remove(white_space) if white_space
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went back and forth on next unless (white_space = white_space_range(child, column) but I think this reads better.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The repeated appearance of white_space is a bit redundant, but it's not strictly required at this point. Thank you for resolving it!

@koic koic merged commit e8c58ff into rubocop:master Oct 11, 2025
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants