A code example that shows why I don't like warnings

Adam D. Ruppe destructionator at gmail.com
Fri Oct 18 16:38:38 UTC 2019

Consider this code:


string defaultFormat(alias method)() {
         static foreach(attr; __traits(getAttributes, method))
                 static if(is(typeof(attr) == string))
                         return attr;
         return "default";

void test() {}
@("test") void test2() {}

pragma(msg, defaultFormat!test);
pragma(msg, defaultFormat!test2);

void main() {}


If you compile that with warnings enabled, you will get

Warning: statement is not reachable

on test, but on test2, that very *same code* it complains about 
is executed. If you remove it, it errors on the other test2 

I know it is possible to refactor the attribute thing into a 
function (like phobos' getAttribute), but nevertheless, this kind 
of early return in a loop is a common enough pattern that I don't 
think the compiler should warn on.

I almost put this in the "Learn" forum asking for techniques, but 
really, I know how to refactor it already. I am more complaining 
about the compiler here :)

basically not a lot to say, just wanna make sure code like this 
is on the dev's radar if they ever revisit those warnings.

More information about the Digitalmars-d mailing list