We are using 8.9v Enterprise edition sonarqube, and we are doing a PR analysis wherein we only removed comments for a file but sonarqube is reporting issues for the whole file asking to refactor whole function.
Can you please let me know why is sonar asking to refactor whole file when only comments have been removed?
From your screenshot, your removal of comments was actually uncommenting code. The net effect was to add code in those changed lines and it looks like the result was to increase Cognitive Complexity in an already complex function. That is, the PR made a the issue worse, and that’s why you’re seeing it reported on PR analysis.
So there were few lines which were commented before and for this PR we removed those comments so only for the lines where the comments were removed they should be considered as new line rather than whole file right?
Yes, that’s correct. Only those line - as indicated by the yellow highlight in your screenshot - are considered new.
Going back to your OP:
I think it’s worth explicitly drawing the distinction between “whole file” and “whole function”. At a guess, it’s not reporting “new issues” on the whole file, but only on the function those new lines are in.
I can understand why that’s surprising. Cognitive Complexity issues are reported at the method/function level, and your function declaration isn’t “new”. What’s happening is that the issue is being reported in the PR because of those new lines. Your changes didn’t cause the issue - it was clearly there already - but they made it worse. And that worsening is why it’s being reported in the PR analysis.
Understood Ann.
So basically since new lines were added to the existing function and Cognitive complexity measures the whole function where the changes are made and these new lines in PR increased the score for cognitive complexity and hence it was reported in PR right Ann?