New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnnecessaryParentheses: Reject parentheses around single parameters in lambdas #4639

Closed
JnRouvignac opened this Issue Jul 6, 2017 · 3 comments

Comments

Projects
None yet
2 participants
@JnRouvignac

JnRouvignac commented Jul 6, 2017

http://checkstyle.sourceforge.net/config_coding.html#UnnecessaryParentheses

$ cat config.xml 
<!DOCTYPE module PUBLIC
          "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
          "http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
<module name="Checker">
    <module name="TreeWalker">
        <module name="UnnecessaryParenthesesCheck">
            <property name="tokens" value="EXPR"/>
        </module>
    </module>
</module>

$ cat Test.java 
class Test {
    int foo(int y) {
        MathOperation addition = (x) -> x + x;
        return (y + 1);
    }
    interface MathOperation {
      int operation(int a);
   }
}

$ java -jar checkstyle-7.6-all.jar -c config.xml Test.java
Starting audit...
[ERROR] Test.java:4:16: Unnecessary parentheses around return value. [UnnecessaryParentheses]
Audit done.
Checkstyle ends with 1 errors.

$ java -jar checkstyle-7.6-all.jar -t Test.java | grep -E "\[3|4\:" | sed "s/^    |       //"
|--VARIABLE_DEF -> VARIABLE_DEF [3:8]
|   |--MODIFIERS -> MODIFIERS [3:8]
|   |--TYPE -> TYPE [3:8]
|   |   `--IDENT -> MathOperation [3:8]
|   |--IDENT -> addition [3:22]
|   `--ASSIGN -> = [3:31]
|       `--LAMBDA -> -> [3:37]
|           |--LPAREN -> ( [3:33]
|           |--PARAMETERS -> PARAMETERS [3:34]
|           |   `--PARAMETER_DEF -> PARAMETER_DEF [3:34]
|           |       |--MODIFIERS -> MODIFIERS [3:34]
|           |       |--TYPE -> TYPE [3:34]
|           |       `--IDENT -> x [3:34]
|           |--RPAREN -> ) [3:35]
|           `--EXPR -> EXPR [3:42]
|               `--PLUS -> + [3:42]
|                   |--IDENT -> x [3:40]
|                   `--IDENT -> x [3:44]
|--SEMI -> ; [3:45]
|--LITERAL_RETURN -> return [4:8]
|   |--EXPR -> EXPR [4:15]
|   |   |--LPAREN -> ( [4:15]
|   |   |--PLUS -> + [4:18]
|   |   |   |--IDENT -> y [4:16]
|   |   |   `--NUM_INT -> 1 [4:20]
|   |   `--RPAREN -> ) [4:21]
|   `--SEMI -> ; [4:22]

we need to add support for new token LAMBDA violate (x).

@checkstyle checkstyle deleted a comment from rnveach Jul 14, 2017

@checkstyle checkstyle deleted a comment from Vladlis Jul 14, 2017

@checkstyle checkstyle deleted a comment from rnveach Jul 14, 2017

@checkstyle checkstyle deleted a comment from Vladlis Jul 14, 2017

@romani romani changed the title from Reject parentheses around single parameters in lambdas to UnnecessaryParentheses: Reject parentheses around single parameters in lambdas Jul 14, 2017

@romani

This comment has been minimized.

Show comment
Hide comment
@romani

romani Jul 14, 2017

Member

Check should skip multiple parameters as it will force code to be non compilable:

$ javac Test.java

$cat Test.java
class Test {
    int foo(int y) {
        MathOperation addition = (x) -> x + x;
        MathOperation addition1 = x -> x + x;
        MathOperation2 addition2 = (a, b) -> a + b;
        // MathOperation2 addition3 = a, b -> a + b; // non compilable
        return (y + 1);
    }
    interface MathOperation {
      int operation(int a);
    }
    interface MathOperation2 {
      int operation(int a, int b);
    }
}

Member

romani commented Jul 14, 2017

Check should skip multiple parameters as it will force code to be non compilable:

$ javac Test.java

$cat Test.java
class Test {
    int foo(int y) {
        MathOperation addition = (x) -> x + x;
        MathOperation addition1 = x -> x + x;
        MathOperation2 addition2 = (a, b) -> a + b;
        // MathOperation2 addition3 = a, b -> a + b; // non compilable
        return (y + 1);
    }
    interface MathOperation {
      int operation(int a);
    }
    interface MathOperation2 {
      int operation(int a, int b);
    }
}

@romani

This comment has been minimized.

Show comment
Hide comment
@romani

romani Jul 14, 2017

Member

@JnRouvignac , please be welcome with patch.

Member

romani commented Jul 14, 2017

@JnRouvignac , please be welcome with patch.

rnveach added a commit to rnveach/checkstyle that referenced this issue Aug 6, 2017

rnveach added a commit to rnveach/checkstyle that referenced this issue Aug 6, 2017

rnveach added a commit to rnveach/checkstyle that referenced this issue Aug 6, 2017

rnveach added a commit to rnveach/checkstyle that referenced this issue Aug 8, 2017

romani added a commit that referenced this issue Aug 10, 2017

@romani romani added this to the 8.2 milestone Aug 10, 2017

@romani

This comment has been minimized.

Show comment
Hide comment
@romani

romani Aug 10, 2017

Member

fix is merged

Member

romani commented Aug 10, 2017

fix is merged

@romani romani closed this Aug 10, 2017

ArneLimburg pushed a commit to ArneLimburg/checkstyle that referenced this issue Aug 23, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment