Skip to content

Commit

Permalink
Merge pull request #2194 from dabelknap/no-fallthrough-only
Browse files Browse the repository at this point in the history
Add "No Fallthrough Only" Rule
  • Loading branch information
marcelofabri authored Jun 12, 2018
2 parents 6f9591e + e6517d9 commit 3eba5e9
Show file tree
Hide file tree
Showing 7 changed files with 421 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

* Updates the `untyped_error_in_catch` rule to support autocorrection.
[Daniel Metzing](https://github.com/dirtydanee)
* Add `no-fallthrough-only` rule to check that `case` statements do not contain
only a `fallthrough`.
[Austin Belknap](https://github.com/dabelknap)

* Add `indented_cases` support to `switch_case_alignment` rule.
[Shai Mishali](https://github.com/freak4pc)
Expand Down
179 changes: 179 additions & 0 deletions Rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
* [Nesting](#nesting)
* [Nimble Operator](#nimble-operator)
* [No Extension Access Modifier](#no-extension-access-modifier)
* [No Fallthrough Only](#no-fallthrough-only)
* [No Grouping Extension](#no-grouping-extension)
* [Notification Center Detachment](#notification-center-detachment)
* [Number Separator](#number-separator)
Expand Down Expand Up @@ -10298,6 +10299,184 @@ extension String {}



## No Fallthrough Only

Identifier | Enabled by default | Supports autocorrection | Kind | Minimum Swift Compiler Version
--- | --- | --- | --- | ---
`no_fallthrough_only` | Enabled | No | idiomatic | 3.0.0

Fallthroughs can only be used if the `case` contains at least one other statement.

### Examples

<details>
<summary>Non Triggering Examples</summary>

```swift
switch myvar {
case 1:
var a = 1
fallthrough
case 2:
var a = 2
}
```

```swift
switch myvar {
case "a":
var one = 1
var two = 2
fallthrough
case "b": /* comment */
var three = 3
}
```

```swift
switch myvar {
case 1:
let one = 1
case 2:
// comment
var two = 2
}
```

```swift
switch myvar {
case MyFunc(x: [1, 2, YourFunc(a: 23)], y: 2):
var three = 3
fallthrough
default:
var three = 4
}
```

```swift
switch myvar {
case .alpha:
var one = 1
case .beta:
var three = 3
fallthrough
default:
var four = 4
}
```

```swift
let aPoint = (1, -1)
switch aPoint {
case let (x, y) where x == y:
let A = "A"
case let (x, y) where x == -y:
let B = "B"
fallthrough
default:
let C = "C"
}
```

```swift
switch myvar {
case MyFun(with: { $1 }):
let one = 1
fallthrough
case "abc":
let two = 2
}
```

</details>
<details>
<summary>Triggering Examples</summary>

```swift
switch myvar {
case 1:
↓fallthrough
case 2:
var a = 1
}
```

```swift
switch myvar {
case 1:
var a = 2
case 2:
↓fallthrough
case 3:
var a = 3
}
```

```swift
switch myvar {
case 1: // comment
↓fallthrough
}
```

```swift
switch myvar {
case 1: /* multi
line
comment */
↓fallthrough
case 2:
var a = 2
}
```

```swift
switch myvar {
case MyFunc(x: [1, 2, YourFunc(a: 23)], y: 2):
↓fallthrough
default:
var three = 4
}
```

```swift
switch myvar {
case .alpha:
var one = 1
case .beta:
↓fallthrough
case .gamma:
var three = 3
default:
var four = 4
}
```

```swift
let aPoint = (1, -1)
switch aPoint {
case let (x, y) where x == y:
let A = "A"
case let (x, y) where x == -y:
↓fallthrough
default:
let B = "B"
}
```

```swift
switch myvar {
case MyFun(with: { $1 }):
↓fallthrough
case "abc":
let two = 2
}
```

</details>



## No Grouping Extension

Identifier | Enabled by default | Supports autocorrection | Kind | Minimum Swift Compiler Version
Expand Down
3 changes: 2 additions & 1 deletion Source/SwiftLintFramework/Models/MasterRuleList.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generated using Sourcery 0.11.2 — https://github.com/krzysztofzablocki/Sourcery
// Generated using Sourcery 0.13.1 — https://github.com/krzysztofzablocki/Sourcery
// DO NOT EDIT

public let masterRuleList = RuleList(rules: [
Expand Down Expand Up @@ -75,6 +75,7 @@ public let masterRuleList = RuleList(rules: [
NestingRule.self,
NimbleOperatorRule.self,
NoExtensionAccessModifierRule.self,
NoFallthroughOnlyRule.self,
NoGroupingExtensionRule.self,
NotificationCenterDetachmentRule.self,
NumberSeparatorRule.self,
Expand Down
Loading

0 comments on commit 3eba5e9

Please sign in to comment.