Superfluous Else
Else branches should be avoided when the previous if-block exits the current scope
- Identifier:
superfluous_else
- Enabled by default: No
- Supports autocorrection: Yes
- Kind: style
- Analyzer rule: No
- Minimum Swift compiler version: 5.0.0
- Default configuration:
Key Value severity warning
Non Triggering Examples
if i > 0 {
// comment
} else if i < 12 {
return 2
} else {
return 3
}
if i > 0 {
let a = 1
if a > 1 {
// comment
} else {
return 1
}
// comment
} else {
return 3
}
if i > 0 {
if a > 1 {
return 1
}
} else {
return 3
}
for i in list {
if i > 12 {
// Do nothing
} else {
continue
}
if i > 14 {
// Do nothing
} else if i > 13 {
break
}
}
if #available(iOS 13, *) {
return
} else {
deprecatedFunction()
}
Triggering Examples
if i > 0 {
return 1
// comment
} ↓else {
return 2
}
if i > 0 {
return 1
} ↓else if i < 12 {
return 2
} ↓else if i > 18 {
return 3
}
if i > 0 {
if i < 12 {
return 5
} ↓else {
if i > 11 {
return 6
} ↓else {
return 7
}
}
} ↓else if i < 12 {
return 2
} ↓else if i < 24 {
return 8
} ↓else {
return 3
}
for i in list {
if i > 13 {
return
} ↓else if i > 12 {
continue
} ↓else if i > 11 {
break
} ↓else {
throw error
}
}