AssociativeMethodInvocation

SUGGESTION

Simplification

View source code on GitHub

Summary

This method implements an associative operation, so the list of operands can be flattened

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("AssociativeMethodInvocation") to the enclosing element.

Disable this pattern completely by adding -Xep:AssociativeMethodInvocation:OFF as compiler argument. Learn more.

Samples

Replacement

Shows the difference in example code before and after the bug pattern is applied.

 import com.google.errorprone.matchers.Matchers;
 import com.google.errorprone.refaster.Refaster;
 
 class A {
   void m() {
-    Matchers.allOf(Matchers.allOf());
-    Matchers.anyOf(Matchers.anyOf());
-    Refaster.anyOf(Refaster.anyOf());
+    Matchers.allOf();
+    Matchers.anyOf();
+    Refaster.anyOf();
 
-    Matchers.allOf(Matchers.allOf((t, s) -> true));
-    Matchers.anyOf(Matchers.anyOf((t, s) -> true));
-    Refaster.anyOf(Refaster.anyOf(0));
+    Matchers.allOf((t, s) -> true);
+    Matchers.anyOf((t, s) -> true);
+    Refaster.anyOf(0);
 
     Matchers.allOf(
-        Matchers.anyOf(),
-        Matchers.allOf((t, s) -> false, (t, s) -> true),
-        Matchers.allOf(),
-        Matchers.anyOf((t, s) -> false));
+        Matchers.anyOf(), (t, s) -> false, (t, s) -> true, Matchers.anyOf((t, s) -> false));
     Matchers.anyOf(
-        Matchers.allOf(),
-        Matchers.anyOf((t, s) -> false, (t, s) -> true),
-        Matchers.anyOf(),
-        Matchers.allOf((t, s) -> false));
-    Refaster.anyOf(Matchers.allOf(), Refaster.anyOf(1, 2), Matchers.anyOf());
+        Matchers.allOf(), (t, s) -> false, (t, s) -> true, Matchers.allOf((t, s) -> false));
+    Refaster.anyOf(Matchers.allOf(), 1, 2, Matchers.anyOf());
   }
 }
 

Identification

Shows code lines which will (not) be flagged by this bug pattern.
A //BUG: Diagnostic contains: comment is placed above any violating line.

import com.google.common.collect.ImmutableList;
import com.google.errorprone.matchers.Matchers;
import com.google.errorprone.refaster.Refaster;

class A {
  void m() {
    Matchers.allOf();
    Matchers.anyOf();
    Refaster.anyOf();

    Matchers.allOf((t, s) -> true);
    Matchers.anyOf((t, s) -> true);
    Refaster.anyOf(0);

    Matchers.allOf(Matchers.anyOf((t, s) -> true));
    Matchers.anyOf(Matchers.allOf((t, s) -> true));
    Refaster.anyOf(Matchers.allOf((t, s) -> true));

    // BUG: Diagnostic contains:
    Matchers.allOf(Matchers.allOf((t, s) -> true));
    // BUG: Diagnostic contains:
    Matchers.anyOf(Matchers.anyOf((t, s) -> true));
    // BUG: Diagnostic contains:
    Refaster.anyOf(Refaster.anyOf(0));

    Matchers.allOf(Matchers.allOf(ImmutableList.of((t, s) -> true)));
    Matchers.anyOf(Matchers.anyOf(ImmutableList.of((t, s) -> true)));

    // BUG: Diagnostic contains:
    Matchers.allOf(
        (t, s) -> true, Matchers.allOf((t, s) -> false, (t, s) -> true), (t, s) -> false);
    // BUG: Diagnostic contains:
    Matchers.anyOf(
        (t, s) -> true, Matchers.anyOf((t, s) -> false, (t, s) -> true), (t, s) -> false);
    // BUG: Diagnostic contains:
    Refaster.anyOf(0, Refaster.anyOf(1, 2), 3);
  }
}

Copyright © 2017-2024 Picnic Technologies BV