Against precise imports and unused imports warnings
I am frequently troubled by unused imports warnings. Often while developing I add temporary commits that add #![allow(dead_code)] // XXXX
to random other crates. (Most recently for the one fixed in 399d4c8d and another in tor-proto
.)
In general, it is a lot of effort to maintain the property that every element of every use
statement is properly decorated with precisely the right set of cfg
options. I think this is makework.
The only benefit of this approach is that we save very small amounts of compiler time: the compiler doesn't need to add the unused entries to its symbol tables.
Also, more generally, we are spending too much time editing (and sometimes fixing up conflicts in) imports lists. Maintaining precise import lists leads to churn as code develops.
Option 1. Preludes
My preferred solution is that we should have an internal prelude for (approximately) each crate. Every module in the crate would use crate::prelude::*
. The prelude would have an allow for unused imports.
The advantage would be that every module in a crate would have the same namespace (or, at least, a very similar one). There would be less use
overall. When elements did need to be conditional because they're only available in certain configurations, the cfg
stuff would appear once in each crate rather than once in each module.
The downside is that the automatic use
-adding suggestions would be wrong, since they would add the imports to the individual modules, not the prelude. In !622 I volunteered to do the necessary cleanup myself on an ad-hoc as-and-when basis.
I don't think this is a particularly novel approach (even in the Rust community, despite the lack of formal tooling support).
CC @nickm @gabi-250, and @ahf since this was very controversial last time and I think the discussion might benefit from some oversight.
allow
Option 2. If crate-specific preludes remain too controversial, I propose that we #![allow(unused_imports)]
everywhere.
We wouldn't want ever-growing import lists that just accumulated cruft, but we could periodically (a few times a year) run a cargo check --all-* --workspace
to identify unused imports and prune them.
allow
Option 3. Conditional If even that is too controversial, I suggest we use a cfg_attr(allow)
which disables the unused imports warnings unless all the crate's features are enabled.