Reputation: 16416
I'm trying to do something like this in my .swiftlint.yml file:
force_cast:
severity: warning # explicitly
excluded:
- 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 ?
Upvotes: 67
Views: 74192
Reputation: 151
Configure Excluded Files/Folders:
In the .swiftlint.yml file, you can use the excluded parameter to specify the files or folders that you want to exclude from SwiftLint linting. Here's an example of how to exclude specific files and folders:
yaml
excluded:
- Carthage
- Pods
- SomeFolder/ExcludedFile.swift
- AnotherFolder/ExcludedFolder/**
Configure Excluded Files/Folders For specific Rule:
Start of file you can disable any particular rule
// swiftlint:disable type_name (Rule)
End of the file you can enable the swift lint
// swiftlint:enable type_name (Rule)
Upvotes: 1
Reputation: 9354
You can add a new .swiftlint.yml
file to the excluded folder and override rules there:
project_root/.swiftlint.yml
:
opt_in_rules:
force_unwrapping
.. and other rules
and in
project_root/your_excluded_folder/.swiftlint.yml
disabled_rules:
- 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
Upvotes: 0
Reputation: 64
Maybe this is a better approach:
guard let cell = tableView.dequeueReusableCellWithIdentifier(Constants.dogViewCellReuseIdentifier,
forIndexPath: indexPath) as? DogViewCell else {
return UITableviewCell()
}
Upvotes: -1
Reputation: 14329
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
Reference - https://github.com/realm/SwiftLint#disable-rules-in-code
Upvotes: 14
Reputation: 40146
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
force_try:
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
type_body_length:
- 300 # warning
- 400 # error
# or they can set both explicitly
file_length:
warning: 500
error: 1200
# naming rules can set warnings/errors for min_length and max_length
# additionally they can set excluded names
type_name:
min_length: 4 # only warning
max_length: # warning and error
warning: 40
error: 50
excluded: iPhone # excluded via string
identifier_name:
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
Upvotes: 4
Reputation: 4425
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
excluded:
- 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.
https://swifting.io/blog/2016/03/29/11-swiftlint/
https://github.com/realm/SwiftLint#disable-rules-in-code
Upvotes: 115
Reputation: 2809
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
Upvotes: 29