SONARKT-542 Fix CPD with string templates #560
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
SONARKT-542
See here for a complete analysis of the issue.
The adopted solution is to treat the entire string template as a single
LITERAL
token. In the previous solutions, eachKtStringTemplateEntry
emitted aLITERAL
token.Downsides
This may not be perfect, as we may not detect duplication patterns in expressions within interpolations of a string template.
For example, the following string:
We would have a single
LITERAL
for the entire string template:varx5=LITERAL
.Therefore, we will not see duplications between
<interpolation1>
,<interpolation2>
, and<interpolation3>
.We will also not see equivalent interpolation structures.
For example, given the following two strings:
We will see just two
LITERAL
: one forx1
and one forx2
, instead of a sequence ofLITERAL
which would be the same, up to$something
/$somethingElse
For both scenarios mentioned above, the probability that a duplication would be detected (i.e. at least 100 equal tokens on at least 10 different lines) seems close to zero, so it's better to treat the entire template as a literal to avoid issues like the one explained in the community post.