Reference Branch New Code Analysis is showing code smells in unchanged lines

Must-share information (formatted with Markdown):
SonarQube: Enterprise Edition Version 9.9.2
Scanner: Maven,
Plugin: 3.11.0.3922

How is SonarQube deployed: zip

What are you trying to achieve?

For now, we are using Previous Version concept for new code analysis. We used to create a baseline version before making code changes so that current analysis will pick the new code and identify issues in it.

We are moving from Previous Version to Reference branch for new code analysis. We are using our Git Master as reference branch.

We did a POC for Reference Branch new code analysis and compared it with the outcome of Previous Version.
Also, the analysis shows same lines as new code, but Reference Branch is showing code smells at the method level in which the new code was introduced. There are not changes in method signature. Previous Version analysis is showing smells on the changed lines alone.

what have you tried so far to achieve this?

mvn clean install org.sonarsource.scanner.maven:sonar-maven-plugin:3.11.0.3922:sonar -Dsonar.host.url=
https://sonarqubehost
-Dsonar.login=token -Dversion=1.0 -DjavaVersion=11 -Dsonar.newCode.referenceBranch=master -Dsonar.coverage.jacoco.xmlReportPaths=target/site/jacoco/jacoco.xml -Dsonar.projectName=Project -Dsonar.projectKey=Project -Dsonar.branch.name=sonar-test

Hey there.

Does this issue exist in the reference branch?

Can you specify which rule is being raised, exactly?

Thank you for the response, Colin. Yes, this issue exists in reference branch (master). But in the actual branch which is being analysed, we don’t have any changes in method signature. It is just having changes in couple of lines inside the method.

Rule Cognitive Complexity of methods should not be too high. java: S3776

Hi Colin,

Did you get a chance to look at my response? Could you please explain why code smells are shown at a method level even though change happened at couple of lines inside the method?

Regards,
Shankar Vignesh S

Hi Colin, or anyone else that can help, is there any update on this please? This is causing us issues in our SDLC so it would be good to understand if this is a bug or something we are doing wrong.

Hey there.

Ah, okay I think I know what’s going on here.

This is a tricky one because, in fact, the issue is probably slightly different than the one in the target branch. Probably the cognitive complexity has changed somewhat (it might have been 20 in the reference branch, but now it’s 19 or 21).

This specific rule will always be raised on the method – and is a rare issue that can be raised on a different line than the change that caused the issue.

To give another example of an issue like this, let’s say you have a rule that targets variables that are defined but never used. You delete the only place in your file that variable got used, but the declaration remains. Should that issue fall into the new code or not? The line of the variable declaration did not change, but it’s still a new issue.

So there’s nothing you’re doing wrong – it’s just that some issues won’t (and can’t) be raised on the changed lines.