Sonarcloud duplicate code showing two locations

Template for a good new topic, formatted with Markdown:

  • ALM: Bitucket cloud
  • CI system used: Bitbucket Cloud
  • Scanner command used when applicable (private details masked)
  • Languages of the repository: C/C++
  • Only if the SonarCloud project is public: Not Public
  • Error observed: not an “error”
  • Steps to reproduce: TBD
  • Potential workaround: ignore issue

I’ve got a repository which has some code duplication blocks/lines marked in the develop branch, which we tag upon commit and use as the metric for “new code” when we do PRs. In a recent PR, a developer added 1 line of code in a block that is duplicate, which then raised a bunch of these as issues for his PR.

What is weird is that we were looking through the baseline and found that in the code, sonarcloud had marked code as duplicate, with two locations. One makes sense, one seems totally unreleated except that it is in the same file. Here is the duplication that makes sense:

And here is the “weird” one:


Thoughts on why the odd reference?

Hello @mmedved,

Welcome to the community!

It is hard to guess why the two-block are considered duplicates without looking at the actual example.
The rule for C++ duplicate block is:

  • 10 lines of code
  • Differences in the indentation and in string literals are ignored while detecting duplications.

If you ignore the string literals in your example, does the duplication start to make sense?


Yeah, the weird thing isn’t that the lines were identified as duplicate, that makes sense. The weird thing is that it has two references. The one is totally correct. The other makes absolutely no sense and is not even close to duplicated. The weird one is also a duplicated block itself, but of a totally different batch of code.

Can you provide a small reproducer where I can reproduce the issue? Or a detailed screenshot of all the reports and the code it is pointing to. Otherwise it is impossible for me to help with the provided information.