S3457: "String contains no format specifiers" incorrectly works with slf4j Logger

  • SonarJava 6.0
  • SonarQube 7.9.1 LTS

Sorry for the incomplete code sample, please let me know if it needs to be improved. It happened deep inside our code.

import org.slf4j.LoggerFactory;

public class MyClass {

    public void func() {
                    try {
                         
                    } catch (final MyException e) {
                         LoggerFactory.getLogger(MyClass.class).error("Some error", e); 
                         // S3457 "String contains no format specifiers"
                    }
    }
}

It seems the reason is that Sonar mistakes the org.slf4j.Logger’ method

    public void error(String msg, Throwable t);

where the first argument is a simple string with the

    public void error(String format, Object... arguments);

which indeed has format as the first argument.

Same thing with all other Logger methods: info, debug, warn

2 Likes

Hi,

This is indeed a false positive : ticket created to handle the issue : https://jira.sonarsource.com/browse/SONARJAVA-3274

2 Likes

Hey @ruslanbes,

I looked today at actual implementation of the rule S3457, and the case should be perfectly handled already (I added some test cases just to be sure). While I agree that what you observe is a FP, the actual implementation of the rule is not supposed to raise such issues. With a small project containing only your code, I could not reproduce the FP with version 5.14.1, 6.0 and 6.0.2 of SonarJava, on a SQ LTS 7.9.2. I suspect that some misconfiguration of the project could be the root cause.

If you are still observing such FPs, could you maybe provide a bit more information?

  • Confirm you are using SonarJava 6.x
  • What version of slf4j library you are providing as dependency?
  • What’s the java version of your project?
  • How are you analyzing it? (maven?, gradle?, sonar-scanner?)

Thanks,
Michael