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

[Fix #10605] Fix autocorrect for Style/RedundantCondition #10606

Conversation

nobuyo
Copy link
Contributor

@nobuyo nobuyo commented May 7, 2022

...if argument for method in else branch is hash without braces.

Fixes #10605.


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

@nobuyo nobuyo force-pushed the fix-autocorrect-for-style-redundant-condition-with-hash branch from cf698dc to 56bdb4f Compare May 7, 2022 07:31
@@ -196,6 +200,10 @@ def require_parentheses?(node)
(node.respond_to?(:semantic_operator?) && node.semantic_operator?)
end

def require_braces?(node)
node.hash_type?
Copy link
Member

@koic koic May 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will get a syntax error in the following already braced case:

           if foo
           ^^^^^^ Use double pipes `||` instead.
             bar foo
           else
             bar({ baz => quux })
           end

Auto-corrected code is bar foo || { { baz => quux } }.

% ruby -ce "bar foo || { { baz => quux } }"
-e:1: syntax error, unexpected '}', expecting =>
bar foo || { { baz => quux } }

Can you add the above test case and fix it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, fixed in 97c6273.

@nobuyo nobuyo force-pushed the fix-autocorrect-for-style-redundant-condition-with-hash branch from 56bdb4f to 97c6273 Compare May 7, 2022 08:19
… argument for method in else branch is hash without braces

Update spec/rubocop/cop/style/redundant_condition_spec.rb

Co-authored-by: Koichi ITO <koic.ito@gmail.com>
@nobuyo nobuyo force-pushed the fix-autocorrect-for-style-redundant-condition-with-hash branch from 1a908bb to e7885d0 Compare May 7, 2022 09:16
@koic koic merged commit 3531610 into rubocop:master May 7, 2022
@koic
Copy link
Member

koic commented May 7, 2022

Thanks!

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

Successfully merging this pull request may close these issues.

Style/RedundantCondition making invalid corrections
2 participants