rules repository

G-1020

🆓
Warning

Always have a matching loop or block label.

Reason

Use a label directly in front of loops and nested anonymous blocks:

  • To give a name to that portion of code and thereby self-document what it is doing.
  • So that you can repeat that name with the end statement of that block or loop.

The PL/SQL compiler allows different start and end labels, but this makes the code harder to read and understand.

Example

Non-Compliant Example

declare
   i simple_integer := 1;
begin
   <<prepare_data>>
   begin
      null;
   end data_preparation;

   <<process_with_while_loop>>
   while (i <= 10)
   loop
      i := i + 1;
   end loop while_loop;

   <<process_with_basic_loop>>
   loop
      exit process_with_basic_loop when true;
   end loop basic_loop;

   <<process_with_for_loop>>
   for i in 1..10 loop
      sys.dbms_output.put_line(i);
   end loop for_loop;

   <<process_with_cursor_for_loop>>
   for r in (select table_name from user_tables) loop
      sys.dbms_output.put_line(r.table_name);
   end loop cursor_for_loop;
end;
/
Issues
LineColumnMessage
78End label 'data_preparation' does not match start label 'prepare_data'.
1313End label 'while_loop' does not match start label 'process_with_while_loop'.
1813End label 'basic_loop' does not match start label 'process_with_basic_loop'.
2313End label 'for_loop' does not match start label 'process_with_for_loop'.
2813End label 'cursor_for_loop' does not match start label 'process_with_cursor_for_loop'.

Compliant Solution - ★★★★★

declare
   i simple_integer := 1;
begin
   <<prepare_data>>
   begin
      null;
   end prepare_data;

   <<process_with_while_loop>>
   while (i <= 10)
   loop
      i := i + 1;
   end loop process_with_while_loop;

   <<process_with_basic_loop>>
   loop
      exit process_with_basic_loop when true;
   end loop process_with_basic_loop;

   <<process_with_for_loop>>
   for i in 1..10 loop
      sys.dbms_output.put_line(i);
   end loop process_with_for_loop;

   <<process_with_cursor_for_loop>>
   for r in (select table_name from user_tables) loop
      sys.dbms_output.put_line(r.table_name);
   end loop process_with_cursor_for_loop;
end;
/

Parameters

Use parameters to customize the rule to your needs.

ParameterDescriptionDefault Value
DisableAllQuickFixComma-separated ist of rules for which a quick fix should not be applied to all the problems in a file.Core G-3130

References