S3457/S2629: inconsistent reporting with multiline and getter

sonarlint
java

(Jérôme) #1

Hello,

Versions used:

  • SonarLint for Eclipse 3.6.0.2018060712
  • Eclipse Photon Release 4.8.0

Here is a sample class, including the issues it raises:

protected final Logger logger = LoggerFactory.getLogger(this.getClass());

public void methodPojo(Person value) {
	// raise S3457: Format specifiers should be used instead of string concatenation.
	logger.info("My value " + value.getName());
	
	// no issue raised
	logger.info("My value "
		+ value.getName());
	
	// raise S2629: Use the built-in formatting to construct this argument.
	logger.info("My value " + value);
	
	// raise S2629: Use the built-in formatting to construct this argument.
	logger.info("My value "
		+ value);
}

public void methodString(String value) {
	// raise S2629: Use the built-in formatting to construct this argument.
	logger.info("My value " + value);
	
	// raise S2629: Use the built-in formatting to construct this argument.
	logger.info("My value "
		+ value);
}

public class Person {
	private String name;
	private int age;
	
	public String getName() {
		return name;
	}
	
	public int getAge() {
		return age;
	}
}

This snippet uses an SLF4J Logger.

  1. Why do I have different issues whether I use a getter or not ?
  2. No issue is raised when using a getter on a multi line statement. Is this expected ?

Thanks in advance for your answers.


(Nicolas Peru) #2

I am not sure I understand your first point given your example.

Regarding the multi line : this is because of that (old) ticket : https://jira.sonarsource.com/browse/SONARJAVA-1153 but I suspect that this could be improved in the description of the rule.


(Jérôme) #3

For the first point. Admit value is a Pojo with some getter.

If I write logger.info("My value " + value);, the rule S2629 raises the issue Use the built-in formatting to construct this argument. The correct form is logger.info("My value {}", value);.

If I write logger.info("My value " + value.getName());, the rule S3457 raises the issue Format specifiers should be used instead of string concatenation. The correct form is logger.info("My value {}", value.getName());.

Why do I have two different rules for the same kind of error ?

If I write:

	logger.info("My value "
		+ value.getName());

none of the rules mentionned above raise an issue.