Thread overview
[Issue 10437] New: Warnings for unused private imports
Jun 21, 2013
qznc@web.de
Jun 21, 2013
Kenji Hara
Jun 21, 2013
Andrej Mitrovic
Jun 21, 2013
qznc@web.de
Jun 21, 2013
Kenji Hara
June 21, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10437

           Summary: Warnings for unused private imports
           Product: D
           Version: future
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: qznc@web.de


--- Comment #0 from qznc@web.de 2013-06-21 05:46:07 PDT ---
The compiler should output warnings, if the source code contains imports, which are not used and can be removed.

Example: For debugging the following private imports were used, but now the module does not contain any writeln or text call anymore, but parse is used.

private import std.stdio;
private import std.conv: text, parse;

The compiler should print warnings like:

foo.d(3): Warning: unused private import std.stdio; should be deleted
foo.d(4): Warning: unused private import std.conv: text; should be deleted

This has been discussed together with other unused stuff, but there seems to be no conclusion in the thread.

http://forum.dlang.org/thread/i4luk9$2rdg$1@digitalmars.com

This enhancement helps to keep a code base clean. Less imports might reduce compile times.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 21, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10437



--- Comment #1 from Kenji Hara <k.hara.pg@gmail.com> 2013-06-21 06:14:08 PDT ---
What will occur with template function?

import std.conv : to;

T convert(T, S)(S src)
{
    return to!T(src);
}

void main() {}
// template function convert is not instantiated.

In this case, imported std.conv is unused then compiler might warn it. Is this right?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 21, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10437


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich@gmail.com


--- Comment #2 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-06-21 06:16:52 PDT ---
This warning should at best be a separate switch (or we should add customization to the -w switch like we have with -transition). I dislike chatty compilers which complain about every single little nuisance.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 21, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10437



--- Comment #3 from qznc@web.de 2013-06-21 06:30:37 PDT ---
(In reply to comment #1)
> What will occur with template function?
> 
> import std.conv : to;
> 
> T convert(T, S)(S src)
> {
>     return to!T(src);
> }
> 
> void main() {}
> // template function convert is not instantiated.
> 
> In this case, imported std.conv is unused then compiler might warn it. Is this right?

I think the question is misleading. The actual question is how clever the analysis is. Call-graph information provides a similar scenario:

import std.stdio;

void foo() {
    writeln("foo");
}

void main () {}
// foo is never called

In such scenarios, deleting a statement might declare a whole chain (actually DAG) of (template or normal) functions unused. It is a question of taste, if the user should be flooded with warnings in this case.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 21, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10437



--- Comment #4 from Kenji Hara <k.hara.pg@gmail.com> 2013-06-21 06:52:57 PDT ---
(In reply to comment #3)
> I think the question is misleading. The actual question is how clever the analysis is. Call-graph information provides a similar scenario:
> 
> import std.stdio;
> 
> void foo() {
>     writeln("foo");
> }
> 
> void main () {}
> // foo is never called
> 
> In such scenarios, deleting a statement might declare a whole chain (actually DAG) of (template or normal) functions unused. It is a question of taste, if the user should be flooded with warnings in this case.

It's impossible to do that strictly with templates. Let's try to show more suitable case.

import std.conv;
import std.stdio;

auto call(string name, A...)(A args)
{
    return mixin(name~"(args)");
}

Until 'call' template function is instantiated, compiler cannot determine which import declaration is actually unused.

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