Warn on unused imports?

Nick Sabalausky (Abscissa) SeeWebsiteToContactMe at semitwist.com
Tue Sep 25 19:21:50 UTC 2018


On 09/25/2018 09:14 AM, Jonathan M Davis wrote:
> 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.
> 

Warnings ARE a lint tool. The only reason people have gotten the idea 
they're basically toggleable errors is because of the horrid mess that 
is C/C++, where it's common practice to permit things that no sane 
language would ever even CONSIDER not making a big, giant flashing 
sirens-blazing error (thus necessitating, at very least, a warning). 
Hell, even the actual lint tools in C/C++ land spit out tons of stuff 
that should be errors.

Summary: Warning are not bad. The C/C++ approach to warnings is bad, and 
had corrupted millions of programmer's minds.


More information about the Digitalmars-d mailing list