I just started using the new Eclipse 4.2 (Juno) Null Analysis.
On code like this:
x = foo();
if (x == null)
fail("x is null");
return x.bar();
I’m getting warnings that x may be null. But it can’t be, because fail always throws and therefore never returns. (With better inter-procedural analysis it could presumably determine this automatically, but it currently doesn’t seem to.)
Obviously, there are ways to rewrite the code to get around the warning, but what I’d like is a way to indicate (e.g. an annotation) that fail never returns.
I also tried to suppress the warning with @SuppressWarnings(“null”) but that didn’t work.
One way to get rid of the warning is to add: assert x != null; (assuming you have turned on the setting to include asserts in null analysis)
In GCC C++ I can do: void fn __attribute__ ((noreturn))
One semi-traditional way to do this is as follows:
so you can write
throw fail("x is null"). Of course,failwill always end up doing the throwing, not thethrow, but it’s enough to reassure the compiler that that line will always throw.