Skip to main content

Module hoist

Module hoist 

Source
Expand description

Hoist absolute paths in generated code into module-scope use items.

The emitters name an item from another module by its full path, such as crate::primitives::array::put_array_len or crate::owned::fetch_request::PartitionData. That keeps each emitter local: it never has to know which names the rest of the file imports. The workspace denies clippy::absolute_paths beyond two segments, though, so the committed code must name such an item through an import.

hoist_absolute_paths closes that gap on the parsed file. It finds every path that starts at a crate root and names an item through three or more segments, as the lint counts them, picks the deepest segment that it can import without a name clash, adds one use item per imported path, and shortens each path to start at the imported name. A name that two different paths would import, or that the file already binds, falls back to its parent module, so crate::owned::fetch_request::PartitionData in a file that defines its own PartitionData becomes fetch_request::PartitionData.

A generated file is include!d into its wrapper module, so its module-scope use items are that module’s imports.

Functions§

hoist_absolute_paths
Shorten every crate-rooted path in file that names an item through three or more segments to start at an imported name, and add the use items that import those names.