如果块有两行以上,则不应拥抱 return 语句 (wsl)

Ste*_*tta 6 go

我的 golint 返回此错误消息,但我真的不明白它的含义。

如标题:

return statements should not be cuddled if block has more than two lines (wsl)

我的代码是这样的:

func validateCountry(product models.Product, countries []models.Country) bool {
    if !product.CountryCode.Valid {
        return true
    }

    for _, country := range countries {
        if country.Code == product.CountryCode.String {
            return !country.Enabled && country.Deprecated
        }
    }

    return false
}
Run Code Online (Sandbox Code Playgroud)

linter 不喜欢的似乎是最后一个return false

我很困惑,我没有在这个代码库中设置 linter,我真的不知道如何跳过这个规则或如何修复它。

Kor*_*las 6

此错误意味着,在您的情况下,您需要在任何下一个 return 语句之前放置一个空行:

[empty line] <-- need this
return true
...
[empty line] <-- need this
return !country.Enabled && country.Deprecated
Run Code Online (Sandbox Code Playgroud)

should not be cuddled 意味着 return 语句附近不能有代码行。

  • 也许我是个白痴。但为什么它不只是说(你需要在返回上方有一个空的新行) (3认同)