I have the following code
switch (attr.templateType) {
case 'text': return tpl_default; break;
case 'currency': return tpl_currency; break;
case 'percentage': return tpl_percentage; break;
case 'latlong': return tpl_latlong; break;
case 'tel': return tpl_phone; break;
case 'number': return tpl_number; break;
case 'address': return tpl_address; break;
case 'date': return tpl_date; break;
case 'permissions': return tpl_permissions; break;
case 'pagination': return tpl_pagination; break;
case 'time': return tpl_time; break;
case 'notEmpty': return tpl_notEmpty; break;
default: return tpl_default; break;
}
and JavaScript lint tells me "unreachable code detected" for ALL the breaks. If i take out the breaks, lint has no errors.
does anyone know why? The code works and with out any errors.
The JavaScript warning "unreachable code after return statement" occurs when using an expression after a return statement, or when using a semicolon-less return statement but including an expression directly after.
The break KeywordIt is not necessary to break the last case in a switch block. The block breaks (ends) there anyway. Note: If you omit the break statement, the next case will be executed even if the evaluation does not match the case.
Detection of unreachable code is a form of control flow analysis to find code that can never be reached in any possible program state. In some languages (e.g. Java) some forms of unreachable code are explicitly disallowed.
Unreachable code is a compile-time error in languages like C++, Java, and C or Unreachable code error occurs when the code can't be compiled; but why it is just a warning in C++ & C ? Warnings are a way that the compiler indicates that the particular mentioned thing might become an error in future.
why is break required after return? switch will return and break will never execute, that is why it is unreachable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With