I don't understand haw to create new rule on the FOR loop

Hello everyone,

I’ve been looking for 2 days but without success. I am a beginner.

Someone would know how I could go about saying that if in a FOR loop we find a 3rd argument written like this: “i ++” it is noncompliant, that it should rather be written like this “++ i”?
It’s stupid I know but I’m testing a lot of stuff to gain skills on SonarQube and it’s complicated …

Hello @jojo,

If you are targeting a Java rule, I believe the best place to start is custom rules 101. Thanks to this guide, you will have a simple rule working.

Then, you can have a look at the rules that are already implemented. You can find here more than 500 examples. Here is an example of rule targeting for loop for instance.
Of course, it requires knowledge about abstract syntax trees (AST), visitor pattern, … to understand exactly what going on. Hopefully, it is quite common computer science topics, you will find plenty of documentation online.

Also note that if you believe the rule you want to implement could help many people because it is a common mistake or known bad pattern, you can also propose it in the “Suggest new features” category.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.