felipebz / zpa

Parser and static code analysis tool for PL/SQL and Oracle SQL.
https://zpa.felipebz.com
GNU Lesser General Public License v3.0
213 stars 78 forks source link

Rule plsql:SameBranch should check only consecutive branches #73

Closed felipebz closed 6 years ago

felipebz commented 6 years ago

Considering this code:

if foo.type = 1 then
  do_a_thing();
elsif check_value(foo) then
  go_another_thing();
elsif foo.type = 3 then
  do_a_thing(); -- Issue reported here
end if;

Now, let's suppose that a developer fixed this issue changing the code to this:

if foo.type in (1, 3) then
  do_a_thing();
elsif check_value(foo) then
  go_another_thing();
end if;

Then we have a behavior change and probably a bug, much harded to track. In the original code, when foo.type = 3 the do_a_thing() procedure is only executed when check_value(foo) is false. In the new code, it is executed always.

The safest option to avoid this problem is make the rule only report an issue when the two branches are consecutive, lile:

if foo.type = 1 then
  do_a_thing();
elsif foo.type = 3 then
  do_a_thing(); -- Issue reported here
elsif check_value(foo) then
  go_another_thing();
end if;