Sonarlint [squid:S1186]:Spring Tool Suite 中构造函数的方法不应为空

Sonarlint [squid:S1186]: Method should not be empty on constructor in Spring Tool Suite

我在 Spring 工具套件中使用 Sonarlint V3.5.0。当我在代码中有默认构造函数时,我收到 squid:S1186 警告:

public class TestClass{
    public TestClass() {}
}

一直有这个警告有点烦人。正如我所发现的,SonarSource 已将此作为 3.5 版中的错误解决,但最新的 Sonarlint 仍然给我这个警告。

如何使用 Sonarlint 解决这个问题?谢谢。

您无法单独使用 SonarLint 删除规则检查。您可以使用 SonarQube 服务器忽略违规规则,并将您的 SonarLint 连接到 SQ 服务器以忽略该规则,如建议的那样 here.

如 OP 中所述,SonarSource 团队在 方面存在问题。

在考虑忽略规则之前,请考虑以下事项:

squid:S1186 issue is a good practice concept - having an empty scope in general in our code, not just in a constructor, is a code anti-pattern.

在您的情况下,默认构造函数已明确实现,这显然是您正在使用的 Sprint 工具套件所必需的(这种用例讨论得很好 here)。

考虑到空范围确实是一种反模式这一事实,我建议在构造函数的空范围内添加一条注释,解释由于使用 STS,需要显式 public 默认构造函数。

这样做显然可以解决 SonarLint 提出的问题。

如果您有许多自动生成的空默认构造函数实例 - 您应该能够使用带有正则表达式的 IDE find/replace 解决方案,以便用包含解释原因的注释的范围替换空范围原来如此。