简体   繁体   中英

rubocop cops disagree: Layout/EmptyLineAfterGuardClause vs Layout/TrailingWhitespace

Ruby 2.6.5 Rails 5.2.3

When I ran rubocop app/models/foo.rb , I got:

app/models/foo.rb:24:5: C: Layout/EmptyLineAfterGuardClause: Add empty line after guard clause.
    return false if new_record?
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected

So I made the change:

# before
def readonly?
  return false if new_record?
  bars.any?
end

#after
def readonly?
  return false if new_record?

  bars.any?
end

And got:

app/models/foo.rb:25:1: C: Layout/TrailingWhitespace: Trailing whitespace detected.

1 file inspected, 1 offense detected

Fixing one triggered the other and vice-versa forever.

If I want this file to pass rubocop and be good in terms of Ruby/Rails way, which cop is best to ignore?

To suppress TrailingWhitespace cop remove any space or tab in the line between return false if new_record? and bars.any? :

def readonly?
  return false if new_record?

  bars.any?
end

Trailing whitespace \\s is any space, tab, carriage return at the end of a line, without any other characters following it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM