Sonar Cloud complains about duplicates when new items are added to a list or object initializers are used

We are finding that Sonar Cloud is giving false reports about code duplication. In the first case if there is a file with constants and a new value is added it reports 100% duplication
Language c#

public static class Countries
{
   public const int Canada = 1;
   public const int Denmark =2;  <-- this new line will be reported as 100% duplicated.
}

The second case if some code is creating a bunch of objects and using object initializer syntax the code is reported as duplicate.

var list = new List<MyType>{
new MyType{
  Property1 = "xxx",
  Property2 = 123,
},
new MyType{
  Property1 = "xxx",
  Property2 = 124,
};
}

This block of code will be reported as duplicate. Even though new objects are being created and they differ. It is possible to extract the assignment of Property1 to reduce duplication changing the code would make it much less understandable.

Hello @Darryl_Braaten

My apologies for the late answer. Could you please share with us a small reproducer project that we can investigate?

Thank you,
Andrei

There was a new feature suggestion raised a while ago asking to be able to mark duplications as won’t fix etc, but they didn’t seem to think it would be possible and that you should just exclude the whole file from duplication analysis

Sorry I can only submit code to projects our devops group has setup. I don’t have enough rights to start a new project.
We will just work around by not blocking merges when the quality gate fails.

Does this problem occur that often in order to make such a radical change?

Rather than not blocking merges when the QG fails, you can workaround by modifying the QG to not fail on duplications (and still keep all the other parameters for failing the QG), and keep the condition of a green QG for merging PRs.

We are early in our use of Sonar Cloud we are not blocking merges yet and will just leave that capability off.