6

In running swiftlint version 0.24.0, errors are reported. Running autocorrect states that the files have been corrected. Yet no modifications are made as a subsequent run of swiftlint proves.

See the attempt to lint and correct two files Player.swift and Prize.swift.

MacBook-Pro-5: Developer$ swiftlint 
Loading configuration from '.swiftlint.yml'
Linting Swift files in current working directory
Linting 'Prize.swift' (1/2)
Linting 'Player.swift' (2/2)
Player.swift:26:19: warning: Operator Function Whitespace Violation: Operators should be surrounded by a single whitespace when defining them. (operator_whitespace)
Player.swift:27: warning: Line Length Violation: Line should be 120 characters or less: currently 131 characters (line_length)
Player.swift:39: warning: Line Length Violation: Line should be 120 characters or less: currently 147 characters (line_length)
Prize.swift:44:34: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
Prize.swift:44: error: Line Length Violation: Line should be 120 characters or less: currently 210 characters (line_length)
Done linting! Found 5 violations, 1 serious in 2 files.
MacBook-Pro-5: Developer$ swiftlint autocorrect
Loading configuration from '.swiftlint.yml'
Correcting Swift files in current working directory
Correcting 'Prize.swift' (1/2)
Correcting 'Player.swift' (2/2)
Done correcting 2 files!
MacBook-Pro-5: Developer$ swiftlint 
Loading configuration from '.swiftlint.yml'
Linting Swift files in current working directory
Linting 'Prize.swift' (1/2)
Linting 'Player.swift' (2/2)
Prize.swift:44:34: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
Prize.swift:44: error: Line Length Violation: Line should be 120 characters or less: currently 210 characters (line_length)
Player.swift:26:19: warning: Operator Function Whitespace Violation: Operators should be surrounded by a single whitespace when defining them. (operator_whitespace)
Player.swift:27: warning: Line Length Violation: Line should be 120 characters or less: currently 131 characters (line_length)
Player.swift:39: warning: Line Length Violation: Line should be 120 characters or less: currently 147 characters (line_length)
Done linting! Found 5 violations, 1 serious in 2 files.
MacBook-Pro-5: Developer$

Here's another run but this time with one file Prize.swift containing only one correctable violation. Again, autocorrect does not modify:

MacBook-Pro-5:P Developer$ swiftlint 
Linting Swift files in current working directory
Linting 'Prize.swift' (1/2)
Linting 'Player.swift' (2/2)
PPrize.swift:44:34: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
Done linting! Found 1 violation, 0 serious in 2 files.
MacBook-Pro-5:P Developer$ swiftlint autocorrect
Correcting Swift files in current working directory
Correcting 'Prize.swift' (1/2)
Correcting 'Player.swift' (2/2)
Done correcting 2 files!
MacBook-Pro-5:P Developer$ swiftlint 
Linting Swift files in current working directory
Linting 'Prize.swift' (1/2)
Linting 'Player.swift' (2/2)
PPrize.swift:44:34: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
Done linting! Found 1 violation, 0 serious in 2 files.
MacBook-Pro-5:P Developer$ 

Note that the presence or not of a .swiftlint.yml file had zero bearing on this.

Abbreviated - that is, only showing relevant rules - output from swiftlint rules is shown below. Note that of the three violations, colon is definitely correctable.

MacBook-Pro-5: Developer$ swiftlint rules
+------------------------------------------+--------+-------------+------------------------+-------------+---------------------------------------------------------------------------------------------------------------------------+
| identifier                               | opt-in | correctable | enabled in your config | kind        | configuration                                                                                                             |
+------------------------------------------+--------+-------------+------------------------+-------------+---------------------------------------------------------------------------------------------------------------------------+
| colon                                    | no     | yes         | yes                    | style       | warning, flexible_right_spacing: false, apply_to_dictionaries: true                                                       |
| line_length                              | no     | no          | yes                    | metrics     | warning: 120, error: 200, ignores urls: false, ignores function declarations: false, ignores comments: false              |
| operator_whitespace                      | no     | no          | yes                    | style       | warning                                                                                                                   |
+------------------------------------------+--------+-------------+------------------------+-------------+---------------------------------------------------------------------------------------------------------------------------+

Any thoughts on how swiftlint autocorrect can run successfully? Might there be some obvious configuration or parameter I am missing? I have consulted https://github.com/realm/SwiftLint to no avail.

2 Answers 2

9

updated
swiftlint autocorrect --format
swiftlint --fix --format
swiftlint lint --fix --format

will work for all. source: https://github.com/realm/SwiftLint/issues/3571

old
if swiftlint version below 0.42.0 then you can auto correct it using swiftlint autocorrect --format

but if its 0.43.0 or higher then you need to use swiftlint --fix

it will auto correct the white space, trailing space, initial formating, bracket in if else. etc. which will not affect on your logics or codes.

swiftlint autocorrect --format //for version < 0.42.0
swiftlint --fix //for latest version >= 0.43.0

Sign up to request clarification or add additional context in comments.

Comments

3

Not all SwiftLint rules are correctable, which mean that they will remain after you run swiftlint autocorrect. If you run swiftlint rules you can see a table that – among other things – show which rules are correctable.

I'm not sure what version of SwiftLint you are using, but I don't think line_length or operator_whitespace are correctable. You will have to deal with those yourself.

2 Comments

Added detail to the question. I'm wondering if - for example - a file has three violations of which just one is correctable - autocorrect simply ignores the file. Sometimes autocorrect does appear to work. Note that the question is an abbreviated example. I've been running autocorrect against a workspace with hundreds of files and correctable violations. Of say 620 files, autocorrect will fix perhaps 4. I don't know why and I don't know why those specific files are corrected. Feels like something obvious is missing. Just don't know what.
this could be a bug in the latest version. I am currently using 0.23 and it works fine. sometimes it cant fix particular cases, but fixes everything else

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.