solven-eu / cleanthat

Github App opening automatically cleaning PR
56 stars 16 forks source link

SafeButNotConsensualMutators removes call to this() in constructor #816

Closed thungsten closed 3 months ago

thungsten commented 4 months ago

If we apply SafeButNotConsensualMutators to our sources, all calls of this(); inside a constructor are removed. This shouldn't be considered a safe operation, because other constructors may call the "empty" default constructor to initialise an object properly.

Example: Gradle Configuration

spotless {
  java {
    cleanthat()
      .sourceCompatibility('11')
      .addMutator('SafeAndConsensual')
      .addMutator('SafeButNotConsensual')
      .includeDraft(false)
  }
}

Java Source Code

class Horse {
  private String sound;
  private String name;

  Horse() {
    // here it is a static assignment
    // however, this might be fetched from any other class
    sound = "neigh neigh";
  }

  Horse(String name) {
    this(); // <-- this is removed if applying "SafeButNotConsensualMutators"
    this.name = name;
  }

  Horse(String name, String sound) {
    this.name = name;
    this.sound = sound;
  }  
} 
blacelle commented 4 months ago

Hello. This should be fixed in CLeanthat 2.19: https://github.com/solven-eu/cleanthat/issues/804

blacelle commented 4 months ago

@thungsten Have you been able to confirm the fix works for you?