[Issue 7989] New: isInputRange and isForwardRange declare unused variables

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Apr 26 07:01:17 PDT 2012


http://d.puremagic.com/issues/show_bug.cgi?id=7989

           Summary: isInputRange and isForwardRange declare unused
                    variables
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody at puremagic.com
        ReportedBy: smjg at iname.com
            Blocks: 3960


--- Comment #0 from Stewart Gordon <smjg at iname.com> 2012-04-26 07:02:21 PDT ---
http://dlang.org/function.html
"It is an error to declare a local variable that is never referred to. Dead
variables, like anachronistic dead code, are just a source of confusion for
maintenance programmers."

However, as Timon Gehr has pointed out on digitalmars.D.learn,
std.range.isInputRange breaks this rule, and I've discovered that
isForwardRange does too.

template isInputRange(R)
{
    enum bool isInputRange = is(typeof(
    {
        R r = void;       // can define a range object
        if (r.empty) {}   // can test for empty
        r.popFront();     // can invoke popFront()
        auto h = r.front; // can get the front of the range
    }));
}

template isForwardRange(R)
{
    enum bool isForwardRange = isInputRange!R && is(typeof(
    {
        R r1 = void;
        R r2 = r1.save; // can call "save" against a range object
    }));
}

h and r2 are unused variables.  These templates only work at the moment because
of issue 3960.  Fixing that issue would at the moment cause these templates to
always evaluate to false, since the declarations of h and r2 are illegal
because the variables are never referred to.

Possible solutions:

-        auto h = r.front; // can get the front of the range
+        cast(void) r.front; // can get the front of the range

-        R r2 = r1.save; // can call "save" against a range object
+        static assert(is(typeof(r1.save) : R));

If issue 3960 is dealt with by removing the statement from the spec instead,
there is still some support for a warning about it, so it would be still worth
dealing with this in order to avoid such a warning.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list