17

my question is very simple, how do I make all warnings become errors on SwiftLint? (without manually configuring each rule separately)

3
  • Try the --strict option
    – Andrew Li
    Commented Feb 23, 2017 at 4:09
  • Where do I add this --strict ? Commented Mar 4, 2017 at 21:29
  • 1
    "${PODS_ROOT}/SwiftLint/swiftlint" lint --strict (for pod install) or swiftlint lint --strict (for brew install)
    – Cœur
    Commented Mar 30, 2017 at 1:46

3 Answers 3

18

To integrate SwiftLint to your project, you normally need to add a Run Script Phase, as described by the doc.

If you used the CocoaPods installation, this script would look like:

"${PODS_ROOT}/SwiftLint/swiftlint"

That is where you can customize the command line options. In your case, you may want to use:

"${PODS_ROOT}/SwiftLint/swiftlint" lint --strict

The warnings will still be displayed as warnings, but an extra error will be given, preventing running or archiving:

Command /bin/sh failed with exit code 3

That is the desired error.

12
  • Still get the error /Pods/SwiftLint/swiftlint lint --strict: No such file or directory. And the actual thing written in the script is "${PODS_ROOT}/SwiftLint/swiftlint lint --strict", not ${PODS_ROOT}/SwiftLint/swiftlint lint --strict Commented Mar 30, 2017 at 1:01
  • Their documentation shows the quotes github.com/realm/SwiftLint like I wrote. And yes, I am using CocoaPods. Commented Mar 30, 2017 at 1:46
  • And even if I remove the quotes, I get this error Command /bin/sh failed with exit code 3 Commented Mar 30, 2017 at 1:47
  • It's not showing me the line where the error is, the warnings are still warnings. Commented Mar 30, 2017 at 1:49
  • Is there a way to be a little better and have the warnings display as errors? But in any case, thank you for the help. Commented Mar 30, 2017 at 1:50
10

One drawback with "--strict" flag is that it won't show which line has a Warning.

You can pipe the output and replace "warning" with "error" by adding:

| sed "s/warning:/error:/"

the whole command will look like:

"${PODS_ROOT}/SwiftLint/swiftlint" lint --strict | sed "s/warning:/error:/"

then Xcode will show all SwiftLint warnings as errors.

1

You can make every swiftlint rule throw an error by enabling --strict

swiftlint lint --strict

Alternatively, you can configure each rule of swiftlint if you are using a config.yml file so that only certain rules throw an error.

swiftlint lint --config 'config.yml'

In the config.yml file simply add the configuration for the rule you want to throw as an error.

implicit_return:
    severity: error

I believe this will work for any swiftlint rule. This is useful if you are looking to gradually adopt a stricter approach to linting.

Not the answer you're looking for? Browse other questions tagged or ask your own question.