Unused import tool

Basile B. b2.temp at gmx.com
Wed Aug 16 17:17:22 UTC 2023


On Wednesday, 16 August 2023 at 05:26:11 UTC, Basile B. wrote:
> On Tuesday, 15 August 2023 at 08:13:04 UTC, RazvanN wrote:
>> Hello everyone,
>>
>> I have started working on [a tool that identifies unused 
>> imports](https://github.com/RazvanN7/Unused-Import/tree/master). It uses dmd as a library.
>>
>> The purpose is to have a working tool, but also to better 
>> understand what sort of information the frontend needs to 
>> output so that such tools are possible.
>>
>> Up to this point, I managed to make it work with global 
>> imports (since dmdfe currently does not provide the scope 
>> hierarchy).
>
> You can handle nested import declarations without the scope. 
> The trick would be to use a single visitor and a 2D import 
> list, i.e`ImportInfo[][] imports;`. The first dim matches to a 
> scope, so when you enter a scope (BlockStmt, AggrDecl, etc) you 
> push the local imports, and when you leave the scope, you check 
> and pop. Styx 
> [Example](https://gitlab.com/styx-lang/styx/-/blob/master/src/styx/lint.sx?ref_type=heads#L247), (which has same semantics as D about out-of-order decls).

To be clear the styx version had the same problem as those you 
mention, until [that 
commit](https://gitlab.com/styx-lang/styx/-/commit/b890f94f78e05c244be67a743e8d8d013a018048). The flag mentioned previously, and that is set [here](https://gitlab.com/styx-lang/styx/-/commit/b890f94f78e05c244be67a743e8d8d013a018048#bd524f0aa3bc570bcb844ae4f453b2f2a32db4ae_354_355) (implementation of search is a bit different...), did not cause any slowdown.

To conclude, I dont see how such a tool could work correctly 
without being integrated directly in the compiler.


More information about the Digitalmars-d mailing list