Refs are assumed not-null. How to disable?

With optmizations on, the `return' on this code:

bool foo(int &k) {
  return &k != nullptr;
}

is reduced to `return true;'.

Is there an option for disabling this optimization?

Nope, this was committed in
http://llvm.org/viewvc/llvm-project?rev=209723&view=rev and doesn't
look like it has any flag control.

This code has Undefined Behavior (by binding a dereferenced null
pointer to a reference).

- David

There isn't a warning for this. There should be a tautological comparison
warning here.

Nick

David, Nick,

Nick Lewycky <nlewycky@google.com> writes:

> With optmizations on, the `return' on this code:
>
> bool foo(int &k) {
> return &k != nullptr;
> }
>
> is reduced to `return true;'.
>
> Is there an option for disabling this optimization?

Nope, this was committed in
http://llvm.org/viewvc/llvm-project?rev=209723&view=rev and doesn't
look like it has any flag control.

This code has Undefined Behavior (by binding a dereferenced null
pointer to a reference).

There isn't a warning for this. There should be a tautological comparison
warning here.

I've found code that crashes (in the best case) because of this
optimization. That code "works" on g++, MSVC, Intel C++ and Clang (until
recently).

Speaking as a compiler user, a warning (active by default) would be
highly appreciated.

I filed http://llvm.org/bugs/show_bug.cgi?id=19899 suggesting to
implement that warning.

Thanks.