Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-2539

"PACKAGE BODY" initialization sections should not contain "RETURN" statements

    XMLWordPrintable

    Details

    • Type: Bug Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Remove this "RETURN" statement.
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way
    • Legacy Key:
      ReturnInInitializationSectionCheck
    • Covered Languages:
      PL/SQL
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      1h

      Description

      In a CREATE PACKAGE BODY, the purpose of the initialization section is to set the initial values of the package's global variables. It is therefore surprising to find a RETURN statement there, as all its following statements will be unreachable.

      Noncompliant Code Example

      SET SERVEROUTPUT ON
      
      CREATE OR REPLACE PACKAGE foo AS
        FUNCTION getBar RETURN PLS_INTEGER;
        bar PLS_INTEGER;
      END;
      /
      
      CREATE OR REPLACE PACKAGE BODY foo AS
        FUNCTION getBar RETURN PLS_INTEGER AS
        BEGIN
          RETURN bar; -- Compliant
        END;
      BEGIN
        bar := 42;
        DBMS_OUTPUT.PUT_LINE('package loaded');
        RETURN; -- Noncompliant
        DBMS_OUTPUT.PUT_LINE('this is unreachable code');
      END;
      /
      
      DROP PACKAGE BODY foo;
      
      DROP PACKAGE foo;
      

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            ann.campbell.2 Ann Campbell
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated: