Skip to content

Commit 3933e85

Browse files
author
Daniel Kroening
authored
Merge pull request #301 from thk123/void-return-warning
Added a warning for when function returns a value but has return void
2 parents 46393fe + f28f7ac commit 3933e85

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
void fun()
2+
{
3+
return 5;
4+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE
2+
main.c
3+
--verbosity 2
4+
^file main.c line 3 function fun: function has return void but a return statement returning signed int$
5+
^SIGNAL=0$
6+
7+
--
8+
^warning: ignoring
9+
^CONVERSION ERROR$

src/ansi-c/c_typecheck_code.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,16 @@ void c_typecheck_baset::typecheck_return(codet &code)
911911
{
912912
// gcc doesn't actually complain, it just warns!
913913
if(follow(code.op0().type()).id()!=ID_empty)
914+
{
915+
warning().source_location=code.source_location();
916+
917+
warning() << "function has return void ";
918+
warning() << "but a return statement returning ";
919+
warning() << to_string(follow(code.op0().type()));
920+
warning() << eom;
921+
914922
code.op0().make_typecast(return_type);
923+
}
915924
}
916925
else
917926
implicit_typecast(code.op0(), return_type);

0 commit comments

Comments
 (0)