Search code examples
c#msbuildsonarqube

SonarQube does not detect duplication in cs code


I am testing SonarQube and this code exists in the test project:

public class Widgetor
{
    internal static int SelectValue(int ret)
    {
        switch(ret)
        {
            case 0: return 1;
            case 4: return 7;
            case -1: return 2;
            case 2: return -1;
            default: return 0;
        }
    }
    internal static int SelectValue_Copy(int ret)
    {
        switch (ret)
        {
            case 0: return 1;
            case 4: return 7;
            case -1: return 2;
            case 2: return -1;
            default: return 0;
        }
    }
}

I am Running SonarQube Version 5.6 with MSBuild Runner and MSBuil 14.

I can not find any help on how to configure my project for code dublication detection. as far as I know this should be part of it out of the box. The Project overview shows "0%" duplicated code.

I found this answer which does not really tell me anything.


Solution

  • SonarQube Duplications documentation gives a good insight:

    A piece of code is considered duplicated as soon as there are at least 100 successive and duplicated tokens (can be overridden with property sonar.cpd.${language}.minimumTokens) spread on at least 10 lines of code (can be overridden with property sonar.cpd.${language}.minimumLines).

    Accordingly, adding a couple more case to your example does trigger duplication detection.