SwiftLint icon indicating copy to clipboard operation
SwiftLint copied to clipboard

Usage of `exclude` with force_unwrapping

Open kostasniks opened this issue 8 months ago • 6 comments

opt_in_rules:
  - force_unwrapping

force_unwrapping: 
  severity: error
  excluded:
    - ".*Tests\\.swift"

Seels like excluded is not working to my expectations. I want to exclude force_unwrapping from certain files that belong in the following folders pattern ⏬

📁Folders
Modules/PAM/Foo1/Tests
Modules/PAM/Foo2/Tests
Modules/PAM/Foo3/Tests
Modules/PAM/Foo4/Tests
Modules/PAM/Foo5/Tests
Modules/PAM/TwoFactorAuthentication/Tests
Modules/PAM/Wallet/Tests
Modules/PLATFORM/Analytics/Tests
Modules/PLATFORM/AppSecurity/Tests
Modules/PLATFORM/Core/Tests
Modules/PLATFORM/Debug/Tests
Modules/PLATFORM/Deeplinks/Tests
Modules/PLATFORM/DomainResolver/Tests
Modules/PLATFORM/ExecutableTask/Tests

I've come up with following questions

  1. Is excluded supported on non-custom rules?
  2. Is there a guide on what excluded supports and what not? If someone can point me to a source code to take a look (is it a regex? a glob pattern?)
  3. What would be the solution for this usecase, if exists 🤔

kostasniks avatar May 27 '25 10:05 kostasniks

I hope all your tests are located in one folder. Then you can just create a child swiftlint.yml in this folder with the rule disabled.

kapitoshka438 avatar May 27 '25 15:05 kapitoshka438

Ah I see you have many modules. But anyway, having nested configuration files seems to be a good practice.

kapitoshka438 avatar May 27 '25 15:05 kapitoshka438

@kapitoshka438 I guess it is a common practice each module to contain the tests, isn't it? I guess to understand better I will have to take a look at internal implementation as it seems that the excluded is not working as expected 🤔

kostasniks avatar May 28 '25 08:05 kostasniks

You also could try to have two configs in the root folder: a parent and a child. The parent disables the rule. The child enables in for everything but tests. (Read "Child/Parent Configs (Locally)" in the README)

kapitoshka438 avatar May 28 '25 09:05 kapitoshka438

Sure, I will take a look at this as plan_B as I am currently using swiftlint as Xcode build plugin so need to check if is supported there, anyways thank you @kapitoshka438 🙌

kostasniks avatar May 28 '25 14:05 kostasniks

The main issue is that excluded is not a property of rules but of the whole configuration. Thus, your configuration should actually look more like:

excluded:
  - ".*Tests\\.swift"

force_unwrapping: 
  severity: error

SimplyDanny avatar Jun 15 '25 19:06 SimplyDanny

This issue has been marked as stale because it has not seen any activity in the last 4 months. It is going to be closed soon if it stays inactive.

github-actions[bot] avatar Oct 15 '25 00:10 github-actions[bot]

This issue is being closed due to inactivity. Please feel free to reopen if you have more information.

github-actions[bot] avatar Nov 15 '25 09:11 github-actions[bot]