SwiftLint: exclude file for specific rule

I am 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 get 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 any way to do this?

+21
swiftlint
source share
4 answers

Well, if you don’t want specific rules to apply to a particular file, you can use the technique mentioned by @Benno Kress. To do this, you need to add a comment to your swift file as follows.

Rules will be disabled until the end of the file or until the linter sees the corresponding inclusion comment:

 // swiftlint:disable <rule1> YOUR CODE WHERE NO rule1 is applied // swiftlint:enable <rule1> 

You can also skip some files by setting up swiftlint. Add the " .swiftlint.yml " file to the directory where you will run SwiftLint.

Add the following content to exclude some files. Let's 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 to the following links.

https://swifting.io/blog/2016/03/29/11-swiftlint/

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

+28
source share

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

 disabled_rules: # rule identifiers to exclude from running - force_cast 

enter image description here

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

+9
source share

You can write //swiftlint:disable force_cast at the beginning of the file in which you want to suppress the warning for this rule. It is 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

+8
source share

Configure SwiftLint by adding the .swiftlint.yml file from the directory from which you will run SwiftLint. Here is the complete set of options that you can use in the .swiftlint.yaml 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) 

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

0
source share

All Articles