else
keyword should be used properly in if...else
sequence
- MISSING_ELSE_KEYWORD
- Error
- Medium
- No tags
This rule applies when else
keyword appears to be missing in front of if
statement.
When if
keyword appears immediately after the closing brace (}) of previous if
statement and there is no line break, it is highly probable that else
keyword is unintentionally missing.
If else
keyword is indeed not needed, it is recommended to put the line break for code readability.
Note: Not applied when all branch bodies of the previous if
statement end with return
or throw
.
Noncompliant Code Example
View with compliant examples side by sideif (cond1) {
temp = a;
} if (cond2) { // MISSING_ELSE_KEYWORD alarm
return b;
} else {
temp = c;
}
Compliant Code Example
View with noncompliant examples side by side// Insert 'else' keyword if you intended 'else if'
if (cond1) {
temp = a;
} else if (cond2) {
return b;
} else {
temp = c;
}
// Insert line break for readability if 'else if' was not intended
if (cond1) {
temp = a;
}
if (cond2) {
return b;
} else {
temp = c;
}
Version
This rule was introduced in DeepScan 1.10.0-beta.