Skip to content

Style/SoleNestedConditional incorrectly autofixes condition with assignment #14309

@knarewski

Description

@knarewski

Autofix for Style/SoleNestedConditional breaks my code, despite being flagged as safe

export RUBOCOP_TARGET_RUBY_VERSION=3.3
echo "
if 'truthy' && assigned = '1'
  if '2'
    puts assigned
  end
end
" | rubocop --stdin user.rb --only 'Style/SoleNestedConditional' -a

Expected behavior

Correction shouldn't change original code's behaviour (prints 1), ie:

if 'truthy' && (assigned = '1') && '2'
  puts assigned
end

Actual behavior

Correction changes original code's behaviour (prints 2):

if 'truthy' && assigned = '1' && '2'
  puts assigned
end

Steps to reproduce the problem

As above

RuboCop version

1.76.2 (using Parser 3.3.8.0, Prism 1.4.0, default, rubocop-ast 1.45.1, analyzing as Ruby 3.3, running on ruby 3.3.6) [x86_64-linux]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions