Repository navigation
Fix an incorrect autocorrect for Style/EmptyLiteral with Hash[] as the first argument - #15863
Merged
Merged
Conversation
…s the first argument
1 task done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When
Hash[]orHash([])is the first argument of a method call without parentheses,Style/EmptyLiteralautocorrects it into invalid Ruby:Hash.newin the same position is corrected properly toassert_equal({}, foo).The cause is that
correctionusesoffense_hash_node?, which coversHash.new,Hash[]andHash([]), so it builds the parenthesized argument list({}, foo). Butreplacement_rangeonly checkedhash_node, which matchesHash.newalone. ForHash[]andHash([])the replacement therefore went over just the receiver node instead of the whole argument list.replacement_rangenow usesoffense_hash_node?as well.I found this by running
rubocop -Aover a corpus of popular gems and checking the corrected files withruby -c. It hitrack/test/spec_headers.rb(assert_equal Hash[], Rack::Headers[]).I added specs for
Hash[]with a following argument and forHash([])as the only argument. Both fail on master and pass with this change. All ofspec/rubocop/cop/stylepasses (15,595 examples), and RuboCop reports no offenses on the changed files. I did not run the fullrake default.master(if not - rebase it).bundle exec rake default. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.mdif the new code introduces user-observable changes. See changelog entry format for details.