Skip to main content

Module isolation

Module isolation 

Source
Expand description

Rust unit-isolation lint (#44): an inline #[cfg(test)] mod may call only into the unit under test — its parent module, reached via super::. A call out of the test’s own module — into another first-party module (crate::…), an external crate, or effectful std — is a violation. Inject a trait double (hand-rolled or mockall) instead; the compiler checks the double.

Detection is AST-based: each *.rs file under the crate root is parsed with syn and its #[cfg(test)] modules are walked with a Visitor. This is the deterministic syn heuristic; full name-resolution precision is a future dylint pass. The design and its precision limits live in internals/rust/isolation.md.

Implemented detectors:

  • no-out-of-module-call (D1): a call expression A::…::f(…) inside a #[cfg(test)] module whose leading segment A reaches out of the module — crate:: (first-party, another module), super::super::… (an ancestor), an external crate from Cargo.toml, or effectful std. A single super::, self/Self, a bare/unqualified call, and pure std (incl. io::Cursor) stay in-module and are not flagged.
  • no-out-of-module-import (D2): a use inside a #[cfg(test)] module that brings in a foreign surface — a glob of anything but super::*, or a named import rooted at crate::, an external crate, or effectful std. use super::* / use super::Thing (the unit under test), self, and pure std (e.g. collections, io::Cursor) are in-module. Catches a collaborator imported then called unqualified, which D1’s call check can’t see.

Re-exports§

pub use crate::violation::Violation;

Enums§

Language
A language whose unit-isolation convention can be checked (Python #42 is a separate detector). Each detector lives in its own module; this enum is the shared unit isolation language selector.

Functions§

find_violations
Scan the Rust source files under root and return every isolation violation, sorted by (file, line) for deterministic output.