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 a false positive for Style/RedundantCondition #8807

Merged

Conversation

koic
Copy link
Member

@koic koic commented Sep 28, 2020

This PR fixes the following incorrect autocorrect for Style/RedundantCondition when using assignment by hash key access.

Before

The result of auto-correction is syntax error.

% cat example.rb
if @columns_cache[table_name]
  @columns_cache[table_name]
else
  @columns_cache[table_name] = super(table_name)
end

% bundle exec rubocop -a --only Style/RedundantCondition
(snip)

Inspecting 1 file
C

Offenses:

example.rb:1:1: C: [Corrected] Style/RedundantCondition: Use double
pipes || instead.
if @columns_cache[table_name] ...
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense corrected

% cat example.rb
@columns_cache[table_name] || []=(table_name, super(table_name))

% ruby -c example.rb
example.rb:1: syntax error, unexpected '=', expecting end-of-input
...olumns_cache[table_name] || []=(table_name, super(table_name...

After

The cop accepts the example case.


Before submitting the PR make sure the following are checked:

  • 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.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

@dvandersluis
Copy link
Member

Not sure if your example makes perfect sense here, because it should be @columns_cache[table_name] ||= super(table_name) since it was originally a setter, right? I'm not sure if setters are handled by this cop (doesn't appear so from the tests), another one, or not at all.

@koic koic force-pushed the fix_incorrect_autocorrect_for_redundant_condition branch from 61f7a9d to 16f8699 Compare September 28, 2020 21:44
@koic koic changed the title Fix an incorrect autocorrect for Style/RedundantCondition Fix a false positive for Style/RedundantCondition Sep 28, 2020
@koic
Copy link
Member Author

koic commented Sep 28, 2020

My example was incorrect and your feedback is perfectly right. In any case, there was an incompatible auto-correction, and first of all, I made it possible to accept the example case where hash assignment setter is used. I updated this PR. Thank you for your review!

@koic koic force-pushed the fix_incorrect_autocorrect_for_redundant_condition branch from 16f8699 to b5305b6 Compare September 29, 2020 08:50
This PR fixes the following incorrect autocorrect for `Style/RedundantCondition`
when using assignment by hash key access.

### Before

The result of auto-correction is syntax error.

```console
% cat example.rb
if @columns_cache[table_name]
  @columns_cache[table_name]
else
  @columns_cache[table_name] = super(table_name)
end

% bundle exec rubocop -a --only Style/RedundantCondition
(snip)

Inspecting 1 file
C

Offenses:

example.rb:1:1: C: [Corrected] Style/RedundantCondition: Use double
pipes || instead.
if @columns_cache[table_name] ...
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense corrected

% cat example.rb
@columns_cache[table_name] || []=(table_name, super(table_name))

% ruby -c example.rb
example.rb:1: syntax error, unexpected '=', expecting end-of-input
...olumns_cache[table_name] || []=(table_name, super(table_name...
```

## After

The cop accepts the example case.
@koic koic force-pushed the fix_incorrect_autocorrect_for_redundant_condition branch from b5305b6 to 6fd1bfe Compare October 3, 2020 17:31
@koic koic merged commit 918f7bb into rubocop:master Oct 3, 2020
@koic koic deleted the fix_incorrect_autocorrect_for_redundant_condition branch October 3, 2020 18:14
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.

None yet

2 participants