False positive in code duplications in c# preventing a merge for a failed quality gate

  • ALM used
    GitHub

  • CI system used
    AWS CodeBuild

  • Scanner command used when applicable
    dotnet sonarscanner begin (version 10.2.0)

  • Languages of the repository

  • C#

  • Only if the SonarCloud project is public
    not public

  • Error observed

We recently added a PR quality gate in our repos in Github and used the property
sonar.qualitygate.wait=true to avoid merged code where there are quality issues.

The problem now is we have a PR, where the code change is replacing an internal nuget package, this means replacing an attribute in controller methods (the methods only have this attribute once) and also updating the usings of the files. This is creating an issue in the PR for code duplication, the change is something like this (in multiple classes in different folders):

original file

using Namespace1;

public class Controller
{
    [Attribute1]
    public Task Method1()
    {
    }

    [Attribute1]
    public Task Method2()
    {
    }
}

after the change

using Namespace2;

public class Controller
{
    [Attribute2]
    public Task Method1()
    {
    }

    [Attribute2]
    public Task Method2()
    {
    }
}

Is there a way to ignore this issue?

Thanks

Hi,

Unfortunately, there’s no “accept” mechanism for duplications. You’ll need to override the merge-block manually.

 
HTH,
Ann

Ok thanks will do that, but what about the duplication error? should I create an issue in github for the analyzer? that code change should not fire a duplication error.

Juan Zamudio

Hi Juan,

Unfortunately, that’s how our duplication detection algorithm works. Per the docs:

For a block of code to be considered as duplicated:

  • Non-Java projects:
    • There should be at least 100 successive and duplicated tokens.
    • Those tokens should be spread at least on:
    • …
    • 10 lines of code for other languages

…
Differences in indentation and in string literals are ignored while detecting duplications.

 
Ann

1 Like