September 25, 2018 Warn on unused imports? | ||||
---|---|---|---|---|
| ||||
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? |
September 25, 2018 Re: Warn on unused imports? | ||||
---|---|---|---|---|
| ||||
Posted in reply to FeepingCreature | 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:
>
> [...]
For instance, I've been thinking about hiding the warning behind an additional flag (-wunused-imports or -wu?) so that it's opt-in. Would that be an acceptable approach?
|
September 25, 2018 Re: Warn on unused imports? | ||||
---|---|---|---|---|
| ||||
Posted in reply to FeepingCreature | 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
|
September 25, 2018 Re: Warn on unused imports? | ||||
---|---|---|---|---|
| ||||
Posted in reply to FeepingCreature | 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:
Honestly, I hate these types of warnings/errors. It makes playing with and designing code such a chore. I hope this is opt-in.
|
September 25, 2018 Re: Warn on unused imports? | ||||
---|---|---|---|---|
| ||||
Posted in reply to FeepingCreature | 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 { } |
September 25, 2018 Re: Warn on unused imports? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | On Tuesday, 25 September 2018 at 13:14:36 UTC, Jonathan M Davis wrote:
> 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
If that's the way D wanted to go, it shouldn't have turned itself into a metaprogramming monster that's completely unevaluable by Linter tools, *or* it should offer some way to dump a fixed-format lowered representation with line number information that tools can analyze. As it stands, it is literally impossible to evaluate whether an import is unused without evaluating the entire compile-time subset of D.
|
September 25, 2018 Re: Warn on unused imports? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dominikus Dittes Scherkl | 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? |
September 25, 2018 SOLVED: I'll put it in -deps | ||||
---|---|---|---|---|
| ||||
Posted in reply to FeepingCreature | On Tuesday, 25 September 2018 at 14:28:48 UTC, FeepingCreature wrote:
> Maybe stick the info in -v or -deps?
Actually, -deps is the perfect place for it. It needs to recursively evaluate modules anyways, so it'll see an import even if it was only used from a template. And it doesn't spam up warnings, and the -deps info is made to be extensible anyways.
Great! I'll port it to there.
|
September 25, 2018 Re: Warn on unused imports? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | 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.
|
September 25, 2018 Re: Warn on unused imports? | ||||
---|---|---|---|---|
| ||||
Posted in reply to FeepingCreature | On 2018-09-25 16:20, FeepingCreature wrote: > If that's the way D wanted to go, it shouldn't have turned itself into a metaprogramming monster that's completely unevaluable by Linter tools, *or* it should offer some way to dump a fixed-format lowered representation with line number information that tools can analyze. As it stands, it is literally impossible to evaluate whether an import is unused without evaluating the entire compile-time subset of D. The DMD compiler is available as a library. A linter tool can be based on that. -- /Jacob Carlborg |
Copyright © 1999-2021 by the D Language Foundation