Skip to content

Commit

Permalink
Enable unused import detection by default
Browse files Browse the repository at this point in the history
  • Loading branch information
ileitch committed May 19, 2024
1 parent 1a727b1 commit 4505cb0
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 6 deletions.
1 change: 0 additions & 1 deletion .periphery.linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ targets:
- PeripheryTests
- SPMTests
- AccessibilityTests
enable_unused_import_analysis: true
1 change: 0 additions & 1 deletion .periphery.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ targets:
- XcodeTests
- SPMTests
- AccessibilityTests
enable_unused_import_analysis: true
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

##### Enhancements

- None.
- Unused import detection is now enabled by default.

##### Bug Fixes

Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
- [Enumerations](#enumerations)
- [Assign-only Properties](#assign-only-properties)
- [Redundant Public Accessibility](#redundant-public-accessibility)
- [Unused Imports](#unused-imports)
- [Objective-C](#objective-c)
- [Encodable](#encodable)
- [XCTestCase](#xctestcase)
Expand Down Expand Up @@ -301,6 +302,12 @@ Declarations that are marked `public` yet are not referenced from outside their

This analysis can be disabled with `--disable-redundant-public-analysis`.

### Unused Imports

Periphery can detect unused imports of targets it has scanned, i.e. those specified with the `--targets` argument. 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, therefore, 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. It is recommended therefore to disable unused import detection for projects with a significant amount of Objective-C, or manually exclude the mixed language targets from the results.

### Objective-C

Periphery cannot analyze Objective-C code since types may be dynamically typed.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Frontend/Commands/ScanCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ struct ScanCommand: FrontendCommand {
@Flag(help: "Disable identification of redundant public accessibility")
var disableRedundantPublicAnalysis: Bool = defaultConfiguration.$disableRedundantPublicAnalysis.defaultValue

@Flag(help: "Enable identification of unused imports (experimental)")
@Flag(help: "Enable identification of unused imports")
var enableUnusedImportAnalysis: Bool = defaultConfiguration.$enableUnusedImportsAnalysis.defaultValue

@Flag(help: "Retain properties that are assigned, but never used")
Expand Down
4 changes: 2 additions & 2 deletions Sources/Shared/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public final class Configuration {
@Setting(key: "disable_redundant_public_analysis", defaultValue: false)
public var disableRedundantPublicAnalysis: Bool

@Setting(key: "enable_unused_import_analysis", defaultValue: false)
@Setting(key: "enable_unused_import_analysis", defaultValue: true)
public var enableUnusedImportsAnalysis: Bool

@Setting(key: "retain_codable_properties", defaultValue: false)
Expand Down Expand Up @@ -163,7 +163,7 @@ public final class Configuration {
if $retainObjcAnnotated.hasNonDefaultValue {
config[$retainObjcAnnotated.key] = retainObjcAnnotated
}

if $retainPublic.hasNonDefaultValue {
config[$retainPublic.key] = retainPublic
}
Expand Down

0 comments on commit 4505cb0

Please sign in to comment.