Swiftlint: "colon" violation no longer autocorrected in dictionary literals (since 0.23.1)

Created on 14 Feb 2018  路  3Comments  路  Source: realm/SwiftLint

New Issue Checklist

Bug Report

swiftlint autocorrect does not correct the colon rule on some cases (especially in dictionary literals?).

I've tested with versions 0.24.0 thru 0.25.0 but the bug is still present, while it was working in 0.23.1

Environment

  • SwiftLint versions: 0.23.1 OK, but bug present with 0.24.0, 0.24.1, 0.24.2 and 0.25.0
  • Installation method used: portable zip (also tested 0.23.1 with homebrew, and 0.25.0 with CocoaPods)
  • Which Xcode version are you using (check xcode-select -p)? 9.2

Sample code to reproduce

Sample swift file

import UIKit

class SwiftLintBugSample: UIViewController {

    fileprivate func updateTableView() {

        let dict = [KeysConstants.CELL_KEYS.CELL_TYPE : FooCellType.editTextCell.rawValue,
                    KeysConstants.CELL_KEYS.CELL_IDENTIFIER_KEY : FooEditTextTableViewCell.cellIdentifier,
                    KeysConstants.CELL_KEYS.CELL_GRAY : false,
                    KeysConstants.CELL_KEYS.VALUE_ERROR : self.isInError,
                    KeysConstants.CELL_KEYS.ESTIMATED_CELL_HEIGHT : FooEditTextTableViewCell.cellHeight] as [String: Any]

        self.dataSource = dict as NSDictionary

    }
}

Configuration File

whitelist_rules:
  - colon

Test script

This script aims to test a given swiftlint version by linting first to show the colon violations, then trying to autocorrect them, then linting again to check if they have indeed been corrected.

#!/bin/sh

bin=$1/swiftlint
echo ">>> Testing with version `$bin version`"

git checkout sample.swift

echo "------ Initial linting ------"
$bin lint --path sample.swift

echo "------ Autocorrecting  ------"
$bin autocorrect --path sample.swift

echo "------  Linting again  ------"
$bin lint --path sample.swift

Note: I made this test script in order to test the bug across multiple versions of SwiftLint. In order for this script to work, I've downloaded various versions of SwiftLint (portable zip) and placed each version in its own folder (each folder named after the corresponding version)

Output for various Swiftlint versions


swiftlint 0.23.1

This version works as expected: 5 violations before autocorrect, all corrected, no more violation after autocorrect.

```sh
$ ./test.sh 0.23.1

Testing with version 0.23.1
------ Initial linting ------
Loading configuration from '.swiftlint.yml'
Linting Swift files at path sample.swift
Linting 'sample.swift' (1/1)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:7:54: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:8:64: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:9:54: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:10:56: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:11:66: 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 5 violations, 0 serious in 1 file.
------ Autocorrecting ------
Loading configuration from '.swiftlint.yml'
Correcting Swift files at path sample.swift
Correcting 'sample.swift' (1/1)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:11:66 Corrected Colon
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:10:56 Corrected Colon
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:9:54 Corrected Colon
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:8:64 Corrected Colon
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:7:54 Corrected Colon
Done correcting 1 files!
------ Linting again ------
Loading configuration from '.swiftlint.yml'
Linting Swift files at path sample.swift
Linting 'sample.swift' (1/1)
Done linting! Found 0 violations, 0 serious in 1 file.
```


swiftlint 0.24.0

This version does not work as expected: 5 violations before autocorrect, none auto-corrected, still 5 violation present after autocorrect.

```sh
$ ./test.sh 0.24.0

Testing with version 0.24.0
------ Initial linting ------
Loading configuration from '.swiftlint.yml'
Linting Swift files at path sample.swift
Linting 'sample.swift' (1/1)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:7:54: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:8:64: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:9:54: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:10:56: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:11:66: 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 5 violations, 0 serious in 1 file.
------ Autocorrecting ------
Loading configuration from '.swiftlint.yml'
Correcting Swift files at path sample.swift
Correcting 'sample.swift' (1/1)
Done correcting 1 files!
------ Linting again ------
Loading configuration from '.swiftlint.yml'
Linting Swift files at path sample.swift
Linting 'sample.swift' (1/1)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:7:54: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:8:64: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:9:54: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:10:56: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:11:66: 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 5 violations, 0 serious in 1 file.
```


swiftlint 0.24.1

This version does not work as expected: 5 violations before autocorrect, none auto-corrected, still 5 violation present after autocorrect.

```sh
$ ./test.sh 0.24.1

Testing with version 0.24.1
------ Initial linting ------
Loading configuration from '.swiftlint.yml'
Invalid configuration for 'indentation'. Falling back to default.
Linting Swift files at path sample.swift
Linting 'sample.swift' (1/1)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:7:54: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:8:64: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:9:54: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:10:56: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:11:66: 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 5 violations, 0 serious in 1 file.
------ Autocorrecting ------
Loading configuration from '.swiftlint.yml'
Invalid configuration for 'indentation'. Falling back to default.
Correcting Swift files at path sample.swift
Correcting 'sample.swift' (1/1)
Done correcting 1 files!
------ Linting again ------
Loading configuration from '.swiftlint.yml'
Invalid configuration for 'indentation'. Falling back to default.
Linting Swift files at path sample.swift
Linting 'sample.swift' (1/1)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:7:54: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:8:64: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:9:54: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:10:56: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:11:66: 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 5 violations, 0 serious in 1 file.
```


swiftlint 0.24.2

This version does not work as expected: 5 violations before autocorrect, none auto-corrected, still 5 violation present after autocorrect.

```sh
$ ./test.sh 0.24.2

Testing with version 0.24.2
------ Initial linting ------
Loading configuration from '.swiftlint.yml'
Linting Swift files at path sample.swift
Linting 'sample.swift' (1/1)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:7:54: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:8:64: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:9:54: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:10:56: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:11:66: 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 5 violations, 0 serious in 1 file.
------ Autocorrecting ------
Loading configuration from '.swiftlint.yml'
Correcting Swift files at path sample.swift
Correcting 'sample.swift' (1/1)
Done correcting 1 files!
------ Linting again ------
Loading configuration from '.swiftlint.yml'
Linting Swift files at path sample.swift
Linting 'sample.swift' (1/1)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:7:54: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:8:64: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:9:54: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:10:56: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:11:66: 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 5 violations, 0 serious in 1 file.
```


swiftlint 0.25.0

This version does not work as expected: 5 violations before autocorrect, none auto-corrected, still 5 violation present after autocorrect.

```sh
$ ./test.sh 0.25.0

Testing with version 0.25.0
------ Initial linting ------
Loading configuration from '.swiftlint.yml'
Linting Swift files at path sample.swift
Linting 'sample.swift' (1/1)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:7:54: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:8:64: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:9:54: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:10:56: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:11:66: 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 5 violations, 0 serious in 1 file.
------ Autocorrecting ------
Loading configuration from '.swiftlint.yml'
Correcting Swift files at path sample.swift
Correcting 'sample.swift' (1/1)
Done correcting 1 files!
------ Linting again ------
Loading configuration from '.swiftlint.yml'
Linting Swift files at path sample.swift
Linting 'sample.swift' (1/1)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:7:54: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:8:64: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:9:54: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:10:56: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:11:66: 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 5 violations, 0 serious in 1 file.
```


\cc @marcelofabri as discussed on Slack.

bug

Most helpful comment

Interestingly, changing the Dictionary literal into a function call makes autocorrect work in 0.25.0 (but not 0.24.2).

So maybe it's rather an issue with auto-correcting colons in dictionary literals specifically?

i.e. this gets auto-corrected properly in 0.25.0 (and also in 0.23.1, but not in 0.24.2) :

import UIKit

class SwiftLintBugSample: UIViewController {

    fileprivate func updateTableView() {

        foo(
            CELL_TYPE : FooCellType.editTextCell.rawValue,
            CELL_IDENTIFIER_KEY :FooEditTextTableViewCell.cellIdentifier,
            CELL_GRAY:  false,
            VALUE_ERROR  : self.isInError,
            ESTIMATED_CELL_HEIGHT:FooEditTextTableViewCell.cellHeight
        )

        self.dataSource = dict as NSDictionary

    }
}

See also https://github.com/realm/SwiftLint/pull/2007 which probably corrected only _part_ of the regression

All 3 comments

Additional info: I've also tested with this sample code:

import UIKit

class SwiftLintBugSample: UIViewController {

    fileprivate func updateTableView() {

        let dict = [
            KeysConstants.CELL_KEYS.CELL_TYPE : FooCellType.editTextCell.rawValue,
            KeysConstants.CELL_KEYS.CELL_IDENTIFIER_KEY :FooEditTextTableViewCell.cellIdentifier,
            KeysConstants.CELL_KEYS.CELL_GRAY:  false,
            KeysConstants.CELL_KEYS.VALUE_ERROR  : self.isInError,
            KeysConstants.CELL_KEYS.ESTIMATED_CELL_HEIGHT:FooEditTextTableViewCell.cellHeight
        ] as [String: Any]

        self.dataSource = dict as NSDictionary

    }
}

And the problem still remains: autocorrected properly in 0.23.1, not in 0.25.0.

Interestingly, changing the Dictionary literal into a function call makes autocorrect work in 0.25.0 (but not 0.24.2).

So maybe it's rather an issue with auto-correcting colons in dictionary literals specifically?

i.e. this gets auto-corrected properly in 0.25.0 (and also in 0.23.1, but not in 0.24.2) :

import UIKit

class SwiftLintBugSample: UIViewController {

    fileprivate func updateTableView() {

        foo(
            CELL_TYPE : FooCellType.editTextCell.rawValue,
            CELL_IDENTIFIER_KEY :FooEditTextTableViewCell.cellIdentifier,
            CELL_GRAY:  false,
            VALUE_ERROR  : self.isInError,
            ESTIMATED_CELL_HEIGHT:FooEditTextTableViewCell.cellHeight
        )

        self.dataSource = dict as NSDictionary

    }
}

See also https://github.com/realm/SwiftLint/pull/2007 which probably corrected only _part_ of the regression

Oops I didn't realize this bug. Sorry, and thanks for fixing it!

Was this page helpful?
0 / 5 - 0 ratings