Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix false positives on explicit_type_interface rule #2426

Merged
merged 4 commits into from
Nov 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
[Marcelo Fabri](https://github.com/marcelofabri)
[#2489](https://github.com/realm/SwiftLint/issues/2489)

* Fix false positives on `explicit_type_interface` rule when
configured with option `allowRedundancy` set to `true`.
[Cihat Gündüz](https://github.com/Dschee)
[#2425](https://github.com/realm/SwiftLint/issues/2425)

## 0.29.0: A Laundry List of Changes

#### Breaking
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ public struct ExplicitTypeInterfaceRule: ASTRule, OptInRule, ConfigurationProvid
}

let contentAfterName = file.contents.bridge().substring(from: afterNameRange.location)
let initCallRegex = regex("^\\s*=\\s*\\p{Lu}[^\\(\\s<]*(?:<[^\\>]*>)?\\(")
let initCallRegex = regex(
"^\\s*=\\s*(?:try[!?]?\\s+)?\\[?\\p{Lu}[^\\(\\s<]*(?:<[^\\>]*>)?(?::\\s*[^\\(\\n]+)?\\]?\\("
)

return initCallRegex.firstMatch(in: contentAfterName, options: [], range: contentAfterName.fullNSRange) != nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,20 @@ class ExplicitTypeInterfaceRuleTests: XCTestCase {
"class Foo {\n class var myVar: Int? = 0\n}\n",
"class Foo {\n static let shared = Foo()\n}\n",
"class Foo {\n let myVar = Int(0)\n}\n",
"class Foo {\n let myVar = Set<Int>(0)\n}\n"
"class Foo {\n let myVar = Set<Int>(0)\n}\n",
"class Foo {\n let regex = try! NSRegularExpression(pattern: \".*\")\n}\n",
"class Foo {\n let regex = try? NSRegularExpression(pattern: \".*\")\n}\n",
"class Foo {\n let array = [String]()\n}\n",
"class Foo {\n let dict = [String: String]()\n}\n",
"class Foo {\n let dict = [String: [String: Array<String>]]()\n}\n"
]
let triggeringExamples = [
"class Foo {\n ↓var myVar = 0\n\n}\n",
"class Foo {\n ↓let mylet = 0\n\n}\n",
"class Foo {\n ↓static var myStaticVar = 0\n}\n",
"class Foo {\n ↓class var myClassVar = 0\n}\n"
"class Foo {\n ↓class var myClassVar = 0\n}\n",
"class Foo {\n ↓let array = [\"foo\", \"bar\"]\n}\n",
"class Foo {\n ↓let dict = [\"foo\": \"bar\"]\n}\n"
]
let description = ExplicitTypeInterfaceRule.description
.with(triggeringExamples: triggeringExamples)
Expand Down