www.digitalmars.com

D Programming Language 2.0


Last update Sun Dec 30 20:21:55 2012

Warnings

Depending on one's point of view, warnings are either a symptom of broken language design or a useful ‘lint’ like tool to analyze code and look for potential trouble spots. Most of the time, those trouble spots will be legitimate code intended to be that way. More rarely, it may indicate an unintentional error on the part of the programmer.

Warnings are not a defined part of the D Programming Language. They exist at the discretion of the compiler vendor, and will most likely vary from vendor to vendor. All constructs for which an implementation may generate a warning message are legal D code.

These are the warnings generated by the Digital Mars D compiler when the -w switch is thrown. This mode also forces compiler to stop if any warning is produced. Most have generated some spirited debate as to whether it should be a warning, an error, and what the correct way to write D code in these situations should be. Since no consensus emerged, they appear as optional warnings, with alternatives on how to write correct code.

warning - statement is not reachable

Consider the following code:

int foo(int i)
{
    return i;
    return i + 1;
}

The second return statement is not reachable, i.e. it is dead code. While dead code is poor style in released code, it can legitimately happen a lot when rapidly trying to isolate down a bug or experiment with different bits of code.

The warning can be resolved by:

  1. Commenting out the dead code with /+ ... +/ comments:
    int foo(int i)
    {
        return i;
        /+
    	return i + 1;
         +/
    }
    
  2. Putting the dead code inside a version(none) conditional:
    int foo(int i)
    {
        return i;
        version (none)
        {
    	return i + 1;
        }
    }
    
  3. Only compile with warnings enabled when doing release builds.




Forums | Comments |  D  | Search | Downloads | Home