kputnam / piggly

PL/pgSQL stored procedure code coverage tool
Other
69 stars 14 forks source link

for loop with literals violates 'loop always iterates more than once' rule #37

Closed born-in-brooklyn closed 6 years ago

born-in-brooklyn commented 6 years ago

in the case of a structure like this:

FOR i in 1..4 LOOP
/*magic happens*/
END LOOP;

piggly reports 'loop always iterates more than once' . this hard coded loop was put there by design, since it is for calculating a thing exactly 4 times. There is no conditional here, so I fail to see the lack of coverage.

kputnam commented 6 years ago

Yes, that is annoying. However, it's difficult to work support for this in, because it's still possible your loop body could exit early (using an EXIT statement or an exception). So piggly conservatively assumes any loop is capable of executing 0, 1, or 1+ iterations and records if those happen.

One other approach to handling this (besides trying to analyze the loop body, which is probably a ton of work -- exceptions can happen unpredictably, in division by zero, in calls to other procedures, if a value is unexpectedly NULL, etc, or inferring a loop cannot exit early might require evaluating complex conditional expressions) would be to allow users to add comments to annotate their code with hints to piggly.

I'm not sure how that would work in practice, at least in terms of syntax, and how piggly would know exactly which segment of code the comment applies to. But I can vaguely imagine having something like -- piggly: assume loop iterates zero times. This too would be non-trivial, so I don't imagine adding it myself anytime soon.

born-in-brooklyn commented 6 years ago

understood. no worries.