Rubocop: Style/GuardClause false positive

Created on 4 May 2020  路  4Comments  路  Source: rubocop-hq/rubocop

Style/GuardClause false positive for code like this:

b = nil
a = 
  if b
    true || raise "err"
  else
    puts "else"
  end

Expected behavior

No trigger

Actual behavior

Cop triggers on if statement

Steps to reproduce the problem

rubocop code above

RuboCop version

$ [bundle exec] rubocop -V
0.82.0 (using Parser 2.7.1.2, running on ruby 2.6.6 x86_64-darwin19)
bug

All 4 comments

I think this is not a false positive, so it is an expected behavior:

# bad
if cond
  foo || raise('hi')
else
  bar
end

# good
foo || raise('hi') if cond

bar

OTOH, the offense message for that would be incorrect. I have opened a PR #7932. Thank you.

So:

# bad
a = 
  if cond
    foo || raise('hi')
  else
    bar
  end

# good  
a = foo || raise('hi') if cond
a ||= bar

?

Ah, that case is a false positive. If assigned, this cop should be accept it. Seems to have multiple issues. I will open the patch for it.

I opened a PR #7935 to solve this issue.

Was this page helpful?
0 / 5 - 0 ratings