Skip to content

Add unused_import config option to require imports for each module used - #3117

Closed
jpsim wants to merge 3 commits into
masterfrom
add-unused_import-config-option-to-require-imports-for-each-module-used
Closed

jpsim wants to merge 3 commits into
masterfrom
add-unused_import-config-option-to-require-imports-for-each-module-used

Conversation

@jpsim

@jpsim jpsim commented Feb 16, 2020

Copy link
Copy Markdown
Collaborator

This is a proof of concept, see TODOs for what would need to be cleaned up

For example, if CGFloat is used in a file where only UIKit is imported but not CoreGraphics, this will be a violation even if the file previously compiled.

This is because Swift allows referencing some declarations that are only transitively imported.

Enabling the require_explicit_imports configuration option will require that the module of every declaration referenced in a source file be explicitly imported.

This will add significant noise to the imports list, but has a few advantages:

  1. It will be easier to understand all the dependencies explicitly referenced in a source file.
  2. Correcting the unused_import rule will no longer introduce compilation errors in files that compiled prior to the correction.

Missing imports will always be added to the top of the file, which may cause sorted_imports violations. You may run SwiftLint autocorrect with the sorted_imports rule enabled to re-sort those import statements afterwards.

For example, if `CGFloat` is used in a file where only `UIKit` is
imported but not `CoreGraphics`, this will be a violation even if the
file previously compiled.

This is because Swift allows referencing some declarations that are only
transitively imported.

Enabling the `require_explicit_imports` configuration option will
require that the module of every declaration referenced in a source file
be explicitly imported.

This will add significant noise to the imports list, but has a few
advantages:

1. It will be easier to understand all the dependencies explicitly
   referenced in a source file.
2. Correcting the `unused_import` rule will no longer introduce
   compilation errors in files that compiled prior to the correction.

Missing imports will always be added to the top of the file, which may
cause `sorted_imports` violations. You may run SwiftLint autocorrect
with the `sorted_imports` rule enabled to re-sort those import
statements afterwards.
@SwiftLintBot

SwiftLintBot commented Feb 16, 2020 •

Copy link
Copy Markdown
2 Warnings
⚠️ Please include a CHANGELOG entry to credit yourself!
You can find it at CHANGELOG.md.
⚠️ This PR may need tests.
12 Messages
📖 Linting Aerial with this PR took 1.26s vs 1.24s on master (1% slower)
📖 Linting Alamofire with this PR took 2.16s vs 2.12s on master (1% slower)
📖 Linting Firefox with this PR took 8.95s vs 8.86s on master (1% slower)
📖 Linting Kickstarter with this PR took 13.77s vs 13.86s on master (0% faster)
📖 Linting Moya with this PR took 1.34s vs 1.33s on master (0% slower)
📖 Linting Nimble with this PR took 1.35s vs 1.35s on master (0% slower)
📖 Linting Quick with this PR took 0.53s vs 0.53s on master (0% slower)
📖 Linting Realm with this PR took 2.31s vs 2.35s on master (1% faster)
📖 Linting SourceKitten with this PR took 1.0s vs 1.01s on master (0% faster)
📖 Linting Sourcery with this PR took 6.64s vs 6.61s on master (0% slower)
📖 Linting Swift with this PR took 12.21s vs 12.22s on master (0% faster)
📖 Linting WordPress with this PR took 14.87s vs 14.88s on master (0% faster)

Here's an example of your CHANGELOG entry:

* Add unused_import config option to require imports for each module used.  
  [jpsim](https://github.com/jpsim)
  [#issue_number](https://github.com/realm/SwiftLint/issues/issue_number)

note: There are two invisible spaces after the entry's text.

Generated by 🚫 Danger

@jpsim

jpsim commented Feb 18, 2020

Copy link
Copy Markdown
Collaborator Author

Continuing in #3123.

@jpsim jpsim closed this Feb 18, 2020
@jpsim
jpsim deleted the add-unused_import-config-option-to-require-imports-for-each-module-used branch February 18, 2020 21:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants