Exception for S1142: Methods should not have too many return statements?

This is not a bug report or false positive finding per se, but I’m wondering whether the rule S1142 should apply to methods which are usually auto generated. The only example I currently have is the equals() method:

public class OverridenEquals {

  private String stringMember;
  private int intMember;

  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    if (obj == null) {
      return false;
    if (getClass() != obj.getClass()) {
      return false;
    OverridenEquals other = (OverridenEquals) obj;
    return intMember == other.intMember && Objects.equals(stringMember, other.stringMember);

This is what eclipse generates when using Objects.equals(Object, Object), using Object.equals(Object) there are many more cases and early returns.

So what do you think about this case? Would you handle the method the same as any other method? How would you refactor the method? Would you close any issue as ‘won’t fix’? Would you advise to use Lombok?

Thanks for the input!

Hello @oliver, thanks for the proposition.

I agree with you, this code is a common implementation of the equals method, in addition, this is not the first time we would add an exception related to equals method (see SONARJAVA-3133).

I created a ticket to tackle this.
I don’t see any other problematic auto-generated method, if others pop into your mind, feel free to comment the ticket!


1 Like