Unused import tool

cc cc at nevernet.com
Tue Aug 15 19:51:48 UTC 2023


On Tuesday, 15 August 2023 at 08:13:04 UTC, RazvanN wrote:
> 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.

Not sure how relevant this is to your methods, but something I 
used to stumble into occasionally:

```d
import std.format;
import std.range.primitives;
struct S {
	void toString(W)(ref W writer) if (isOutputRange!(W, char)) {
		writer.formattedWrite("ABC");
	}
}
void main() {
	S().writeln;
}
```
Output: `ABC`


```d
import std.format;
//import std.range.primitives;
struct S {
	void toString(W)(ref W writer) if (isOutputRange!(W, char)) {
		writer.formattedWrite("ABC");
	}
}
void main() {
	S().writeln;
}
```
Output: `S()`

The latter still compiles successfully and does not issue an 
error for the undefined template `isOutputRange`.  This is an 
issue with std.format.internal's `hasToString` I believe, since, 
because there is no toString that would successfully pass 
`__traits(compiles)` (due to the undefined symbol), it never 
actually attempts to *call* the toString, therefore the template 
isn't compiled at all and thus no bark for the problem. But worth 
mentioning IMO as an example of a process that fails to identify 
undefined symbols when an import is missing.



More information about the Digitalmars-d mailing list