Skip to content

Latest commit

 

History

History
193 lines (155 loc) · 4.71 KB

unnecessaryparentheses.md

File metadata and controls

193 lines (155 loc) · 4.71 KB

Remove unnecessary parentheses

org.openrewrite.java.cleanup.UnnecessaryParentheses

Removes unnecessary parentheses from code where extra parentheses pairs are redundant.

Tags

  • RSPEC-1110
  • RSPEC-1611

Source

GitHub, Issue Tracker, Maven Central

  • groupId: org.openrewrite
  • artifactId: rewrite-java
  • version: 8.1.3

Example

{% tabs %} {% tab title="Test.java" %}

Before

{% code title="Test.java" %}

import java.util.*;

class Test {
    int square(int a, int b) {
        int square = (a * b);

        int sumOfSquares = 0;
        for (int i = (0); i < 10; i++) {
            sumOfSquares += (square(i * i, i));
        }
        double num = (10.0);

        List<String> list = Arrays.asList("a1", "b1", "c1");
        list.stream()
                .filter((s) -> s.startsWith("c"))
                .forEach(System.out::println);

        return (square);
    }
}

{% endcode %}

After

{% code title="Test.java" %}

import java.util.*;

class Test {
    int square(int a, int b) {
        int square = a * b;

        int sumOfSquares = 0;
        for (int i = 0; i < 10; i++) {
            sumOfSquares += square(i * i, i);
        }
        double num = 10.0;

        List<String> list = Arrays.asList("a1", "b1", "c1");
        list.stream()
                .filter(s -> s.startsWith("c"))
                .forEach(System.out::println);

        return square;
    }
}

{% endcode %}

{% endtab %} {% tab title="Diff" %} {% code %}

--- Test.java
+++ Test.java
@@ -5,1 +5,1 @@
class Test {
    int square(int a, int b) {
-       int square = (a * b);
+       int square = a * b;

@@ -8,2 +8,2 @@

        int sumOfSquares = 0;
-       for (int i = (0); i < 10; i++) {
-           sumOfSquares += (square(i * i, i));
+       for (int i = 0; i < 10; i++) {
+           sumOfSquares += square(i * i, i);
        }
@@ -11,1 +11,1 @@
            sumOfSquares += (square(i * i, i));
        }
-       double num = (10.0);
+       double num = 10.0;

@@ -15,1 +15,1 @@
        List<String> list = Arrays.asList("a1", "b1", "c1");
        list.stream()
-               .filter((s) -> s.startsWith("c"))
+               .filter(s -> s.startsWith("c"))
                .forEach(System.out::println);
@@ -18,1 +18,1 @@
                .forEach(System.out::println);

-       return (square);
+       return square;
    }

{% endcode %} {% endtab %} {% endtabs %}

Usage

This recipe has no required configuration parameters and comes from a rewrite core library. It can be activated directly without adding any dependencies. {% tabs %} {% tab title="Gradle" %} {% code title="build.gradle" %}

plugins {
    id("org.openrewrite.rewrite") version("6.1.4")
}

rewrite {
    activeRecipe("org.openrewrite.java.cleanup.UnnecessaryParentheses")
}

repositories {
    mavenCentral()
}

{% endcode %} {% endtab %} {% tab title="Maven POM" %} {% code title="pom.xml" %}

<project>
  <build>
    <plugins>
      <plugin>
        <groupId>org.openrewrite.maven</groupId>
        <artifactId>rewrite-maven-plugin</artifactId>
        <version>5.2.4</version>
        <configuration>
          <activeRecipes>
            <recipe>org.openrewrite.java.cleanup.UnnecessaryParentheses</recipe>
          </activeRecipes>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

{% endcode %} {% endtab %}

{% tab title="Maven Command Line" %} You will need to have Maven installed on your machine before you can run the following command. {% code title="shell" %}

mvn -U org.openrewrite.maven:rewrite-maven-plugin:run \
  -Drewrite.activeRecipes=org.openrewrite.java.cleanup.UnnecessaryParentheses

{% endcode %} {% endtab %} {% endtabs %}

Contributors

See how this recipe works across multiple open-source repositories

Moderne Link Image

The community edition of the Moderne platform enables you to easily run recipes across thousands of open-source repositories.

Please contact Moderne for more information about safely running the recipes on your own codebase in a private SaaS.