-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Fix some cops errors when condition is empty brace #3625
Conversation
@@ -12,6 +12,7 @@ | |||
* [#3607](https://github.com/bbatsov/rubocop/pull/3607): Fix `Style/RedundantReturn` cop for empty if body. ([@pocke][]) | |||
* [#3291](https://github.com/bbatsov/rubocop/issues/3291): Improve detection of `raw` and `html_safe` methods in `Rails/OutputSafety`. ([@lumeet][]) | |||
* Redundant return style now properly handles empty `when` blocks. ([@albus522][]) | |||
* [#xxxx](https://github.com/bbatsov/rubocop/pull/xxxx): Fix some cops errors when condition is empty brace. ([@pocke][]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
xxxx? :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I'll fix it 👍
@@ -9,10 +9,13 @@ module NegativeConditional | |||
include IfNode | |||
|
|||
def_node_matcher :single_negative?, '(send !(send _ :!) :!)' | |||
def_node_matcher :empty_brace?, '(begin)' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should name this empty_condition?
or empty_begin_block?
. It's definitely not exactly an empty_brace
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might also be overkill to use a node matcher for this. What do you think about something like (not tested):
def empty_condition?(node)
node.begin_type? && node.children.emtpy?
end
One of the downsides of def_node_matcher
is that it will circumvent auto-complete in your editor, which makes these methods pretty undiscoverable. 😞
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 I think empty_condition?
is better than others.
Btw, I hope such code ( |
Surprisingly, executing this in verbose mode produces no warnings. 😶 |
The problem is like |
👍 |
e219b68
to
48f3c71
Compare
Problem ---- RuboCop crashes when `if` condition is an empty brace. For example ```ruby if () a end ``` ```sh $ rubocop -d --cache false test.rb An error occurred while Lint/AssignmentInCondition cop was inspecting /home/pocke/ghq/github.com/bbatsov/rubocop/test.rb. An error occurred while Style/NegatedIf cop was inspecting /home/pocke/ghq/github.com/bbatsov/rubocop/test.rb. An error occurred while Style/ParenthesesAroundCondition cop was inspecting /home/pocke/ghq/github.com/bbatsov/rubocop/test.rb. For /home/pocke/ghq/github.com/bbatsov/rubocop: configuration from /home/pocke/ghq/github.com/bbatsov/rubocop/.rubocop.yml Inheriting configuration from /home/pocke/ghq/github.com/bbatsov/rubocop/.rubocop_todo.yml Default configuration from /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/config/default.yml Inheriting configuration from /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/config/enabled.yml Inheriting configuration from /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/config/disabled.yml Inspecting 1 file Scanning /home/pocke/ghq/github.com/bbatsov/rubocop/test.rb undefined method `equals_asgn?' for nil:NilClass /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/mixin/safe_assignment.rb:11:in `safe_assignment?' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/lint/assignment_in_condition.rb:51:in `skip_children?' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/lint/assignment_in_condition.rb:34:in `block in check' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/lint/assignment_in_condition.rb:56:in `traverse_node' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/lint/assignment_in_condition.rb:33:in `check' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/lint/assignment_in_condition.rb:15:in `on_if' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:41:in `block (2 levels) in on_if' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:96:in `with_cop_error_handling' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:40:in `block in on_if' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:39:in `each' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:39:in `on_if' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/ast_node/traversal.rb:12:in `walk' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:58:in `investigate' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/team.rb:120:in `investigate' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/team.rb:108:in `offenses' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/team.rb:51:in `inspect_file' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:243:in `inspect_file' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:190:in `block in do_inspection_loop' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:222:in `block in iterate_until_no_changes' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:215:in `loop' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:215:in `iterate_until_no_changes' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:186:in `do_inspection_loop' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:101:in `block in file_offenses' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:111:in `file_offense_cache' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:99:in `file_offenses' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:90:in `process_file' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:68:in `block in each_inspected_file' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:65:in `each' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:65:in `reduce' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:65:in `each_inspected_file' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:57:in `inspect_files' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:36:in `run' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cli.rb:71:in `execute_runner' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cli.rb:27:in `run' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/bin/rubocop:13:in `block in <top (required)>' /usr/lib/ruby/2.3.0/benchmark.rb:308:in `realtime' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/bin/rubocop:12:in `<top (required)>' /home/pocke/.gem/ruby/2.3.0/bin//rubocop:23:in `load' /home/pocke/.gem/ruby/2.3.0/bin//rubocop:23:in `<main>' undefined method `begin_type?' for nil:NilClass /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/mixin/negative_conditional.rb:18:in `check_negative_conditional' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/style/negated_if.rb:17:in `on_if' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:41:in `block (2 levels) in on_if' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:96:in `with_cop_error_handling' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:40:in `block in on_if' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:39:in `each' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:39:in `on_if' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/ast_node/traversal.rb:12:in `walk' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:58:in `investigate' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/team.rb:120:in `investigate' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/team.rb:108:in `offenses' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/team.rb:51:in `inspect_file' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:243:in `inspect_file' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:190:in `block in do_inspection_loop' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:222:in `block in iterate_until_no_changes' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:215:in `loop' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:215:in `iterate_until_no_changes' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:186:in `do_inspection_loop' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:101:in `block in file_offenses' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:111:in `file_offense_cache' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:99:in `file_offenses' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:90:in `process_file' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:68:in `block in each_inspected_file' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:65:in `each' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:65:in `reduce' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:65:in `each_inspected_file' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:57:in `inspect_files' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:36:in `run' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cli.rb:71:in `execute_runner' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cli.rb:27:in `run' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/bin/rubocop:13:in `block in <top (required)>' /usr/lib/ruby/2.3.0/benchmark.rb:308:in `realtime' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/bin/rubocop:12:in `<top (required)>' /home/pocke/.gem/ruby/2.3.0/bin//rubocop:23:in `load' /home/pocke/.gem/ruby/2.3.0/bin//rubocop:23:in `<main>' undefined method `loc' for nil:NilClass /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/mixin/if_node.rb:10:in `ternary?' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/style/parentheses_around_condition.rb:43:in `modifier_op?' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/style/parentheses_around_condition.rb:33:in `process_control_op' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/style/parentheses_around_condition.rb:15:in `on_if' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:41:in `block (2 levels) in on_if' 3 errors occurred: An error occurred while Lint/AssignmentInCondition cop was inspecting /home/pocke/ghq/github.com/bbatsov/rubocop/test.rb. An error occurred while Style/NegatedIf cop was inspecting /home/pocke/ghq/github.com/bbatsov/rubocop/test.rb. An error occurred while Style/ParenthesesAroundCondition cop was inspecting /home/pocke/ghq/github.com/bbatsov/rubocop/test.rb. Errors are usually caused by RuboCop bugs. Please, report your problems to RuboCop's issue tracker. Mention the following information in the issue report: 0.44.1 (using Parser 2.3.1.3, running on ruby 2.3.1 x86_64-linux) /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:96:in `with_cop_error_handling' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:40:in `block in on_if' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:39:in `each' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:39:in `on_if' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/ast_node/traversal.rb:12:in `walk' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/commissioner.rb:58:in `investigate' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/team.rb:120:in `investigate' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/team.rb:108:in `offenses' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cop/team.rb:51:in `inspect_file' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:243:in `inspect_file' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:190:in `block in do_inspection_loop' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:222:in `block in iterate_until_no_changes' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:215:in `loop' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:215:in `iterate_until_no_changes' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:186:in `do_inspection_loop' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:101:in `block in file_offenses' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:111:in `file_offense_cache' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:99:in `file_offenses' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:90:in `process_file' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:68:in `block in each_inspected_file' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:65:in `each' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:65:in `reduce' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:65:in `each_inspected_file' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:57:in `inspect_files' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/runner.rb:36:in `run' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cli.rb:71:in `execute_runner' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/lib/rubocop/cli.rb:27:in `run' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/bin/rubocop:13:in `block in <top (required)>' /usr/lib/ruby/2.3.0/benchmark.rb:308:in `realtime' /home/pocke/.gem/ruby/2.3.0/gems/rubocop-0.44.1/bin/rubocop:12:in `<top (required)>' /home/pocke/.gem/ruby/2.3.0/bin//rubocop:23:in `load' /home/pocke/.gem/ruby/2.3.0/bin//rubocop:23:in `<main>' C Offenses: test.rb:1:1: C: Style/FrozenStringLiteralComment: Missing frozen string literal comment. if () ^ test.rb:1:1: C: Style/IfUnlessModifier: Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||. if () ^^ 1 file inspected, 2 offenses detected Finished in 0.0758015800092835 seconds ```
48f3c71
to
06e4546
Compare
Fixed |
Problem
RuboCop crashes when
if
condition is an empty brace.For example
Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).and description in grammatically correct, complete sentences.