Skip to content

Avoid Duplicated Exports

Description

Generally, we don't encourage the use of re-exports.

However, sometimes, to keep the interface exposed by a lib crate tidy, we use re-exports to shorten the path to specific items. When doing so, a pitfall is to export a single item under two different names.

Consider:

rs
pub mod foo;
pub use foo::Foo;

The issue with this code, is that Foo is now exposed under two different paths: Foo, foo::Foo.

This unnecessarily increases the surface of your API. It can also cause issues on the client side. For example, it makes the usage of auto-complete in the IDE more involved.

Instead, ensure you export only once with pub.

YAML

yaml
id: avoid-duplicate-export
language: rust
rule:
  all:
     - pattern: pub use $B::$C;
     - inside:
        kind: source_file
        has:
          pattern: pub mod $A;
     - has:
        pattern: $A
        stopBy: end

Example

rs
pub mod foo;
pub use foo::Foo;
pub use foo::A::B;


pub use aaa::A;
pub use woo::Woo;

Contributed by

Julius Lungys(voidpumpkin)

Made with ❤️ with Rust