SwiftLint: Exclude file for specific rule


Swiftlint Problem Overview

I'm trying to do something like this in my .swiftlint.yml file:

  severity: warning # explicitly
    - Dog.swift

I have this code and I don't like the force_try warning I'm getting for it:

let cell = tableView.dequeueReusableCellWithIdentifier(Constants.dogViewCellReuseIdentifier,
                                                               forIndexPath: indexPath) as! DogViewCell

I want to suppress the warning for this file by excluding this file from the rule.

Is there a way to do that ?

Swiftlint Solutions

Solution 1 - Swiftlint

Well, if you don't want some specific rules to be applied to a specific file you can use the technique mentioned by @Benno Kress. For that you need to add a comment in your swift file as given below.

The rules will be disabled until the end of the file or until the linter sees a matching enable comment:

// swiftlint:disable <rule1> 

   YOUR CODE WHERE NO rule1 is applied

// swiftlint:enable <rule1>

It is also possible to skip some files by configuring swiftlint. add a ".swiftlint.yml" file in the directory where you'll run SwiftLint.

Add the following content to exclude some files. Lets say file1, file2 ... etc

  - file1
  - file2
  - folder1
  - folder1/ExcludedFile.swift

To disable some rules completely add the following to the same ".swiftlint.yml" file.

disabled_rules: # rule identifiers to exclude from running
  - colon
  - comma
  - control_statement

for more information, refer the following links.



Solution 2 - Swiftlint

You can write // swiftlint:disable force_cast at the beginning of the file in which you want to supress the warning for this rule. It gets disabled until the end of the file or until you add the line // swiftlint:enable force_cast.

Source: [https://github.com/realm/SwiftLint#disable-rules-in-code][1]

[1]: https://github.com/realm/SwiftLint#disable-rules-in-code "Github realm/SwiftLint"

Solution 3 - Swiftlint

I just get rid with force_cast

Step 1:

cd path-to-your-project

Step 2:

touch .swiftlint.yml

Step 3: open .swiftlint.yml and add the rule

disabled_rules: # rule identifiers to exclude from running
 - force_cast

enter image description here

Reference - https://github.com/realm/SwiftLint#disable-rules-in-code

Solution 4 - Swiftlint

Configure SwiftLint by adding a .swiftlint.yml file from the directory you'll run SwiftLint from. Here is the complete set of options you can use in your .swiftlint.yml file

disabled_rules: # rule identifiers to exclude from running
  - colon
  - comma
  - control_statement
opt_in_rules: # some rules are only opt-in
  - empty_count
  # Find all the available rules by running:
  # swiftlint rules
included: # paths to include during linting. `--path` is ignored if present.
  - Source
excluded: # paths to ignore during linting. Takes precedence over `included`.
  - Carthage
  - Pods
  - Source/ExcludedFolder
  - Source/ExcludedFile.swift
  - Source/*/ExcludedFile.swift # Exclude files with a wildcard
analyzer_rules: # Rules run by `swiftlint analyze` (experimental)
  - explicit_self

# configurable rules can be customized from this configuration file
# binary rules can set their severity level
force_cast: warning # implicitly
  severity: warning # explicitly
# rules that have both warning and error levels, can set just the warning level
# implicitly
line_length: 110
# they can set both implicitly with an array
  - 300 # warning
  - 400 # error
# or they can set both explicitly
  warning: 500
  error: 1200
# naming rules can set warnings/errors for min_length and max_length
# additionally they can set excluded names
  min_length: 4 # only warning
  max_length: # warning and error
    warning: 40
    error: 50
  excluded: iPhone # excluded via string
  min_length: # only min_length
    error: 4 # only error
  excluded: # excluded via string array
    - id
    - URL
    - GlobalAPIKey
reporter: "xcode" # reporter type (xcode, json, csv, checkstyle, junit, html, emoji, sonarqube, markdown)

Reference: github.com/realm/SwiftLint#disable-rules-in-code

Solution 5 - Swiftlint

You can add a new .swiftlint.yml file to the excluded folder and override rules there: project_root/.swiftlint.yml:

.. and other rules

and in


  - force_unwrapping

Then force_unwrapping will not be applied to your_excluded_folder and all subfolders

Config file from subfolder will "override" rules in the root config file.

This is useful for example for the unit tests folder

Solution 6 - Swiftlint

Maybe this is a better approach:

guard let cell = tableView.dequeueReusableCellWithIdentifier(Constants.dogViewCellReuseIdentifier,
                                                               forIndexPath: indexPath) as? DogViewCell else {
return UITableviewCell()


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionetayluzView Question on Stackoverflow
Solution 1 - Swiftlintarango_86View Answer on Stackoverflow
Solution 2 - SwiftlintBenno KressView Answer on Stackoverflow
Solution 3 - SwiftlintJackView Answer on Stackoverflow
Solution 4 - SwiftlintSazzad Hissain KhanView Answer on Stackoverflow
Solution 5 - SwiftlintVitalii GozhenkoView Answer on Stackoverflow
Solution 6 - SwiftlintOvidiuView Answer on Stackoverflow