[cfe-dev] Is -Wno-return-type a warning or error?

Robinson, Paul via cfe-dev cfe-dev at lists.llvm.org
Thu Oct 29 22:47:39 PDT 2015


C99 6.8.6.4 p1 says:  A return statement without an expression shall only appear in a function whose return type is void.

So, the bare "return;" in foo() is really an error.

Falling off the end of a non-void function (without an actual "return;" statement) is UB only if the return value is used by the caller.
That is, if you removed the "return;" from foo() and then didn't use its return value in the call from main(), it would be legal.  Although, I'd guess you'd see the same diagnostic as a warning, not an error.
--paulr

From: cfe-dev [mailto:cfe-dev-bounces at lists.llvm.org] On Behalf Of David Blaikie via cfe-dev
Sent: Thursday, October 29, 2015 8:10 AM
To: Dan Liew
Cc: Clang Dev
Subject: Re: [cfe-dev] Is -Wno-return-type a warning or error?


Intentional, I believe. We do have some warning which default to being errors. I'm not sure of the specifics of the c language standard in this case, but perhaps there's some case where this is not UB (if the return is never executed, perhaps) so it may not technically be invalid c.
On Oct 29, 2015 4:55 AM, "Dan Liew via cfe-dev" <cfe-dev at lists.llvm.org<mailto:cfe-dev at lists.llvm.org>> wrote:
Hi,

I was playing with Clang 3.7 on some old code I noticed that
``-Wreturn-type`` seems to be treated as error.

For example

```
int foo() {
    int x = 5;
    return;
}

int main() {
    int x = foo();
    return 0;
}
```

Now try building
```
$ clang -std=c99 noret.c
noret.c:3:5: error: non-void function 'foo' should return a value
[-Wreturn-type]
    return;
    ^
1 error generated.
```

It's good that Clang flags up bad code like this but what is confusing
me is the Clang driver is

* Treating this as an error even though I haven't passed ``-Werror``.
* I can suppress this with ``-Wno-return-type``.

So I'm not really sure if this is a "warning" or an "error". I expect

* If it's an "error" then I shouldn't be able to suppress it with the
``-Wno-return-type`` flag.
* If it's a "warning" and I haven't passed ``-Werror`` then Clang
should note the warning but not treat it as an error.

Is Clang's current (and reasonable) behaviour intentional or this a bug?

Thanks,
Dan.
_______________________________________________
cfe-dev mailing list
cfe-dev at lists.llvm.org<mailto:cfe-dev at lists.llvm.org>
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20151030/b2bc9e73/attachment.html>


More information about the cfe-dev mailing list