Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Style/IfWithSemicolon - undefined method `source' for nil:NilClass #11996

Closed
snutij opened this issue Jun 29, 2023 · 0 comments 路 Fixed by #11997
Closed

Style/IfWithSemicolon - undefined method `source' for nil:NilClass #11996

snutij opened this issue Jun 29, 2023 · 0 comments 路 Fixed by #11997
Labels

Comments

@snutij
Copy link

snutij commented Jun 29, 2023

Hello 馃憢 I hope I'll give enough information, do not hesitate if you need more context.

Given a file foo.rb:

if 1; elsif 2; end

Which is a valid syntax:

ruby -c foo.rb
Syntax OK

Expected behavior

Should not trigger an error, display the offense and propose a fix (reading the code I think this is the expected behavior, not sure to be honest, at least not trigger an error as it's a valid syntax).

Actual behavior

bin/rubocop -d --only Style/IfWithSemicolon foo.rb

An error occurred while Style/IfWithSemicolon cop was inspecting foo.rb:1:0.
undefined method `source' for nil:NilClass
/Users/snutij/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.53.1/lib/rubocop/cop/style/if_with_semicolon.rb:51:in `correct_elsif'
/Users/snutij/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.53.1/lib/rubocop/cop/style/if_with_semicolon.rb:39:in `autocorrect'
/Users/snutij/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.53.1/lib/rubocop/cop/style/if_with_semicolon.rb:32:in `block in on_normal_if_unless'
/Users/snutij/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.53.1/lib/rubocop/cop/base.rb:377:in `correct'
/Users/snutij/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.53.1/lib/rubocop/cop/base.rb:181:in `add_offense'
/Users/snutij/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.53.1/lib/rubocop/cop/style/if_with_semicolon.rb:31:in `on_normal_if_unless'
/Users/snutij/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.53.1/lib/rubocop/cop/mixin/on_normal_if_unless.rb:10:in `on_if'
/Users/snutij/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-1.53.1/lib/rubocop/cop/commissioner.rb:107:in `public_send'

Steps to reproduce the problem

In IfWithSemicolon the on_normal_if_unless receive a node:

(ruby) node
s(:if,
  s(:int, 1), nil,
  s(:if,
    s(:int, 2), nil, nil))

Then to propose a fix the cop calls the correct_elsif, as if node.else_branch&.if_type? is true

(ruby) node.else_branch
s(:if,
  s(:int, 2), nil, nil)
(ruby)  node.else_branch&.if_type?
true

But it's interpolate#{node.if_branch.source} which is nil

(ruby) node.condition.source
"1"
(ruby) node.if_branch
nil

def on_normal_if_unless(node)
return if node.parent&.if_type?
beginning = node.loc.begin
return unless beginning&.is?(';')
message = node.else_branch&.if_type? ? MSG_IF_ELSE : MSG_TERNARY
add_offense(node, message: format(message, expr: node.condition.source)) do |corrector|
corrector.replace(node, autocorrect(node))
end
end

def autocorrect(node)
return correct_elsif(node) if node.else_branch&.if_type?
then_code = node.if_branch ? node.if_branch.source : 'nil'
else_code = node.else_branch ? node.else_branch.source : 'nil'
"#{node.condition.source} ? #{then_code} : #{else_code}"
end

def correct_elsif(node)
<<~RUBY.chop
if #{node.condition.source}
#{node.if_branch.source}
#{build_else_branch(node.else_branch).chop}
end
RUBY
end

RuboCop version

bundle exec rubocop -V
1.53.1 (using Parser 3.2.2.3, rubocop-ast 1.29.0, running on ruby 3.2.2) [arm64-darwin22]
  - rubocop-minitest 0.31.0
  - rubocop-rake 0.6.0
  - rubocop-sorbet 0.7.0
@koic koic added the bug label Jun 29, 2023
koic added a commit to koic/rubocop that referenced this issue Jun 29, 2023
Fixes rubocop#11996.

This PR fixes an error for `Style/IfWithSemicolon`
when without branch bodies.
@snutij snutij changed the title Style/IfWithSemicolon - undefined method `source' for nil:NilClass Style/IfWithSemicolon - undefined method `source' for nil:NilClass Jun 29, 2023
bbatsov pushed a commit that referenced this issue Jun 29, 2023
Fixes #11996.

This PR fixes an error for `Style/IfWithSemicolon`
when without branch bodies.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants