rubocop 警察不同意:Layout/EmptyLineAfterGuardClause vs Layout/TrailingWhitespace

rubocop cops disagree: Layout/EmptyLineAfterGuardClause vs Layout/TrailingWhitespace

Ruby 2.6.5 Rails 5.2.3

当我运行rubocop app/models/foo.rb时,我得到:

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

所以我做了改变:

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

#after
def readonly?
  return false if new_record?

  bars.any?
end

得到:

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

1 file inspected, 1 offense detected

修复一个会触发另一个,反之亦然。

如果我想让这个文件通过 rubocop 并且在 Ruby/Rails 方面表现良好,最好忽略哪个 cop?

要抑制 TrailingWhitespace cop,删除 return false if new_record?bars.any? 之间行中的任何 space 或制表符:

def readonly?
  return false if new_record?

  bars.any?
end

尾随白色space \s 是行尾的任何 space、制表符、回车 return,后面没有任何其他字符。