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

Style/IdenticalConditionalBranches cop error #12300

Closed
Brenntron opened this issue Oct 20, 2023 · 4 comments · Fixed by #12304
Closed

Style/IdenticalConditionalBranches cop error #12300

Brenntron opened this issue Oct 20, 2023 · 4 comments · Fixed by #12304
Labels

Comments

@Brenntron
Copy link

Style/IdenticalConditionalBranches cop encounters undefined method 'name' while running over a file.


Expected behavior

The IndenticalConditionalBranches cop should lint the conditional without erroring.

Actual behavior

rubocop -d my_file outputs 1 error after detecting other offenses correctly.

For my_directory: configuration from /Users/me/.config/rubocop/config.yml
configuration from /Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-rails-2.21.2/config/default.yml
configuration from /Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-rails-2.21.2/config/default.yml
Default configuration from /Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/config/default.yml
configuration from /Users/me/.config/rubocop/.rubocop-https---relaxed-ruby-style-rubocop-yml
Use parallel by default.
Skipping parallel inspection: only a single file needs inspection
Inspecting 1 file
Scanning my_file
An error occurred while Style/IdenticalConditionalBranches cop was inspecting my_file
328:18.
undefined method `name' for s(:send,
  s(:lvar, :object_packet), :[]=,
  s(:sym, :current_items),
  s(:hash)):RuboCop::AST::SendNode
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/cop/style/identical_conditional_branches.rb:156:in `check_branches'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/cop/style/identical_conditional_branches.rb:120:in `on_if'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/cop/commissioner.rb:107:in `public_send'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/cop/commissioner.rb:107:in `block (2 levels) in trigger_responding_cops'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/cop/commissioner.rb:171:in `with_cop_error_handling'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/cop/commissioner.rb:106:in `block in trigger_responding_cops'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/cop/commissioner.rb:105:in `each'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/cop/commissioner.rb:105:in `trigger_responding_cops'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/cop/commissioner.rb:69:in `on_if'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-ast-1.29.0/lib/rubocop/ast/traversal.rb:155:in `on_if'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/cop/commissioner.rb:71:in `on_if'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-ast-1.29.0/lib/rubocop/ast/traversal.rb:137:in `block in on_dstr'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-ast-1.29.0/lib/rubocop/ast/traversal.rb:137:in `each'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-ast-1.29.0/lib/rubocop/ast/traversal.rb:137:in `on_dstr'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/cop/commissioner.rb:71:in `on_begin'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-ast-1.29.0/lib/rubocop/ast/traversal.rb:158:in `on_block'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/cop/commissioner.rb:71:in `on_block'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-ast-1.29.0/lib/rubocop/ast/traversal.rb:158:in `on_block'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/cop/commissioner.rb:71:in `on_block'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-ast-1.29.0/lib/rubocop/ast/traversal.rb:137:in `block in on_dstr'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-ast-1.29.0/lib/rubocop/ast/traversal.rb:137:in `each'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-ast-1.29.0/lib/rubocop/ast/traversal.rb:137:in `on_dstr'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/cop/commissioner.rb:71:in `on_begin'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-ast-1.29.0/lib/rubocop/ast/traversal.rb:158:in `on_block'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/cop/commissioner.rb:71:in `on_block'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-ast-1.29.0/lib/rubocop/ast/traversal.rb:137:in `block in on_dstr'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-ast-1.29.0/lib/rubocop/ast/traversal.rb:137:in `each'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-ast-1.29.0/lib/rubocop/ast/traversal.rb:137:in `on_dstr'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/cop/commissioner.rb:71:in `on_begin'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-ast-1.29.0/lib/rubocop/ast/traversal.rb:154:in `on_class'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/cop/commissioner.rb:71:in `on_class'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-ast-1.29.0/lib/rubocop/ast/traversal.rb:138:in `on_while'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/cop/commissioner.rb:71:in `on_module'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-ast-1.29.0/lib/rubocop/ast/traversal.rb:138:in `on_while'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/cop/commissioner.rb:71:in `on_module'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-ast-1.29.0/lib/rubocop/ast/traversal.rb:138:in `on_while'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/cop/commissioner.rb:71:in `on_module'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-ast-1.29.0/lib/rubocop/ast/traversal.rb:138:in `on_while'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/cop/commissioner.rb:71:in `on_module'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-ast-1.29.0/lib/rubocop/ast/traversal.rb:20:in `walk'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/cop/commissioner.rb:87:in `investigate'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/cop/team.rb:156:in `investigate_partial'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/cop/team.rb:98:in `investigate'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/runner.rb:345:in `block in inspect_file'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/runner.rb:344:in `each'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/runner.rb:344:in `flat_map'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/runner.rb:344:in `inspect_file'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/runner.rb:287:in `block in do_inspection_loop'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/runner.rb:321:in `block in iterate_until_no_changes'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/runner.rb:314:in `loop'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/runner.rb:314:in `iterate_until_no_changes'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/runner.rb:283:in `do_inspection_loop'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/runner.rb:164:in `block in file_offenses'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/runner.rb:189:in `file_offense_cache'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/runner.rb:163:in `file_offenses'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/runner.rb:154:in `process_file'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/runner.rb:135:in `block in each_inspected_file'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/runner.rb:134:in `each'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/runner.rb:134:in `reduce'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/runner.rb:134:in `each_inspected_file'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/runner.rb:120:in `inspect_files'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/runner.rb:73:in `run'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/cli/command/execute_runner.rb:26:in `block in execute_runner'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/cli/command/execute_runner.rb:52:in `with_redirect'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/cli/command/execute_runner.rb:25:in `execute_runner'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/cli/command/execute_runner.rb:17:in `run'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/cli/command.rb:11:in `run'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/cli/environment.rb:18:in `run'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/cli.rb:118:in `run_command'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/cli.rb:125:in `execute_runners'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/cli.rb:51:in `block in run'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/cli.rb:77:in `profile_if_needed'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/lib/rubocop/cli.rb:43:in `run'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/exe/rubocop:19:in `block in <top (required)>'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/3.0.0/benchmark.rb:308:in `realtime'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.57.1/exe/rubocop:19:in `<top (required)>'
/Users/me/.asdf/installs/ruby/3.0.4/bin/rubocop:25:in `load'
/Users/me/.asdf/installs/ruby/3.0.4/bin/rubocop:25:in `<top (required)>'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/bundler-2.4.16/lib/bundler/cli/exec.rb:58:in `load'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/bundler-2.4.16/lib/bundler/cli/exec.rb:58:in `kernel_load'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/bundler-2.4.16/lib/bundler/cli/exec.rb:23:in `run'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/bundler-2.4.16/lib/bundler/cli.rb:492:in `exec'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/bundler-2.4.16/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/bundler-2.4.16/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/bundler-2.4.16/lib/bundler/vendor/thor/lib/thor.rb:392:in `dispatch'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/bundler-2.4.16/lib/bundler/cli.rb:34:in `dispatch'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/bundler-2.4.16/lib/bundler/vendor/thor/lib/thor/base.rb:485:in `start'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/bundler-2.4.16/lib/bundler/cli.rb:28:in `start'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/bundler-2.4.16/exe/bundle:37:in `block in <top (required)>'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/bundler-2.4.16/lib/bundler/friendly_errors.rb:117:in `with_friendly_errors'
/Users/me/.asdf/installs/ruby/3.0.4/lib/ruby/gems/3.0.0/gems/bundler-2.4.16/exe/bundle:29:in `<top (required)>'
/Users/me/.asdf/installs/ruby/3.0.4/bin/bundle:25:in `load'
/Users/me/.asdf/installs/ruby/3.0.4/bin/bundle:25:in `<main>'

Offenses print to screen without issue.

1 file inspected, 240 offenses detected, 230 offenses autocorrectable

An error occurred while Style/IdenticalConditionalBranches cop was inspecting my_file
Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker.
https://github.com/rubocop/rubocop/issues

Mention the following information in the issue report:
1.57.1 (using Parser 3.2.2.4, rubocop-ast 1.29.0, running on ruby 3.0.4) [x86_64-darwin22]
Finished in 0.819334999890998 seconds

Steps to reproduce the problem

Install rubocop 1.51.1
Install rubocop-rails 2.21.2
require rubocop-rails in whichever rubocop config file you use, in my case it is a global config in ./config/rubocop/config.yml.
Run rubocop on a file with a branch similar to the below

if object.object_preload.string.present? &&
    object.object_preload.current_item_information != 'DATA ERROR'
  object_packet[:current_items] = {}
  parsed_current_cat_information = JSON.parse(object.object_preload.current_item_information)


  parsed_current_cat_information.each_pair do |key,value|

    object_packet[:current_items][key] = {}
    object_packet[:current_items][key][:other_property] = {}

    object_packet[:current_items][key] = {:property => value['property']}
    object_packet[:current_items][key][:other_property] = [{:other_property => value['other_property']}]
                                                                    ]
  end

else
  object_packet[:current_items] = {}
end

RuboCop version

[bundle exec] rubocop -V
1.57.1 (using Parser 3.2.2.4, rubocop-ast 1.29.0, running on ruby 3.0.4) [x86_64-darwin22]
  - rubocop-rails 2.21.2
@koic
Copy link
Member

koic commented Oct 20, 2023

The provided example code results in the following syntax error. Can you provide a valid syntax repro code?

$ cat example.rb
if object.object_preload.string.present? &&
    object.object_preload.current_item_information != 'DATA ERROR'
  object_packet[:current_items] = {}
  parsed_current_cat_information = JSON.parse(object.object_preload.current_item_information)


  parsed_current_cat_information.each_pair do |key,value|

    object_packet[:current_items][key] = {}
    object_packet[:current_items][key][:other_property] = {}

    object_packet[:current_items][key] = {:property => value['property']}
    object_packet[:current_items][key][:other_property] = [{:other_property => value['other_property']}]
                                                                    ]
  end

else
  object_packet[:current_items] = {}
end

$ ruby -c example.rb
example.rb:14: syntax error, unexpected ']', expecting `end'
...                              ]
example.rb:18: syntax error, unexpected tLAMBEG
...ject_packet[:current_items] = {}

@koic
Copy link
Member

koic commented Oct 23, 2023

I was able to reproduce the issue, so I opened #12304.

@koic koic added the bug label Oct 23, 2023
koic added a commit to koic/rubocop that referenced this issue Oct 23, 2023
Fixes rubocop#12300.

This PR fixes an error for `Style/IdenticalConditionalBranches`
when `if`...`else` with identical leading lines and using index assign.

In RuboCop, the `send` node is used instead of the `indexasgn` node, so `name` cannot be used.
https://github.com/rubocop/rubocop-ast/blob/v1.29.0/lib/rubocop/ast/node/indexasgn_node.rb
@Brenntron
Copy link
Author

@koic sorry for not getting back you with corrected syntax for that code example. Thanks for fixing it!

@koic
Copy link
Member

koic commented Oct 24, 2023

@Brenntron Never mind. Thank you for your feedback!

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