D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 10437 - Warnings for unused private imports
Summary: Warnings for unused private imports
Status: RESOLVED WONTFIX
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-06-21 05:46 UTC by qznc
Modified: 2021-05-11 09:25 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description qznc 2013-06-21 05:46:07 UTC
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.
Comment 1 Kenji Hara 2013-06-21 06:14:08 UTC
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?
Comment 2 Andrej Mitrovic 2013-06-21 06:16:52 UTC
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.
Comment 3 qznc 2013-06-21 06:30:37 UTC
(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.
Comment 4 Kenji Hara 2013-06-21 06:52:57 UTC
(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.
Comment 5 RazvanN 2021-05-11 09:25:26 UTC
Walter is strongly against warnings. The functionality that is being asked for could be implemented by a third party tool. Closing as WONTFIX.