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.
Try something like this:
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".
This same thing could be accomplished with one line
Well, you can replace:
with the equivalent:
This will return
false
ifdivisionId != other.divisionId
andtrue
otherwise.In SonarQube, analyzers contribute rules which are executed on source code to generate issues. There are four
types of rules
:squid:S1871
- Two branches in a conditional structure should not have exactly the same implementation: When multipleelse if() { }
same code inside the block to overcome this problem above we use extraelse {}
block with different implementation.SonarSourcerules, making Code Analyzers - Quality software comes from quality code
See also:
if(true) {...}
" and "if(false){...}
" blocks should be removedThis must work :
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,
now i changed it to,
According this question, it is similar to,
Similarly, it can be resolve like this,