简体   繁体   中英

How to enforce error when function has no return in gcc?

Is it possible to enforce an error (to break the build) when a function definition has no return in its body?

Consider this function:

int sum(int a, int b) {
  int c = a + b;
  // and here should be return
};

When I compile with g++ -Wall , I get:

no return statement in function returning non-void [ -Wreturn-type ]

but I want this to be a hard error rather than a warning.

I'm currently using GCC 4.9.2, but if there's a solution for different version of GCC that would be helpful to know, too.

GCC has the option -Werror to turn all warnings into errors.

If you want to upgrade only a specific warning, you can use -Werror=X , where X is the warning type, without the -W prefix. In your particular case, that would be -Werror=return-type .

Note that this will only report missing return in a function that returns a value. If you want to enforce that return; must be explicitly written in a function returning void , you may be out of luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM