RefasterMethodParameterOrder

SUGGESTION

Style

View source code on GitHub

Summary

Refaster template parameters should be listed in a canonical order

Suppression

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

Disable this pattern completely by adding -Xep:RefasterMethodParameterOrder: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.refaster.annotation.AfterTemplate;
 import com.google.errorprone.refaster.annotation.BeforeTemplate;
 
 class A {
   class UnusedUnsortedParameters {
     @BeforeTemplate
-    void before(int b, int a) {}
+    void before(int a, int b) {}
   }
 
   class UnsortedParametersWithoutAfterTemplate {
     @BeforeTemplate
-    int before(int a, int b, int c, int d) {
+    int before(int b, int a, int d, int c) {
       return b + a + d + b + c;
     }
   }
 
   class UnsortedParametersWithMultipleMethodsAndParameterCounts {
     @BeforeTemplate
-    int before(int b, int a, int g, int f, int d) {
+    int before(int d, int a, int f, int b, int g) {
       return f + a + g + b + d;
     }
 
     @AfterTemplate
     int after(int a, int b) {
       return b + a;
     }
 
     @AfterTemplate
-    int after2(int a, int d, int f) {
+    int after2(int d, int a, int f) {
       return d + a + f;
     }
   }
 }
 

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 static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
import static org.assertj.core.api.Assertions.assertThat;

import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import com.google.errorprone.refaster.annotation.Placeholder;
import com.google.errorprone.refaster.annotation.UseImportPolicy;
import java.util.Map;

class A {
  class UnusedLexicographicallyOrderedParameters {
    @BeforeTemplate
    void singleParam(int a) {}

    @BeforeTemplate
    void twoParams(int a, int b) {}

    @Placeholder
    void notATemplateMethod(int b, int a) {}
  }

  class NonParameterValueIdentifierIsIgnored<K, V> {
    @AfterTemplate
    @UseImportPolicy(value = STATIC_IMPORT_ALWAYS)
    void after(Map<K, V> map, V value) {
      assertThat(map).containsValue(value);
    }
  }

  // BUG: Diagnostic contains:
  class UnusedLexicographicallyUnorderedParameters {
    @BeforeTemplate
    void foo(int a, int b) {}

    @BeforeTemplate
    void bar(int b, int a) {}
  }
}

Copyright © 2017-2024 Picnic Technologies BV