How to Disable SwiftLint Rules?

Originally Posted on medium.com/@milanpanchal24/how-to-disable-s..

How to Disable SwiftLint Rules?

Image for post

In my previous article, we have learned how to integrate SwiftLint into the Project. In this article, we’ll learn how to disable rules. Rules can be disabled either at the project level or source level.

Option 1: Disable Rules at Project Level:

If you don’t want to use the specific rule(s) for your project then you can disable those rules completely by configuring .swiftlint.yml file as below:

# rule identifiers turned on by default to exclude from running
disabled_rules:
   - trailing_whitespace
   - force_cast
   - force_unwrapping
   - force_try

It is also possible to skip some files by configuring .swiftlint.yml file as below:

excluded:
  - file1
  - file2
  - folder1
  - folder2/ExcludedFileFromSwiftLintRules.swift

Option 2: Disable Rules at Source Level:

Sometimes, there will be a case, where you want to disable rule(s) for a specific file or block of code.

To disabled rules inside a source file use the following format:

// swiftlint:disable <rule1> [<rule2> <rule3>...]

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

Example 1:

// swiftlint:disable colon
let noWarning :String = "" // No warning about colons immediately after variable names!
// swiftlint:enable colon

let hasWarning :String = "" // Warning generated about colons immediately after variable names

If you want to disable all the rules until the linter sees a matching enable comment: use all keyword as shown in Example 2:

Example 2:

// swiftlint:disable all
let noWarning: String = "" // No warning about colons immediately after variable names!
let i = "" // Also no warning about short identifier names
// swiftlint:enable all

let hasWarning: String = "" // Warning generated about colons immediately after variable names
let y = "" // Warning generated about short identifier names

It’s also possible to modify a disable or enable command by appending :previous, :this or :next for only applying the command to the previous, this (current) or next line respectively.

Example 3:

// swiftlint:disable:next force_cast
let noWarning = NSNumber() as! Int
let hasWarning = NSNumber() as! Int

let noWarning2 = NSNumber() as! Int // swiftlint:disable:this force_cast

let noWarning3 = NSNumber() as! Int
// swiftlint:disable:previous force_cast

Disabling rules turned out to be something of a mixed blessing. Lazy developers can use this feature to remove warnings for a non-compliant code. So it is vital to check if the rule is disabled due to the important reason during the code review process.

Questions?

Please feel free to comment below, if you have any questions.

If you like this article, feel free to share it with your friends and leave me a comment. Also, click on the 👍 button below to show how much you like the article.

Thanks for reading! 👨🏼‍💻 Stay tuned for my next article.

You can find me on:

Twitter | LinkedIn | GitHub | Medium | HackerRank | LeetCode | Stack Overflow

Did you find this article valuable?

Support Milan Panchal by becoming a sponsor. Any amount is appreciated!