首页 > 解决方案 > rubocop 警察不同意: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?

标签: ruby-on-railsrubyrubocop

解决方案


要禁止cop 删除和 TrailingWhitespace之间的行中的任何空格或制表符:return false if new_record?bars.any?

def readonly?
  return false if new_record?

  bars.any?
end

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


推荐阅读