java Sonarqube,记录常量字符串消息时,“字符串不包含格式说明符”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48683723/
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 10:08:21  来源:igfitidea点击:

Sonarqube, "String contains no format specifiers" when logging constant String message

javasonarqube

提问by Jonathan Schoreels

SonarQube complains about "String contains no format specifiers." when using org.slf4j.Logger, in particular method "public void debug(String msg)". For example

SonarQube 抱怨“字符串不包含格式说明符”。使用时org.slf4j.Logger,特别是方法“ public void debug(String msg)”。例如

 log.info("message");

It refers to this rule : https://wiki.sei.cmu.edu/confluence/display/c/FIO47-C.+Use+valid+format+strings

它指的是这个规则:https: //wiki.sei.cmu.edu/confluence/display/c/FIO47-C.+Use+valid+format+strings

However, in this rule, we can find the following quote :

但是,在此规则中,我们可以找到以下引用:

Each conversion specification is introduced by the % character followed (in order) by

Zero or more flags (in any order), which modify the meaning of the conversion specification

每个转换规范都由 % 字符引入(按顺序)

零个或多个标志(以任何顺序),用于修改转换规范的含义

Is it me that miss something, or is this rule not well implemented? Any experience with that ?

是我错过了什么,还是这条规则没有很好地执行?有这方面的经验吗?

回答by Wohops

This is a known issue introduced with SonarJava 5.1. You can safely consider this issue as a False Positive (FP) and/or ignore it. It has already been fixed while handling JIRA ticket SONARJAVA-2633.

这是 SonarJava 5.1 引入的一个已知问题。您可以放心地将此问题视为误报 (FP) 和/或忽略它。它已经在处理 JIRA 票SONARJAVA-2633 时得到修复。

The fix has been delivered with version 5.1.1of SonarJava analyzer, released on Feb 16, 2018 (requires SonarQube LTS 6.7 or superior).

该修复已随2018 年 2 月 16 日发布的 SonarJava 分析器5.1.1版一起提供(需要 SonarQube LTS 6.7 或更高版本)。

Update for SonarLint standalone users

SonarLint 独立用户的更新

For SonarLint users working with standalone versions (not connected to any SonarQube instance), you may still observe the issue depending of the version you are using. If you are using:

对于使用独立版本(未连接到任何 SonarQube 实例)的 SonarLint 用户,您可能仍会观察到问题,具体取决于您使用的版本。如果您正在使用:

  • SonarLint for Eclipse 3.5:It includes version 5.1.0.13090of SonarJava, so you will still observe the FP on your code. Next release will use a more recent version of SonarJava, therefore resolving the issue. Next version is expected for end of May/early June 2018.
  • SonarLint for IntelliJ 3.4(released on May 9, 2018): It includes SonarJava 5.3.0.13828, which means that the issue has been fixed. Updating your version to latest released version should then fix the issue.
  • Eclipse 3.5 的 SonarLint:它包括SonarJava的 5.1.0.13090版,因此您仍将在代码中观察 FP。下一个版本将使用更新版本的 SonarJava,从而解决该问题。下一版本预计将于 2018 年 5 月底/6 月初发布。
  • IntelliJ 3.4 的 SonarLint(2018 年 5 月 9 日发布):它包含 SonarJava 5.3.0.13828,这意味着该问题已得到修复。将您的版本更新到最新发布的版本应该可以解决问题。

回答by Rohit Chaurasiya

Noncompliant Code Example

不合规的代码示例

logger.info("Query: " , query);

logger.info("查询:", 查询);

LOGGER.info("Query: {0}", query); // issue: String contains no format specifiers

LOGGER.info("查询:{0}", 查询); // 问题:字符串不包含格式说明符

Compliant Solution

合规解决方案

LOGGER.info("Query: {}", query);

LOGGER.info("查询:{}", 查询);