我们如何忽略 Java 中的一些 SonarQube 规则?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39109228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 04:05:55  来源:igfitidea点击:

How can we ignore some SonarQube rules in Java?

javasonarquberulessonar-runnersonarqube-scan

提问by WowBow

We recently started using SonarQube. We have found some rules that are suggested by SonarQube but we want to ignore them or give them a low priority and even configure the time suggested by SonarQube. For e.g

我们最近开始使用SonarQube。我们发现了一些 SonarQube 建议的规则,但我们想忽略它们或给它们一个低优先级,甚至配置 SonarQube 建议的时间。例如

We want to avoid the rule (and/or configure the priority and time suggested by SonarQube) for

我们想避免规则(和/或配置 SonarQube 建议的优先级和时间)

  1. Document this public class. and
  2. Complete the task associated to this TODO comment.
  1. 记录这个公共类。和
  2. 完成与此 TODO 评论关联的任务。

I couldn't find a way to configure this rules to be ignored. We want this kind of rules to be ignored for the whole project not specific classes.

我找不到配置忽略此规则的方法。我们希望在整个项目而不是特定类中忽略这种规则。

Configuring this values would help us to have a better time estimation to fix major issues and give low priority for the rules like the above two. We are using SonarQube 6

配置此值将帮助我们更好地估计时间来解决主要问题,并为上述两个规则提供低优先级。我们正在使用 SonarQube 6

I appericiate your advice.

我很欣赏你的建议。

回答by ΦXoc? ? Пepeúpa ツ

If you have the id of the rule ypu want to ignore, then you can add the SuppressWarningsfor that

如果您有 ypu 想要忽略的规则的 ID,那么您可以为此添加SuppressWarnings

Example:

例子:

@SuppressWarnings("squid:S0016")

I dont like this too much and use to add the comment //NOSONARthat tells SonarQubeto ignore all errors for a specific line.

我不太喜欢这个,并用于添加注释//NOSONAR,它告诉SonarQube忽略特定行的所有错误。

Example2:

示例2:

If I do this:

如果我这样做:

System.setErr(System.out);
ConsoleHandler h = new ConsoleHandler(); 
System.setErr(err);

my sonar complains asking me to use logger instead of system.out...

我的声纳抱怨要求我使用记录器而不是 system.out ...

therefore I can silent the warning doing:

因此我可以使警告静音:

System.setErr(System.out);  //NOSONAR 
ConsoleHandler h = new ConsoleHandler(); 
System.setErr(err);

回答by G. Ann - SonarSource Team

As noted in the comments, all you have to do is remove the rules from your profile or edit them to lower their priority. You need the Global Administer Quality Profiles permission to do that. Once you're logged in with that permission, go to the Rules interface, search for a rule you want to deactivate, select the rule, click on it, and Deactivate it from the relevant profile.

如评论中所述,您所要做的就是从您的个人资料中删除规则或编辑它们以降低其优先级。您需要全局管理质量配置文件权限才能执行此操作。使用该权限登录后,转到规则界面,搜索要停用的规则,选择该规则,单击它,然后从相关配置文件中停用它。