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

Commits on Oct 3, 2020

  1. Fix a false positive for Style/RedundantCondition

    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 committed Oct 3, 2020
    Copy the full SHA
    6fd1bfe View commit details
    Browse the repository at this point in the history