While solving sonarQube issue i face the below warning,does any one tell me how to overcome this warning
Method:-
@Override
public boolean equals
Try something like this:
return null != obj && this == obj || getClass() == obj.getClass() &&
this.divisionId == ((Division) obj).divisionId;
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;
}
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:
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
See also:
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.
This must work :
return this == obj
? true
: obj == null
? false
: getClass() != obj.getClass()
? false
: divisionId != ((Division)obj).divisionId
? false : true
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;
}