Warn on unused imports?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Tue Sep 25 13:14:36 UTC 2018


On Tuesday, September 25, 2018 7:03:30 AM MDT FeepingCreature via 
Digitalmars-d 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-unu
> sed-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?

Honestly, in general, warnings are a terrible idea. Anything that's a
warning in your code has to be fixed, because it's bad practice to leave
warnings in your code, meaning that ultimately, there's not much difference
between a warning and an error. To make matters worse, there's a compiler
flag that turns warnings into errors. And when you combine that with stuff
like is(typeof(...)) and template constraints, whether you use that compiler
flag or not could actually change the resulting program. So, as it stands,
warnings are an even worse idea in D than they are in other languages.
Walter likes to talk about how warnings in C/C++ are there simply because
folks couldn't agree on what should or shouldn't be an error in the
language.

If something is definitively wrong, then it should be an error. If it's not
definitively wrong, then the compiler shouldn't say anything about it, and
it should be left up to a linter tool of some kind like dcd.

- Jonathan M Davis





More information about the Digitalmars-d mailing list