Unreachable code
Code not executed because of preceding control-flow statements
Description
This defect occurs when a section of code cannot be reached because of a previous break in control flow using one these statements:
break
andreturn
: Statements such asbreak
andreturn
, move the flow of the program to another section or function. Because of this flow escape, the statements following the control-flow code cannot execute, and therefore the statements are unreachable.goto
: A statement such asgoto
causes a jump in the program flow to another section of the code. Unless there is a jump back to the immediate statements following thegoto
, these statements cannot be executed.while(1)
: Trivial infinite loops, such aswhile(1)
release the flow of the program by exiting the program. This type of exit causes code after the infinite loop to be unreachable.
Risk
Unreachable code wastes development time, memory and execution cycles. Developers have to maintain code that is not being executed. Instructions that are not executed still have to be stored and cached.
Fix
The fix depends on the intended functionality of the unreachable code. If you want
the code to be executed, check the placement of the code or the prior statement that
diverts the control flow. For instance, if the unreachable code follows a
return
statement, you might have to switch their order or
remove the return
statement altogether.
If you do not want to fix the issue, add comments to your result or code to avoid another review. See:
Address Results in Polyspace User Interface Through Bug Fixes or Justifications if you review results in the Polyspace user interface.
Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access) if you review results in a web browser.
Annotate Code and Hide Known or Acceptable Results if you review results in an IDE.
Examples
Result Information
Group: Data flow |
Language: C | C++ |
Default: On |
Command-Line Syntax: UNREACHABLE |
Impact: Medium |
Version History
Introduced in R2013b
See Also
Find defects (-checkers)
| Dead code
| Useless if
| Code deactivated by constant false condition
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)