Warn on unused imports?

FeepingCreature feepingcreature at gmail.com
Tue Sep 25 14:28:48 UTC 2018


On Tuesday, 25 September 2018 at 14:15:32 UTC, Dominikus Dittes 
Scherkl wrote:
> On Tuesday, 25 September 2018 at 13:03:30 UTC, FeepingCreature 
> wrote:
>> I'm playing with a branch of DMD that would warn on unused 
>> imports:
>>
>> https://github.com/FeepingCreature/dmd/tree/feature/Issue-3507-warn-on-unused-imports
>>
>> Two problems have arisen.
>>
>> First:
>>
>> import std.stdio;
>>
>> void foo(T)() { writeln("Hello World"); }
>>
>> foo.d: Warning: unused import
>>
>> To be fair, it's not *wrong*: if you remove the import, the 
>> module itself compiles just fine. In any case, it's trivial to 
>> instead move the import into the template.
>>
>> The real problem is this:
>>
>> import std.format;
>>
>> class TestException(T) : FormatException { }
>>
>> Now I can't move the import inside the template, because it's 
>> needed at the point of instantiation, but not inside the 
>> template scope *per se*.
>>
>> I could require the class to be written as
>>
>> template TestException(T) {
>>   import std.format;
>>   class TestException : FormatException { }
>> }
>>
>> but that's kind of terrible.
>>
>> I've been working around this for now, with import std.format 
>> : FormatException, but I'm not really happy with it.
>>
>> Any ideas?
>
> Doesn't the "from" idiom work?
> I'm not sure if it is allowed at the template declaration
>
> template from(string moduleName)
> {
>   mixin("import from = " ~ moduleName ~ ";");
> }
>
> class TestException(T) from!"std.format".FormatException
>  : FormatException
> {
> }

class TestException(T) : from!"std.format".FormatException?

That should work, but it's kind of a big step. In any case, I'll 
never get a weird hacky template like that through code review :)

Might as well make import an expression - class TestException(T) 
: (import std.format).FormatException. In any case, it's probably 
not viable to solve the problem that a warning has a false 
positive by introducing a workaround in the language - imo, 
that'd rather mean the warning just isn't viable.

(It's so useful, though!)

Maybe stick the info in -v or -deps?


More information about the Digitalmars-d mailing list