Define Sonar Rules

Last modified by Vincent Massol on 2024/11/19 16:14

 XWiki
 Requirements
 Completed
 

Description

The goal of this page is to define the list of quality rules to execute on Sonar for the XWiki builds.

The strategy I'm following is to enable all available rules (as they existed on 2014-08-17 in Sonar 4.4) and then start removing some, one by one, when we don't want to apply them or when they yield invalid results for us.

I'm writing down here the reasons why some rules were excluded.

First we're excluding all "Deprecated" rules.

We're also excluding all "java8" checks FTM since we're using java7.

In addition, we're excluding the following rules:

 Sonar Rule NameSonar Rule IDReason for excluding it
Headercheckstyle:com.puppycrawl.tools.checkstyle.checks.header.HeaderCheckAlready checked by our Maven License plugin
Regexp Headercheckstyle:com.puppycrawl.tools.checkstyle.checks.header.RegexpHeaderCheckAlready checked by our Maven License plugin
Copyright and license headers should be defined in all source filessquid:S1451Already checked by our Maven License plugin. In addition, I couldn't make it work since copy/pasting our license headers resulted in only the first 5 lines being copied
Strict Duplicate Codecheckstyle:com.puppycrawl.tools.checkstyle.checks.duplicates.StrictDuplicateCodeCheckAlready covered by "Duplicated blocks" (common-java:DuplicatedBlocks). In addition doesn't exclude license headers by default...
Left curly braces should be located at the end of lines of codesquid:LeftCurlyBraceEndLineCheckOur rule is to have curly braces at end of lines except for classes and methods and apparently there's no way to configure this check.
Left curly braces should be located at the beginning of lines of codesquid:LeftCurlyBraceStartLineCheckOur rule is to have curly braces at end of lines except for classes and methods and apparently there's no way to configure this check.
Right curly brace and next "else", "catch" and "finally" keywords should be located on two different linessquid:RightCurlyBraceDifferentLineAsNextBlockCheckOur rule is to have them on the same line
Dataflow Anomaly Analysispmd:DataflowAnomalyAnalysisCategorized as controversial by PMD. Seems complex to use it properly.
Bean Members Should Serializepmd:BeanMembersShouldSerializeToo many false positives for us, we don't expect our component impls to be serializable for example.
Long Variablepmd:LongVariableWe want to favor clarity and thus we don't put a cap on variable names
Missing Constructorcheckstyle:com.puppycrawl.tools.checkstyle.checks.coding.MissingCtorCheckWe don't want to force having a constructor
Public types, methods and fields (API) should be documented with Javadocsquid:UndocumentedApiNormally we should use this rule instead of the Checkstyle ones ("Javadoc Type", "Javadoc Variable" and "Javadoc Method") which are deprecated. However it doesn't let us say that we want to apply it only on classes not in the internal package.
Illegal Catchcheckstyle:com.puppycrawl.tools.checkstyle.checks.coding.IllegalCatchCheckWe want to be able to catch Exception
Avoid Catching Generic Exceptionpmd:AvoidCatchingGenericExceptionWe want to be able to catch Exception
JUnit Test Contains Too Many Assertspmd-unit-tests:JUnitTestContainsTooManyAssertsWe've not been following this practice. Not sure it's that good.
Comment Sizepmd:CommentSizeGenerates false positives with license headers
Comment Requiredpmd:CommentRequiredGenerates false positives for documenting private class fields which we don't want to enforce
Design For Extensioncheckstyle:com.puppycrawl.tools.checkstyle.checks.design.DesignForExtensionCheckWe're not doing this
Final Local Variablecheckstyle:com.puppycrawl.tools.checkstyle.checks.coding.FinalLocalVariableCheckBetter use PMD's "Local variable could be final" (pmd:LocalVariableCouldBeFinal) if we want to do this in the future (see below in the next table)
Javadoc Packagecheckstyle:com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheckA bit too much probably...
Magic Numbercheckstyle:com.puppycrawl.tools.checkstyle.checks.coding.MagicNumberCheckBetter use Squid's "Magic numbers should not be used" (squid:S109) if we want to do this in the future (see below in the next table)
Loggers should be "private static final" and should share a naming conventionsquid:S1312Cannot use since we use field logger injection
JUnit tests should include an assertpmd-unit-tests:JUnitTestsShouldIncludeAssertGood rule but doesn't work well; for ex if a method is called that does the assert, it's not taken into account
Guard Log Statementpmd:GuardLogStatementNot needed with SLF4J
Illegal Tokencheckstyle:com.puppycrawl.tools.checkstyle.checks.coding.IllegalTokenCheckUseful in "for" loops for example
Exception handlers should preserve the original exceptionsquid:S1166Sometimes we want to catch an exception and not rethrow it
Guard Debug Loggingpmd:GuardDebugLoggingNot needed with SLF4J and leads to false positives in lots of cases
Test class without test cases (JUnit 3.x only)pmd-unit-tests:TestClassWithoutTestCasesWe're using JUnit 4.x
Guard Log Statement Java Utilpmd:GuardLogStatementJavaUtilNot fully needed with SLF4J and leads to false positives
Use ConcurrentHashMappmd:UseConcurrentHashMapPotentially interesting but leads to too many false positives. For ex when inside a method we construct a Map to be returned; there's no issue about concurrency in this case.
An abstract class should have both abstract and concrete methodssquid:S1694We've not been following this practice and I don't see why it's better.
Consecutive Appends Should Reusepmd:ConsecutiveAppendsShouldReusePrevents ability to comment each line and it would be strange that the JIT doesn't recompile it at runtime.

Rules that are currently excluded but could be potentially VOTEd for inclusion in the future:

 Sonar Rule NameSonar Rule IDNotes
Law Of Demeterpmd:LawOfDemeterWe've not been following this design practice so far.
Insufficient comment densitycommon-java:InsufficientCommentDensityInteresting rule.
Short Variablepmd:ShortVariableInteresting rule since var names like i or nb are not the best for clarity.
Confusing Ternarypmd:ConfusingTernaryWould make our code more readable and consistent (right now 215 violations in commons).
Immutable Fieldpmd:ImmutableFieldCould be interesting
Avoid Literals In If Conditionpmd:AvoidLiteralsInIfCondition
Local variable could be finalpmd:LocalVariableCouldBeFinalCould be interesting
Inner Type Lastcheckstyle:com.puppycrawl.tools.checkstyle.checks.design.InnerTypeLastCheck
Magic numbers should not be usedsquid:S109
Generic exceptions Error, RuntimeException, Throwable and Exception should never be thrownsquid:S00112Nice but costly to apply
Null Assignmentpmd:NullAssignmentCould lead to false positives for setting to null for garbage collecting
Require Thischeckstyle:com.puppycrawl.tools.checkstyle.checks.coding.RequireThisCheckWe've been mostly following this rule but not enforcing it. We need to decide what we wish to do about it

 

Get Connected