How to stop reporting uncovered line with only braces?

Must-share information (formatted with Markdown):

  • which versions are you using (SonarQube, Scanner, Plugin, and any relevant extension)
  • SonarQube Version 8.5.1 (build 38104)
  • SonarQube Scanner 3.3.0.1492
  • what are you trying to achieve: report uncovered lines of code

In Swift I have a computed property like:

var property: Type {
  if condition {
    return value1
  } else {
    return value2
  }
}

I have unit tests covering the if and else parts, but SonarQube reports that there’s no coverage for the line that has only the closing curly bracket, which looks like a bug to me.

Could it be solved by not having the else and make return value2 the last line of code before the curly bracket?

Thanks!

Hi,

I understand your frustration with this. I would be frustrated too. Unfortunately, at root this is a question of what your coverage engine reports. SonarQube is just reflecting what the report says. So whether there is a viable workaround in code (I think there is) I urge you to also take this up with with your coverage engine to fix the report.

And now on to the code. As you propose, I believe this change would “fix” your coverage report with the same output from the code (not that you should have to do this):

var property: Type {
  if condition {
    return value1
  }
  return value2
}

 
HTH,
Ann

Yes that’s right, I didn’t realise SonarQube merely reports what the XCode coverage report says!