How to fix sonar issues automatically using Rules or Without rule any suggestion please?

I would like to work on sonar issues with automatic fixes (using rules or without rules). Please suggest any good solutions that will be helpful for my development.

Below are the steps I used, but no output!

  1. I Got reference from this github repository https://github.com/SonarSource/sonar-java
  2. I followed all the steps but In my code doesn’t have below highlighted lines.
  3. How can I handle this problem? I am struggling on these steps
    Example Class : I tried the simple remove toString() method in String class using rules
public class StringTest {
	public static void main(String[] args) {
		String string = "hello".toString(); **// Noncompliant [[sc=17;ec=24;quickfixes=qf1]]**
**> 		// fix@qf1 {{Remove "toString()"}}**
**> 		// edit@qf1 [[sc=24;ec=35]] {{}}**
	}
}

Expectation :

  1. Any feasible solution ?
  2. Any other possible solution ?
  3. How to work the fixes without // Noncompliant ?

I hope someone can help me on this topic!

@ Colin please help me on this topic!

Hi,

To be clear, we don’t offer “automatic” fixes, but quick fixes. I.e. the user still has to take an action to apply the fix.

That said, you’re trying to write your own rule that offers quick fixes in SonarLint?

 
Ann

1 Like

@ ganncamp
Thank you for the information. I am planning to write and fix my issue using Java code (java:Sxxxxx,java:Syyyyy), which was analyzed from SonarQube like MAJOR , MINOR , INFO . I’m just looking Automatic fixes for any libraries or tools.
Domain : Java

Hi,

Sorry, but I still don’t understand. Are you trying to create new rules?

 
Ann

Hello @ ganncamp
Source : Java project
Actual :

  1. We use SonarQbue for code quality and coverage.
  2. We received Severity issues, and so on, while analyzing the sonar report.
  3. The report issue count is quite high
  4. Those issues we are fixing manually
    Expectation :
  5. I would like to fix the issue like Severities automatically using any libraries or tools
  6. Without manual work, we are expecting to fix the issue

Below are my ideas to fix :slight_smile:

  1. Using Sonar web api (SonarQube) to get the issue list (https://company.sonar.com/api/issues/search?severities=MINOR&componentKeys=AJI252-api&branch=develop&statuses=OPEN)
  2. Through java code,
    a) I am going to iterate the issue list.
    b) Inside the loop, I have written some logics based on rule to replace the code using line number, which is taken from list of JSON
    c) It is working fine
    Sample Code below,

{
“total”: 2593,
“p”: 1,
“ps”: 100,
“paging”: {
“pageIndex”: 1,
“pageSize”: 100,
“total”: 2593
},
“effortTotal”: 9719,
“issues”: [
{
“key”: “uytututyhhh”,
“rule”: “java:S2333”,
“severity”: “MINOR”,
“component”: “project-123-api:src/main/java/com/action/TestServie.java”,
“project”: “project-123-api”,
“line”: 20,
“hash”: “2a16eb52daca1405aef84621a1768287”,
“textRange”: {
“startLine”: 20,
“endLine”: 20,
“startOffset”: 1,
“endOffset”: 7
},
“flows”: ,
“status”: “OPEN”,
“message”: “"public" is redundant in this context.”,
“effort”: “2min”,
“debt”: “2min”,
“author”: “”,
“tags”: [
“clumsy”,
“finding”
],
“creationDate”: “2023-07-21T07:07:18+0000”,
“updateDate”: “2023-07-21T07:07:18+0000”,
“type”: “CODE_SMELL”,
“scope”: “MAIN”,
“quickFixAvailable”: false,
“messageFormattings”:
}
]
}

void startChangeTheOriginalData(String ruleId, String filePath, int lineNumber) {

  switch (ruleId) {
  case RuleIdConstants.MINOR_JAVA_S1858:
  	FileToStringReplace fileReplace = new FileToStringReplace();
  	fileReplace.startApplyRuleS1858(filePath, lineNumber);
  	break;
  default:
  	break;
  }

}

  String wordToReplace = "";
  String newWord = "";
  Path path = Path.of(filePath);		
  try (Stream<String> stream = Files.lines(path)) {
  	List<String> lines = stream.collect(Collectors.toList());
  	List<String> modifiedLines = new ArrayList<>();						
  		logics here........................		
  	Files.write(Path.of(filePath), modifiedLines, StandardOpenOption.TRUNCATE_EXISTING);
  } catch (Exception e) {			
  }		
private static String replaceWord(String line, String wordToReplace, String newWord) {
                     //logic here 
	return line ;

Please check above code logics whether its correct or wrong ,

Please suggest any good solution to fix the automatic way …

Thanks you …

Hi,

I’m afraid you’re on your own with this one. We might be able to help if you were writing a SonarQube plugin, but that’s not what this is. And there’s a reason we don’t do no-intervention fixes in code.

Good luck.

 
Ann