Detecting Code Duplications Within the Same Python File in SonarQube

Hi everyone,

I’m quite new to SonarQube and currently exploring its features. As part of my evaluation, I created two simple Python scripts that share some duplicated code. SonarQube successfully detects duplicated lines and code blocks between these two files, which is great.

However, I noticed that it doesn’t detect similar duplications within the same file. Is this behavior intentional? And if not, is there a specific setting I need to adjust to enable duplication detection within a single Python script?

For testing, I am using the following command to run the analysis and view the results on my local SonarQube server (localhost):
sonar-scanner.bat -D"sonar.projectKey=python-test" -D"sonar.sources=." -D"sonar.host.url=http://localhost:9000" -D"sonar.token=sqp_..."

Any guidance would be greatly appreciated!

Best regards,
Aaron

Hello!

Yes, SonarQube is capable of detecting code duplication within the same file. You can see an example of this in my SonarQube Cloud organization.

This functionality works out of the box and does not require any special configuration. However, SonarQube does provide several configuration parameters related to duplication detection that you can adjust if you want to fine-tune its behavior or results. You can find detailed information about these parameters in the documentation: Duplication Check Configuration.