Skip to content

Commit ee0eaf2

Browse files
authored
Merge pull request #115 from jeltz/false-without-return
Detect return properly even when record check fails
2 parents 20d7831 + 3e2bcb5 commit ee0eaf2

File tree

6 files changed

+88
-2
lines changed

6 files changed

+88
-2
lines changed

expected/plpgsql_check_active.out

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3410,6 +3410,14 @@ begin
34103410
return row(10,20,30);
34113411
end;
34123412
$$ language plpgsql;
3413+
create type record03 as (a int, b int);
3414+
create or replace function rrecord03()
3415+
returns record03 as $$
3416+
declare r record := row(1);
3417+
begin
3418+
return r;
3419+
end;
3420+
$$ language plpgsql;
34133421
-- should not to raise false alarms
34143422
select * from plpgsql_check_function('rrecord01');
34153423
plpgsql_check_function
@@ -3421,8 +3429,18 @@ select * from plpgsql_check_function('rrecord02');
34213429
------------------------
34223430
(0 rows)
34233431

3432+
-- should detect different return but still detect return
3433+
select * from plpgsql_check_function('rrecord03', fatal_errors => false);
3434+
plpgsql_check_function
3435+
----------------------------------------------------------------------------------
3436+
error:42804:4:RETURN:returned record type does not match expected record type
3437+
Detail: Number of returned columns (1) does not match expected column count (2).
3438+
(2 rows)
3439+
34243440
drop function rrecord01();
34253441
drop function rrecord02();
3442+
drop function rrecord03();
3443+
drop type record03;
34263444
create or replace function bugfunc01()
34273445
returns void as $$
34283446
declare

expected/plpgsql_check_active_1.out

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3419,6 +3419,14 @@ begin
34193419
return row(10,20,30);
34203420
end;
34213421
$$ language plpgsql;
3422+
create type record03 as (a int, b int);
3423+
create or replace function rrecord03()
3424+
returns record03 as $$
3425+
declare r record := row(1);
3426+
begin
3427+
return r;
3428+
end;
3429+
$$ language plpgsql;
34223430
-- should not to raise false alarms
34233431
select * from plpgsql_check_function('rrecord01');
34243432
plpgsql_check_function
@@ -3430,8 +3438,18 @@ select * from plpgsql_check_function('rrecord02');
34303438
------------------------
34313439
(0 rows)
34323440

3441+
-- should detect different return but still detect return
3442+
select * from plpgsql_check_function('rrecord03', fatal_errors => false);
3443+
plpgsql_check_function
3444+
----------------------------------------------------------------------------------
3445+
error:42804:4:RETURN:returned record type does not match expected record type
3446+
Detail: Number of returned columns (1) does not match expected column count (2).
3447+
(2 rows)
3448+
34333449
drop function rrecord01();
34343450
drop function rrecord02();
3451+
drop function rrecord03();
3452+
drop type record03;
34353453
create or replace function bugfunc01()
34363454
returns void as $$
34373455
declare

expected/plpgsql_check_active_2.out

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3410,6 +3410,14 @@ begin
34103410
return row(10,20,30);
34113411
end;
34123412
$$ language plpgsql;
3413+
create type record03 as (a int, b int);
3414+
create or replace function rrecord03()
3415+
returns record03 as $$
3416+
declare r record := row(1);
3417+
begin
3418+
return r;
3419+
end;
3420+
$$ language plpgsql;
34133421
-- should not to raise false alarms
34143422
select * from plpgsql_check_function('rrecord01');
34153423
plpgsql_check_function
@@ -3421,8 +3429,18 @@ select * from plpgsql_check_function('rrecord02');
34213429
------------------------
34223430
(0 rows)
34233431

3432+
-- should detect different return but still detect return
3433+
select * from plpgsql_check_function('rrecord03', fatal_errors => false);
3434+
plpgsql_check_function
3435+
----------------------------------------------------------------------------------
3436+
error:42804:4:RETURN:returned record type does not match expected record type
3437+
Detail: Number of returned columns (1) does not match expected column count (2).
3438+
(2 rows)
3439+
34243440
drop function rrecord01();
34253441
drop function rrecord02();
3442+
drop function rrecord03();
3443+
drop type record03;
34263444
create or replace function bugfunc01()
34273445
returns void as $$
34283446
declare

expected/plpgsql_check_active_3.out

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3410,6 +3410,14 @@ begin
34103410
return row(10,20,30);
34113411
end;
34123412
$$ language plpgsql;
3413+
create type record03 as (a int, b int);
3414+
create or replace function rrecord03()
3415+
returns record03 as $$
3416+
declare r record := row(1);
3417+
begin
3418+
return r;
3419+
end;
3420+
$$ language plpgsql;
34133421
-- should not to raise false alarms
34143422
select * from plpgsql_check_function('rrecord01');
34153423
plpgsql_check_function
@@ -3421,8 +3429,18 @@ select * from plpgsql_check_function('rrecord02');
34213429
------------------------
34223430
(0 rows)
34233431

3432+
-- should detect different return but still detect return
3433+
select * from plpgsql_check_function('rrecord03', fatal_errors => false);
3434+
plpgsql_check_function
3435+
----------------------------------------------------------------------------------
3436+
error:42804:4:RETURN:returned record type does not match expected record type
3437+
Detail: Number of returned columns (1) does not match expected column count (2).
3438+
(2 rows)
3439+
34243440
drop function rrecord01();
34253441
drop function rrecord02();
3442+
drop function rrecord03();
3443+
drop type record03;
34263444
create or replace function bugfunc01()
34273445
returns void as $$
34283446
declare

sql/plpgsql_check_active.sql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2492,12 +2492,26 @@ begin
24922492
end;
24932493
$$ language plpgsql;
24942494

2495+
create type record03 as (a int, b int);
2496+
2497+
create or replace function rrecord03()
2498+
returns record03 as $$
2499+
declare r record := row(1);
2500+
begin
2501+
return r;
2502+
end;
2503+
$$ language plpgsql;
2504+
24952505
-- should not to raise false alarms
24962506
select * from plpgsql_check_function('rrecord01');
24972507
select * from plpgsql_check_function('rrecord02');
2508+
-- should detect different return but still detect return
2509+
select * from plpgsql_check_function('rrecord03', fatal_errors => false);
24982510

24992511
drop function rrecord01();
25002512
drop function rrecord02();
2513+
drop function rrecord03();
2514+
drop type record03;
25012515

25022516
create or replace function bugfunc01()
25032517
returns void as $$

src/stmtwalk.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,8 @@ plpgsql_check_stmt(PLpgSQL_checkstate *cstate, PLpgSQL_stmt *stmt, int *closing,
829829
{
830830
PLpgSQL_stmt_return *stmt_rt = (PLpgSQL_stmt_return *) stmt;
831831

832+
*closing = PLPGSQL_CHECK_CLOSED;
833+
832834
if (stmt_rt->retvarno >= 0)
833835
{
834836
PLpgSQL_datum *retvar = cstate->estate->datums[stmt_rt->retvarno];
@@ -894,8 +896,6 @@ plpgsql_check_stmt(PLpgSQL_checkstate *cstate, PLpgSQL_stmt *stmt, int *closing,
894896
}
895897
}
896898

897-
*closing = PLPGSQL_CHECK_CLOSED;
898-
899899
if (stmt_rt->expr)
900900
plpgsql_check_returned_expr(cstate, stmt_rt->expr, true);
901901
}

0 commit comments

Comments
 (0)