Jump statements (return, break, continue, goto) and throw expressions move control
flow out of the current code block. So any unlabelled statements that come after a jump are dead code.
Noncompliant Code Example
func add(x, y int) int {
return x + y // Noncompliant
z := x + y // dead code
}
Compliant Solution
func add(x, y int) int {
return x + y // Compliant
}
See
- MISRA C:2004, 14.1 - There shall be no unreachable code
- MISRA C++:2008, 0-1-1 - A project shall not contain unreachable code
- MISRA C++:2008, 0-1-9 - There shall be no dead code
- MISRA C:2012, 2.1 - A project shall not contain unreachable code
- MISRA C:2012, 2.2 - There shall be no dead code
- MITRE, CWE-561 - Dead Code
- CERT, MSC56-J. - Detect and remove superfluous code and values
- CERT, MSC12-C. - Detect and remove code that has no effect or is never
executed