Multiple Noncompliant on the same line


I am coding a rule to detect each word of the method name written in camelCase, I realize that if several words are not good, the rule returns several times an error on the same line.

In the test file, is there a way to put several times the // Noncompliant on a single line or not?

For example, if the name of my method is line 9, and there are four words considered false, then by putting one or more // Noncompliant Sonar does come up with :
java.lang.AssertionError: Unexpected at [9, 9, 9]
I would say that we can expect only one maximum per line… but i’m not sure :confused:

Also is it a good way to do it? I mean does it work in real case? If yes, will it return several errors on the same line?

Best regards,

You can specify relative line numbers, e.g.:

// Noncompliant@+2 {{message 1}}
// Noncompliant@+1 {{message 2}}
public void myMethod(...

Thank you somuch for your answer!

Is there any other way to use the Noncompliant? I just found out that you can specify the expected message with the {{message}} so I was wondering if there was something else?

Yes, there is. This is the corresponding javadoc explaining all options: CheckVerifier - java-checks-testkit javadoc

In the test file(s), lines on which it is expected to have issues being raised have to be flagged with a comment prefixed by the “Noncompliant” string, followed by some optional details/specificity of the expected issue.

It is possible to specify the absolute line number on which the issue should appear by appending “@” to “Noncompliant”. But it is usually better to use line number relative to the current, this is possible to do by prefixing the number with either ‘+’ or ‘-’.

For example, the following comment says that an issue is going to be raised on the next line (@+1), with the given message:

// Noncompliant@+1 {{do not import "java.util.List"}}
import java.util.List;

Full syntax:
// Noncompliant@+1 [[startColumn=1;endLine=+1;endColumn=2;effortToFix=4;secondary=3,4]] {{issue message}}

Some attributes can also be written using a simplified form, for instance:
// Noncompliant [[sc=14;ec=42]] {{issue message}}

Finally, note that attributes between [[…]] are all optional:
startColumn (sc): column where the highlight starts
endLine (el): relative endLine where the highlight ends (i.e. +1), same line if omitted
endColumn (ec): column where the highlight ends
effortToFix: the cost to fix as integer
secondary: a comma separated list of integers identifying the lines of secondary locations if any

1 Like