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/DoubleNegation: does not register an offence for last line of a returned hash/array #10846

Closed
djfpaagman opened this issue Jul 29, 2022 · 0 comments · Fixed by #10855
Closed
Labels

Comments

@djfpaagman
Copy link

djfpaagman commented Jul 29, 2022

Expected behavior

It should detect all cases when used inside a hash or array.

Actual behavior

The Style/DoubleNegation cop does not detect double negations for the last line of a hash or array whe it is the returned value of a method. I think this is because of the allowed_in_returns setting, but I'm not entirely sure.

# When using `EnforcedStyle: allowed_in_returns`, allow double negation in contexts
# that use boolean as a return value. When using `EnforcedStyle: forbidden`, double negation
# should be forbidden always.

Steps to reproduce the problem

Simplified examples:

# frozen_string_literal: true

def multi_line_hash
  {
    foo: !!foo,
    bar: !!bar
  }
end

def multi_line_array
  [
    !!foo,
    !!bar
  ]
end

Only detects the offense on the lines for foo:

test.rb:5:10: C: [Correctable] Style/DoubleNegation: Avoid the use of double negation (!!).
    foo: !!foo,
         ^
test.rb:12:5: C: [Correctable] Style/DoubleNegation: Avoid the use of double negation (!!).
    !!foo,
    ^

# frozen_string_literal: true

def singe_line_hash
  { foo: !!foo }
end

def single_line_array
  [!!foo]
end

Does not detect an offense at all:

1 file inspected, no offenses detected

# frozen_string_literal: true

def hash_not_directly_returned
  hash = {
    foo: !!foo,
    bar: !!bar
  }

  do_something_with(hash)
end

def array_not_directly_returned
  array = [
    !!foo,
    !!bar
  ]

  do_something_with(array)
end

Correctly detects the offense for all lines:

test.rb:5:10: C: [Correctable] Style/DoubleNegation: Avoid the use of double negation (!!).
    foo: !!foo,
         ^
test.rb:6:10: C: [Correctable] Style/DoubleNegation: Avoid the use of double negation (!!).
    bar: !!bar
         ^
test.rb:14:5: C: [Correctable] Style/DoubleNegation: Avoid the use of double negation (!!).
    !!foo,
    ^
test.rb:15:5: C: [Correctable] Style/DoubleNegation: Avoid the use of double negation (!!).
    !!bar
    ^

RuboCop version

❯ rubocop -V
1.32.0 (using Parser 3.1.2.0, rubocop-ast 1.19.1, running on ruby 3.0.3 arm64-darwin21)
@Darhazer Darhazer added the bug label Jul 29, 2022
nobuyo added a commit to nobuyo/rubocop that referenced this issue Aug 4, 2022
…hen there is a hash or an array at return location of method
bbatsov pushed a commit that referenced this issue Aug 5, 2022
…re is a hash or an array at return location of method
WJWH pushed a commit to WJWH/rubocop that referenced this issue Aug 8, 2022
…hen there is a hash or an array at return location of method
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants