Wrong Coverage Analysis on nameof variables


  • versions used
    Version 8.3 (build 34182)

  • error observed
    Creating consts where its value is the same as the name and use it, but SonarQube Code Coverage reports “0.0% Coverage”

  • steps to reproduce

    • Create a file like “MyConstants.cs”
    • Create a const like so: public const string MyConst = nameof(MyConst);
    • Use this constant in your unit tests
    • make a build and an analysis with SonarQube
    • See that will report it as “not covered”
  • potential workaround
    Instead of public const string MyConst = nameof(MyConst); the Zero Coverage will disappear, when changing it to public const string MyConst = “MyConst”; Technically this is the same, but the content of the variable will not be changed or recognized by the compiler, when you don’t use nameof()

hi @DominikTouring

thanks for raising the issue.

  • first, what code coverage tool are you using?
  • second, could you please give us the verbose output of the END step? (please run SonarScanner.MSBuild.exe begin /k:"MyProject" /d:sonar.verbose=true as the BEGIN step, and please attach the output of END step)
  • third, could you check inside the coverage file what the entry for ExceptionConstants.ErrorId shows?


The covarge tool we are using is the TFS 2018 Build step “Visual Studio Test” - Version 2.0 ( https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/test/vstest?view=azure-devops ) - Or locally Visual Studio 2019

In coverage file, I can see that this class is never included in the coverage file - Maybe because it’s containing only “non testable” allocation and has been optimized away?

However the behavior changes on SonarQube, when not using “nameof()” - It is still not in the coverage file, but not marked as “uncovered” by SonarQube.

The SonarQube Analysis in Verbose mode is attached
SonarVerbose.zip (63.0 KB)