Issue 24778 - Warning for unused imports
Summary: Warning for unused imports
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2024-09-22 15:00 UTC by Manu
Modified: 2024-09-24 13:12 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Manu 2024-09-22 15:00:48 UTC
This has come up before... but I seriously, desperately want this.
Detecting unused imports is most important to improve build times.
I periodically try and cull old imports to improve build times, but it's difficult to do, and just a massive waste of time!

I know people don't like warnings; but this is exactly what warnings should be for.
Comment 1 Manu 2024-09-22 15:02:56 UTC
I don't know any other language that can't do this, and people expect it to be available.

We've had this in C# for over 20 years, and Java for much longer... we need this. It's an embarrassing omission.
Comment 2 Lance Bachmeier 2024-09-22 15:22:43 UTC
(In reply to Manu from comment #0)
> This has come up before... but I seriously, desperately want this.
> Detecting unused imports is most important to improve build times.
> I periodically try and cull old imports to improve build times, but it's
> difficult to do, and just a massive waste of time!
> 
> I know people don't like warnings; but this is exactly what warnings should
> be for.

Atila started a related thread a couple months ago: https://forum.dlang.org/post/uosaqeqcwvdbzuafjehh@forum.dlang.org

There was a tool posted in that thread that identifies unused global imports: https://forum.dlang.org/post/xlggsjttounitndnecgw@forum.dlang.org
Comment 3 ryuukk_ 2024-09-23 05:10:14 UTC
It should be builtin into the compiler

People should learn to submit PRs to DMD

In odin it is as simple as passing:

-vet-unused-imports
        Checks for unused import declarations.



Nobody wants to download external programs for such a basic, yet important, feature
Comment 4 Lance Bachmeier 2024-09-23 18:45:17 UTC
(In reply to ryuukk_ from comment #3)
> It should be builtin into the compiler
> 
> People should learn to submit PRs to DMD
> 
> In odin it is as simple as passing:
> 
> -vet-unused-imports
>         Checks for unused import declarations.
> 
> 
> 
> Nobody wants to download external programs for such a basic, yet important,
> feature

It's Razvan's script, so the problem is not lack of understanding of how to submit PRs. His post notes cases that will be hard to handle.
Comment 5 RazvanN 2024-09-24 06:50:58 UTC
(In reply to Lance Bachmeier from comment #4)
> (In reply to ryuukk_ from comment #3)
> > It should be builtin into the compiler
> > 
> > People should learn to submit PRs to DMD
> > 
> > In odin it is as simple as passing:
> > 
> > -vet-unused-imports
> >         Checks for unused import declarations.
> > 
> > 
> > 
> > Nobody wants to download external programs for such a basic, yet important,
> > feature
> 
> It's Razvan's script, so the problem is not lack of understanding of how to
> submit PRs. His post notes cases that will be hard to handle.

I was trying to implement it using dmd as a library to see if it's possible, and if not to understand what changes are required to make it possible. Implementing this inside the compiler would actually be easier, provided that Walter does not oppose it.
Comment 6 RazvanN 2024-09-24 12:23:04 UTC
Here's the dmd implementation: https://github.com/dlang/dmd/pull/16878
Comment 7 Dlang Bot 2024-09-24 12:30:19 UTC
@RazvanN7 updated dlang/dmd pull request #16878 "Implement warnings for unused imports" mentioning this issue:

- Add changelog entry + fix importc error + Fix Bugzilla Issue 24778

https://github.com/dlang/dmd/pull/16878
Comment 8 basile-z 2024-09-24 12:57:29 UTC
RazvanN: I dont like how it's implemented.

I remember I already suggested that in a forum discussion but, let's do it again:

If it's implemented in the compiler it would be better to implements this as a "touched" system. You see when searching a symbol has worked and via an import, you flag that import as "touched".

Then when the user asks for unused import you visit the tree and when it's an import node and that import has not been touched you emit the warning.
Comment 9 RazvanN 2024-09-24 13:01:37 UTC
(In reply to basile-z from comment #8)
> RazvanN: I dont like how it's implemented.
> 
> I remember I already suggested that in a forum discussion but, let's do it
> again:
> 
> If it's implemented in the compiler it would be better to implements this as
> a "touched" system. You see when searching a symbol has worked and via an
> import, you flag that import as "touched".
> 
> Then when the user asks for unused import you visit the tree and when it's
> an import node and that import has not been touched you emit the warning.

I am fully aware how to implement it. In fact you can see that my implementation does the same thing, however, in dmdlib you do not have all the information that the compiler does (yet, I hope).
Comment 10 basile-z 2024-09-24 13:12:25 UTC
Sorry I was looking for a `search` overrides modified in dmodule.d but now I remember that you have removed it a few months ago.