Search code examples
javaspringsonarqubereturnvoid

Getting : Remove this Redundant Jump in SonarQube


I have a method in which there is a if else conditions . The if-else looks something similar as below.

public void agreementCoduct(String agreement) {
       if(agreement.equals("Pass")) {
         { //do someting
           return; // getting Remove this Redundant Jump
         } else if(aggrement.equals("NotPass")) {
           // do something
           return; // getting Remove this Redundant Jump
        } else {
          // do something 
          return; // getting Remove this Redundant Jump
        }

}

I'm running Sonarqube to check the code quality. It is showing the message "Remove this redundant jump." . I want to know how can I remove this Code smell and still achieve what I want. as this is void method so return; should work fine?


Solution

  • The return statements have no effect because you do not have any code after the if-elseif-else block. If you remove the return statements, your code will leave the matching if-block and return at the end of the method.

    Only the first one of the if-elseif-else blocks that evaluates to true gets executed.

    A void-method has an implicit "return" statement in the end.