rspec/rules/S2340/plsql/rule.adoc

53 lines
941 B
Plaintext
Raw Permalink Normal View History

== Why is this an issue?
2021-01-27 13:42:22 +01:00
Simple loops, of the form ``++LOOP ... END LOOP++``, behave by default as infinite ones, since they do not have a loop condition. They can often be replaced by other, safer, loop constructs.
2020-06-30 12:48:07 +02:00
=== Noncompliant code example
2020-06-30 12:48:07 +02:00
2022-02-04 17:28:24 +01:00
[source,sql]
2020-06-30 12:48:07 +02:00
----
SET SERVEROUTPUT ON
DECLARE
i PLS_INTEGER;
BEGIN
i := 1;
LOOP -- Noncompliant, an infinite loop by default and therefore dangerous
DBMS_OUTPUT.PUT_LINE('First loop i: ' || i);
i := i + 1;
EXIT WHEN i > 10;
END LOOP;
END;
/
----
=== Compliant solution
2020-06-30 12:48:07 +02:00
2022-02-04 17:28:24 +01:00
[source,sql]
2020-06-30 12:48:07 +02:00
----
SET SERVEROUTPUT ON
DECLARE
i PLS_INTEGER;
BEGIN
FOR i IN 1..10 LOOP -- Compliant, much safer equivalent alternative
DBMS_OUTPUT.PUT_LINE('Second loop i: ' || i);
END LOOP;
END;
/
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Give this loop an end condition.
endif::env-github,rspecator-view[]