java:S6909 false positive when inside control statements

  • What language is this for: Java
  • Which rule: S6909
  • Why do you believe it’s a false-positive/false-negative: because it does not consider control statements
  • SonarLint - Eclipse - not connected

Imagine the following code:

try (final PreparedStatement stmt = con.prepareStatement(STATEMENT, Statement.RETURN_GENERATED_KEYS)) {
	for (final AccountMovement accountMovement : this.accountMovements) {
....
		if (accountMovement.getOtherAccountnumber() == null)
			stmt.setNull(11, Types.BIGINT);         <----------------------
		else
			stmt.setLong(11, accountMovement.getOtherAccountnumber());
....

S6909 will complain saying the Types.BIGINT line could be moved outside the for loop - which of course is not true and would change the semantics because it is inside an if statement!

Hi @OlliL,

Welcome to the community and thank you for reporting this FP!

I feel the pain here since the suggestion is likely to introduce a bug. Hopefully, the sample you shared was just for education and you see how you could improve this to be more efficient :wink:

However, I can reproduce the problem locally, and I created a ticket to track progress on the issue.

Cheers,

Dorian

Oh… it was indeed a reduced form of live code:

Would be cool to hear what you had in mind to improve it to be more efficient :slight_smile:

Sorry, you can discard my comment on efficiency.
I misread the original snippet and thought you were assigning the value in position 11 with the same method called in both branches. In which case you could have moved the test of nullability into a variable assignment and called the method with the same variable without branching anymore.
But that does apply here