Denim Datta
Denim Datta

Reputation: 3802

Indentation Check not working properly for statement label

In checkstyle I have enabled the check for Indentation. I'm getting a weird problem with it.

The check is working fine for everything other than statement label.

I have a code snippet like the following :

    public void doIt(int k) {
        for (int i = 0; i < k; i++){
           search:{
                    for (int j = 0; j < i; j++){
                    if (j == i){
                        break search;
                    }
                }
            }
        }
    }

The indent level is set as 4.

Now, if I put the statement label (search) at level 11, it should give one warning as

- label child at indentation level 11 not at correct indentation, 12

But the problem is, its giving Multiple markers at that line :

- label child at indentation level 11 not at correct indentation, 12
- label child at indentation level 11 not at correct indentation, 8

So, no matter in which level I indent the label, there will always be one/two warnings.

I didn't enabled duplicate checks for indentation with two different Indent Level.

How am I getting two warnings for a single check? How to resolve this issue?

Upvotes: 1

Views: 4406

Answers (1)

barfuin
barfuin

Reputation: 17494

This is a limitation of the IndentationCheck. The label indentation is hardcoded to be one level less than the normal indentation at this point in the tree (verified by looking at the Checkstyle 5.6 sources). The next token, which is the left curly brace, or, if you leave out the braces, the for statement, must be on the expected level. So you could format the code like this with no errors:

public void doIt(int k) {
    for (int i = 0; i < k; i++){
    search:
        for (int j = 0; j < i; j++){
            if (j == i){
                break search;
            }
        }
    }
}

It is of course a matter of personal taste, but I don't like it. I would recommend not using Checkstyle for checking the formatting, and instead use an automatic code formatter. Eclipse, for instance, has a nice formatter built in.

Upvotes: 1

Related Questions