Sonar: lambda - remove useless braces around the statement

These code examples:

import java.util.Observer;

public class Main {
    public static void main(String[] args) {
        Observer observer = (o, arg) -> {
            if (arg != null) {
                System.out.println(arg);
            }
        };
    }
}

import java.util.Observer;

public class Main {
    public static void main(String[] args) {
        Observer observer = (o, arg) -> {
            try {
                String test = (String) arg;
                ...
            }
            catch (ClassCastException e) {
            }
        };
    }
}

do not comply with this rule of sonarks ::

Lamdbas containing only one statement should not insert this statement in a block: remove useless curly braces around the statement

how can i fix this?

+4
source share
1 answer

The classification of these braces as β€œworthless” is incorrect.

You can omit curly braces around one expression statement, that is, for example, a method call, an expression, newor x++, x+=yetc.

… -> { return x; } … -> x.

.

. , , .

, . ...

+7

Source: https://habr.com/ru/post/1545661/


All Articles