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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rubocop crashes on ternary with empty else when in auto-correct mode #3485

Closed
rpherbig opened this issue Sep 10, 2016 · 1 comment
Closed

Comments

@rpherbig
Copy link

rpherbig commented Sep 10, 2016

Be clear, concise and precise in your description of the problem.
Open an issue with a descriptive title and a summary in grammatically correct,
complete sentences.

Use the template below when reporting bugs. Please, make sure that
you're running the latest stable RuboCop and that the problem you're reporting
hasn't been reported (and potentially fixed) already.

Before filing the ticket you should replace all text above the horizontal
rule with your own words.


Expected behavior

Running rubocop in auto-correct mode should behave the same as normal mode, but also fix as many issues as possible. This is the output in normal mode:

$ rubocop temp.rb
Inspecting 1 file
W

Offenses:

temp.rb:2:1: C: Use %w or %W for an array of words.
['a', 'b', 'c'].each_with_index do |name, index|
^^^^^^^^^^^^^^^
temp.rb:2:37: W: Unused block argument - name. If it's necessary, use _ or _name as an argument name to indicate that it won't be used.
['a', 'b', 'c'].each_with_index do |name, index|
                                    ^^^^
temp.rb:3:3: C: Favor the ternary operator (?:) over if/then/else/end constructs.
  if index > 2 then next else end
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
temp.rb:3:26: C: Redundant else-clause.
  if index > 2 then next else end
                         ^^^^
temp.rb:5:14: C: Final newline missing.
puts 'finish'


1 file inspected, 5 offenses detected

Actual behavior

However, rubocop crashes on the same script in auto-correct mode:

$ rubocop --auto-correct temp.rb
Inspecting 1 file


0 files inspected, no offenses detected
undefined method `type' for nil:NilClass
c:/Ruby200/lib/ruby/gems/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/style/one_line_conditional.rb:52:in `requires_parentheses?'
c:/Ruby200/lib/ruby/gems/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/style/one_line_conditional.rb:48:in `expr_replacement'
c:/Ruby200/lib/ruby/gems/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/style/one_line_conditional.rb:44:in `ternary'
c:/Ruby200/lib/ruby/gems/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/style/one_line_conditional.rb:38:in `replacement'
c:/Ruby200/lib/ruby/gems/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/style/one_line_conditional.rb:26:in `block in autocorrect'
c:/Ruby200/lib/ruby/gems/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/corrector.rb:57:in `call'
c:/Ruby200/lib/ruby/gems/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/corrector.rb:57:in `block (2 levels) in rewrite'
c:/Ruby200/lib/ruby/gems/2.0.0/gems/parser-2.3.1.2/lib/parser/source/rewriter.rb:194:in `transaction'
c:/Ruby200/lib/ruby/gems/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/corrector.rb:56:in `block in rewrite'
c:/Ruby200/lib/ruby/gems/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/corrector.rb:54:in `each'
c:/Ruby200/lib/ruby/gems/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/corrector.rb:54:in `rewrite'
c:/Ruby200/lib/ruby/gems/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/team.rb:145:in `autocorrect_all_cops'
c:/Ruby200/lib/ruby/gems/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/team.rb:77:in `autocorrect'
c:/Ruby200/lib/ruby/gems/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/team.rb:105:in `block in offenses'
c:/Ruby200/lib/ruby/gems/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/team.rb:122:in `investigate'
c:/Ruby200/lib/ruby/gems/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/team.rb:101:in `offenses'
c:/Ruby200/lib/ruby/gems/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/team.rb:52:in `inspect_file'
c:/Ruby200/lib/ruby/gems/2.0.0/gems/rubocop-0.42.0/lib/rubocop/runner.rb:223:in `inspect_file'
c:/Ruby200/lib/ruby/gems/2.0.0/gems/rubocop-0.42.0/lib/rubocop/runner.rb:193:in `block in do_inspection_loop'
c:/Ruby200/lib/ruby/gems/2.0.0/gems/rubocop-0.42.0/lib/rubocop/runner.rb:183:in `loop'
c:/Ruby200/lib/ruby/gems/2.0.0/gems/rubocop-0.42.0/lib/rubocop/runner.rb:183:in `do_inspection_loop'
c:/Ruby200/lib/ruby/gems/2.0.0/gems/rubocop-0.42.0/lib/rubocop/runner.rb:93:in `block in file_offenses'
c:/Ruby200/lib/ruby/gems/2.0.0/gems/rubocop-0.42.0/lib/rubocop/runner.rb:103:in `file_offense_cache'
c:/Ruby200/lib/ruby/gems/2.0.0/gems/rubocop-0.42.0/lib/rubocop/runner.rb:91:in `file_offenses'
c:/Ruby200/lib/ruby/gems/2.0.0/gems/rubocop-0.42.0/lib/rubocop/runner.rb:82:in `process_file'
c:/Ruby200/lib/ruby/gems/2.0.0/gems/rubocop-0.42.0/lib/rubocop/runner.rb:59:in `block in inspect_files'
c:/Ruby200/lib/ruby/gems/2.0.0/gems/rubocop-0.42.0/lib/rubocop/runner.rb:57:in `each'
c:/Ruby200/lib/ruby/gems/2.0.0/gems/rubocop-0.42.0/lib/rubocop/runner.rb:57:in `inspect_files'
c:/Ruby200/lib/ruby/gems/2.0.0/gems/rubocop-0.42.0/lib/rubocop/runner.rb:35:in `run'
c:/Ruby200/lib/ruby/gems/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cli.rb:72:in `execute_runner'
c:/Ruby200/lib/ruby/gems/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cli.rb:28:in `run'
c:/Ruby200/lib/ruby/gems/2.0.0/gems/rubocop-0.42.0/bin/rubocop:14:in `block in <top (required)>'
c:/Ruby200/lib/ruby/2.0.0/benchmark.rb:296:in `realtime'
c:/Ruby200/lib/ruby/gems/2.0.0/gems/rubocop-0.42.0/bin/rubocop:13:in `<top (required)>'
c:/Ruby200/bin/rubocop:23:in `load'
c:/Ruby200/bin/rubocop:23:in `<main>'

Steps to reproduce the problem

I made a simple ruby file that demonstrates the problem. Run rubocop on it twice to see the problem (once normally, once with auto-correct):

puts 'start'
['a', 'b', 'c'].each_with_index do |name, index|
  if index > 2 then next else end
end
puts 'finish'

Running it shows it is a valid Ruby script:

$ ruby temp.rb
start
finish

RuboCop version

Include the output of rubocop -V:

$ rubocop -V
0.42.0 (using Parser 2.3.1.2, running on ruby 2.0.0 i386-mingw32)
@Drenmi
Copy link
Collaborator

Drenmi commented Sep 10, 2016

Hm. EmptyElse should definitely take precedence here, but we could probably make OneLineConditional a bit smarter so it doesn't register an offense for incomplete if, else clauses.

tejasbubane added a commit to tejasbubane/rubocop that referenced this issue Sep 18, 2016
bbatsov pushed a commit that referenced this issue Sep 19, 2016
mikezter added a commit to mikezter/rubocop that referenced this issue Sep 28, 2016
* bbatsov/master: (80 commits)
  [Fix rubocop#3540] Make `Style/GuardClause` register offense for instance & singleton methods
  [Fix rubocop#3436] issue related to Rails/SaveBang when returning non-bang call from the parent method
  Allow `#to_yml` to produce single-quoted strings
  Add support for StyleGuideBaseURL and update rules
  Add spec for the existing style guide URL implementation
  Fix the changelog
  Edited regular expression for normal case to fix issues rubocop#3514 and rubocop#3516 (rubocop#3524)
  Add a rake task for generation a new cop (rubocop#3533)
  [Fix rubocop#3510] Various bug fixes for SafeNavigation (rubocop#3517)
  [Fix rubocop#3512] Change error message of `Lint/UnneededSplatExpansion` for array (rubocop#3526)
  Fix false positive in `Lint/AssignmentInCondition` (rubocop#3520) (rubocop#3529)
  Rename a mismatched filename (rubocop#3523)
  Fix a broken changelog entry
  [Fix rubocop#3511] Style/TernaryParentheses false positive (rubocop#3513)
  Fix the release notes for 0.43
  Cut 0.43.0
  [Fix rubocop#3462] Don't flag single-line methods
  Fix false negatives in `Rails/NotNullColumn` cop (rubocop#3508)
  Remove unused doubled methods (rubocop#3509)
  [Fix rubocop#3485] Make OneLineConditional cop ignore empty else (rubocop#3487)
  ...
Neodelf pushed a commit to Neodelf/rubocop that referenced this issue Oct 15, 2016
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

No branches or pull requests

2 participants