Skip to content

Unused Imports

Periphery can only detect unused imports of targets it has scanned. It cannot detect unused imports of other targets because the Swift source files are unavailable and uses of @_exported cannot be observed.

@_exported is problematic because it changes the public interface of a target such that the declarations exported by the target are no longer necessarily declared by the imported target. For example, the Foundation target exports Dispatch, among other targets. If any given source file imports Foundation and references DispatchQueue but no other declarations from Foundation, then the Foundation import cannot be removed as it would also make the DispatchQueue type unavailable. To avoid false positives, Periphery only detects unused imports of targets it has scanned.

Periphery will likely produce false positives for targets with mixed Swift and Objective-C, as Periphery cannot scan the Objective-C files. For projects with a significant amount of Objective-C, it is recommended to disable unused import detection entirely with --disable-unused-import-analysis, or to exclude the mixed language targets from the results with --report-exclude.

To retain specific imports while leaving the analysis enabled, name the modules with --retain-unused-imported-modules "ModuleA" "ModuleB".