6

While solving sonarQube issue i face the below warning,does any one tell me how to overcome this warning

Method:-

@Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Division other = (Division) obj;

        if (divisionId != other.divisionId)
        //getting warning for above if condition

            return false;
        return true;
    }

Warning :

Replace this if-then-else statement by a single return statement.

Description:-

Return of boolean literal statements wrapped into if-then-else ones should be simplified.

6 Answers6

19

Well, you can replace:

if (divisionId != other.divisionId)
    return false;
return true;

with the equivalent:

return divisionId == other.divisionId;

This will return false if divisionId != other.divisionId and true otherwise.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • @h36p If the condition is `false`, `false` will be returned by the method. – Eran Aug 22 '17 at 12:29
  • As @Usagi answered, you don't need to cast the instance in a separate statement. Since it is used only once you can do `return divisionId == ((Division)obj).divisionId;` – fedup Sep 24 '19 at 03:51
5

I received a similar kind of warning message when using sonarlint "Return of boolean expressions should not be wrapped into an "if-then-else" statement" this was my code previously,

if (val.isEmpty()) {
    switchCompat.setChecked( false );
} else {
    switchCompat.setChecked( true );
}

now i changed it to,

boolean checked = val.isEmpty();
switchCompat.setChecked( checked );

According this question, it is similar to,

@Override
public boolean equals(Object obj) {
    Division other = (Division) obj;
    if (this == obj)
        return true;
    else if (obj == null)
        return false;
    else if (getClass() != obj.getClass())
        return false;
    else if (divisionId != other.divisionId)
        return false;
    else
        return true;
}

Similarly, it can be resolve like this,

@Override
public boolean equals(Object obj) {
    boolean success;
    Division other = (Division) obj;

    if (this == obj)
        success = true;
    else if (obj == null)
        success = false;
    else if (getClass() != obj.getClass())
        success = false;
    else if (divisionId != other.divisionId)
        success = false;
    else
        success = true;
    return success;
}
4

Sonar Qube Rule:squid:S1126 - Return boolean expressions instead of boolean literal

In SonarQube, analyzers contribute rules which are executed on source code to generate issues. There are four types of rules:

  • Code Smell (Maintainability domain)
  • Bug (Reliability domain)
  • Vulnerability (Security domain)
  • Security Hotspot (Security domain)
Noncompliant Code Example      |  Compliant Solution
---------------------------    |  ----------------------------
boolean foo(Object param) {    |  boolean foo(Object param) {
    /*Some Condition*/         |    boolean expression = false;
    if(param == null) {        |    if(param != null) { // param == null - squid:S4165
        return true;           |        //expression = true; //(squid:S4165)
    }                          |    //} else {
                               |        if(/**/) { // Compliant
    if(/**/){/* Noncompliant*/ |            expression = true; 
        return true;           |        } else if(/**/) {
    } else if(/**/) {          |            expression = true;      
        return true;           |        } else if(/**/) { // squid:S1871
    } else if(/**/) {          |            expression = true;      
        return true;           |        } else { // To avoid else.
    }                          |            expression = false;
    return false;              |        }
}                              |    }
                               |    return expression;
                               |  }

squid:S1871 - Two branches in a conditional structure should not have exactly the same implementation: When multiple else if() { } same code inside the block to overcome this problem above we use extra else {} block with different implementation.


SonarSourcerules, making Code Analyzers - Quality software comes from quality code

  • SonarQube Continuous Code Quality - Analyze code in your, on-premise CI. For Online Use SonarQube as a Service
  • Use Sonarlint which Catches the issues on the fly, in your IDE.

See also:

Yash
  • 9,250
  • 2
  • 69
  • 74
1

Not completely sure of your intent for the if-statements that return false but it seems as if you could simply always return false unless "this == obj".

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    else
        return false;
}

This same thing could be accomplished with one line

@Override
public boolean equals(Object obj) {
    return this == obj;
}
St-rm
  • 13
  • 4
1

This must work :

return this == obj
        ? true
        : obj == null
            ? false 
            : getClass() != obj.getClass()
                ? false
                : divisionId != ((Division)obj).divisionId
                    ? false : true
0

Try something like this:

return null != obj && this == obj || getClass() == obj.getClass() &&
       this.divisionId == ((Division) obj).divisionId;
Usagi Miyamoto
  • 6,196
  • 1
  • 19
  • 33