Skip to content

Commit

Permalink
Fix false positive in identical_operands (realm#2565)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpsim authored and sjavora committed Mar 9, 2019
1 parent 8e450d7 commit 13a4e69
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 12 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@

#### Bug Fixes

* None.
* Fix false positives on `identical_operands` rule when the right side of the
operand has a chained optional.
[JP Simard](https://github.com/jpsim)
[#2564](https://github.com/realm/SwiftLint/issues/2564)

## 0.29.4: In-Unit Operands

Expand Down
56 changes: 48 additions & 8 deletions Rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -8016,13 +8016,18 @@ $0 == 0
```

```swift
keyValues?.count ?? 0 == 0
keyValues?.count ?? 0 == 0
```

```swift
string == string.lowercased()
```

```swift
let num: Int? = 0
_ = num != nil && num == num?.byteSwapped
```

```swift
1 != 2
```
Expand Down Expand Up @@ -8068,13 +8073,18 @@ $0 != 0
```

```swift
keyValues?.count ?? 0 != 0
keyValues?.count ?? 0 != 0
```

```swift
string != string.lowercased()
```

```swift
let num: Int? = 0
_ = num != nil && num != num?.byteSwapped
```

```swift
1 === 2
```
Expand Down Expand Up @@ -8120,13 +8130,18 @@ $0 === 0
```

```swift
keyValues?.count ?? 0 === 0
keyValues?.count ?? 0 === 0
```

```swift
string === string.lowercased()
```

```swift
let num: Int? = 0
_ = num != nil && num === num?.byteSwapped
```

```swift
1 !== 2
```
Expand Down Expand Up @@ -8172,13 +8187,18 @@ $0 !== 0
```

```swift
keyValues?.count ?? 0 !== 0
keyValues?.count ?? 0 !== 0
```

```swift
string !== string.lowercased()
```

```swift
let num: Int? = 0
_ = num != nil && num !== num?.byteSwapped
```

```swift
1 > 2
```
Expand Down Expand Up @@ -8224,13 +8244,18 @@ $0 > 0
```

```swift
keyValues?.count ?? 0 > 0
keyValues?.count ?? 0 > 0
```

```swift
string > string.lowercased()
```

```swift
let num: Int? = 0
_ = num != nil && num > num?.byteSwapped
```

```swift
1 >= 2
```
Expand Down Expand Up @@ -8276,13 +8301,18 @@ $0 >= 0
```

```swift
keyValues?.count ?? 0 >= 0
keyValues?.count ?? 0 >= 0
```

```swift
string >= string.lowercased()
```

```swift
let num: Int? = 0
_ = num != nil && num >= num?.byteSwapped
```

```swift
1 < 2
```
Expand Down Expand Up @@ -8328,13 +8358,18 @@ $0 < 0
```

```swift
keyValues?.count ?? 0 < 0
keyValues?.count ?? 0 < 0
```

```swift
string < string.lowercased()
```

```swift
let num: Int? = 0
_ = num != nil && num < num?.byteSwapped
```

```swift
1 <= 2
```
Expand Down Expand Up @@ -8380,13 +8415,18 @@ $0 <= 0
```

```swift
keyValues?.count ?? 0 <= 0
keyValues?.count ?? 0 <= 0
```

```swift
string <= string.lowercased()
```

```swift
let num: Int? = 0
_ = num != nil && num <= num?.byteSwapped
```

```swift
func evaluate(_ mode: CommandMode) -> Result<AutoCorrectOptions, CommandantError<CommandantError<()>>>
```
Expand Down
10 changes: 7 additions & 3 deletions Source/SwiftLintFramework/Rules/Lint/IdenticalOperandsRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ public struct IdenticalOperandsRule: ConfigurationProviderRule, OptInRule, Autom
"lhs.identifier \(operation) rhs.identifier",
"i \(operation) index",
"$0 \(operation) 0",
"keyValues?.count ?? 0 \(operation) 0",
"string \(operation) string.lowercased()"
"keyValues?.count ?? 0 \(operation) 0",
"string \(operation) string.lowercased()",
"""
let num: Int? = 0
_ = num != nil && num \(operation) num?.byteSwapped
"""
]
} + [
"func evaluate(_ mode: CommandMode) -> Result<AutoCorrectOptions, CommandantError<CommandantError<()>>>",
Expand All @@ -47,7 +51,7 @@ public struct IdenticalOperandsRule: ConfigurationProviderRule, OptInRule, Autom
public func validate(file: File) -> [StyleViolation] {
let operators = type(of: self).operators.joined(separator: "|")
let pattern = """
(?<!\\.|\\$)(?:\\s|\\b|\\A)([\\$A-Za-z0-9_\\.]+)\\s*(\(operators))\\s*\\1\\b(?!\\s*(\\.|\\>|\\<))
(?<!\\.|\\$)(?:\\s|\\b|\\A)([\\$A-Za-z0-9_\\.]+)\\s*(\(operators))\\s*\\1\\b(?!\\s*(\\.|\\>|\\<|\\?))
"""
let syntaxKinds = SyntaxKind.commentKinds
let excludingPattern = "\\?\\?\\s*" + pattern
Expand Down

0 comments on commit 13a4e69

Please sign in to comment.