Right now when we want to delay fixing checkstyle errors (not good, but sometimes it’s needed temporarily…) or if there are false positives (I don’t think we have that with checkstyle, do we?), we use a checkstyle-suppressions.xml file.
After discussing with other devs on matrix, I’d like to propose that we change this practice to use exclusions inside the source files themselves and drop the usage of checkstyle-suppressions.xml files.
Here are the rules I’m proposing:
If the issue affects the whole class/interface/etc, use @SuppressWarnings("checkstyle:<rule name here>") on the class/interface/etc
If the issue affects a whole method, use @SuppressWarnings("checkstyle:<rule name here>") on the method
If the issue affects a variable declaration, use @SuppressWarnings("checkstyle:<rule name here>") on the variable declaration
If the issue affects a line (basically whenever an annotation is not allowed), use // CHECKSTYLE IGNORE <rule name> or // CHECKSTYLE IGNORE <rule name> FOR <n> LINES.
Example:
@Override
InternalBinaryStringEncoder getEncoder()
{
return new AbstractBouncyCastleInternalBinaryStringEncoder(new HexEncoder(), BLOCK_SIZE, CHAR_SIZE)
{
@Override
public boolean isValidEncoding(byte b)
{
// CHECKSTYLE IGNORE BooleanExpressionComplexity
return ((b >= 0x2f && b <= 0x39) || (b >= 0x41 && b <= 0x46) || (b >= 0x61 && b <= 0x66));
}
};
}
This proposal would be a must for new code and a best effort to fix existing excludes and move from the file usage to the code usage.
WDYT?
Thanks
PS1: I’ll later propose something for SonarCloud too.
PS2: We can configure the wording for the comment excludes. For now I’ve configured checkstyle with:
If we don’t care about being explicit, I’d prefer:
// CHECKSTYLE:BooleanExpressionComplexity
// CHECKSTYLE:BooleanExpressionComplexity(n)
EDIT: But I don’t mind, what you proposed it ok for me too!
EDIT2: using // CHECKSTYLE:<rule id> is a bit more in line with @SuppressWarnings("checkstyle:<rule id>"). We could also use // @SuppressWarnings("checkstyle:<rule id>") if we wanted to use the same syntax but extend it to be valid on any line.
Honestly, as long as it looks technical, and it’s not too long, it’s fine with me.
Another possibility (probably a bit too extreme):
// CS:<rule id>
Yes, I though about this too. It’s not short (but compensated by the IDE completion, before putting the // in front), but it’s consistent and it allows copy/pasting the same syntax whatever the context (easier to remember). So it’s fine with me too.
One potential downside of this best practice is that it’ll make it easier to ignore checkstyle rules (and we’re not supposed to ignore any checkstyle rules, we’re supposed to always fix them). We also need a strategy to fix the temporary ignores…
It was easier to review a single checkstyle-suppressions.xml file than searching for scattered comments or @SuppressWarnings annotations.