Bug 10437 – Warnings for unused private imports

Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-06-21T05:46:07Z
Last change time
2021-05-11T09:25:26Z
Assigned to
No Owner
Creator
qznc

Comments

Comment #0 by qznc — 2013-06-21T05:46:07Z
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/[email protected] This enhancement helps to keep a code base clean. Less imports might reduce compile times.
Comment #1 by k.hara.pg — 2013-06-21T06:14:08Z
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 by andrej.mitrovich — 2013-06-21T06:16:52Z
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 by qznc — 2013-06-21T06:30:37Z
(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 by k.hara.pg — 2013-06-21T06:52:57Z
(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 by razvan.nitu1305 — 2021-05-11T09:25:26Z
Walter is strongly against warnings. The functionality that is being asked for could be implemented by a third party tool. Closing as WONTFIX.