Warn on unused imports?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Wed Sep 26 01:13:11 UTC 2018


On Tuesday, September 25, 2018 1:21:50 PM MDT Nick Sabalausky (Abscissa) via 
Digitalmars-d wrote:
> 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.

The way that C++ handles warnings is how I've seen most languages handle
warnings. IMHO, the only time that anything along the lines of a warning
makes sense is when the programmer is proactively running a tool to
specifically ask to be informed of a potential type of problem where they
will then go look at each of them individually and decide whether what the
tool is telling them is valid or not - at which point, some of what the tool
says will be followed, and some if it will be ignored. It's not something
that should be run as part of a normal build process. If it is, then
inevitably what happens is that either all of the warnings get "fixed" (at
which point, they might as well have all been errors), or they all get
ignored, meaning that you get a huge wall of them, and they're completely
useless. As such, I really have nothing good to say about having any kind of
warnings being built into the compiler. As I understand it, on the whole,
Walter agrees with me and that he only added them in to dmd, because he was
essentially bullied into it, and I wish that he'd never given in. And when
you consider features like is(typeof(...)), the side effects of having -w
are particularly bad.

- Jonathan M Davis





More information about the Digitalmars-d mailing list