Dead code
Code does not execute
Description
This defect occurs when a block of code cannot be reached because of a condition that is always true or false. This defect excludes:
Code deactivated by constant false condition
, which checks for directives with compile-time constants such as#if 0
orif(0)
.Unreachable code
, which checks for code after a control escape such asgoto
,break
, orreturn
.Useless if
, which checks for if statements that are always true.
Risk
Dead 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.
Dead code often represents legacy code that is no longer used. Cleaning up dead code periodically reduces future maintenance.
Fix
The fix depends on the root cause of the defect. For instance, the root cause can be an error condition that is checked twice on the same execution path, making the second check redundant and the corresponding block dead code.
Often the result details (or source code tooltips in Polyspace as You Code) show a sequence of events that led to the defect. You can implement the fix on any event in the sequence. If the result details do not show this event history, you can search for previous references of variables relevant to the defect using right-click options in the source code and find related events. See also Interpret Bug Finder Results in Polyspace Desktop User Interface or Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access).
See examples of fixes below.
If you see dead code from use of functions such as isinf
and
isnan
, enable an analysis mode that takes into account
non-finite values. See Consider non
finite floats (-allow-non-finite-floats)
.
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: DEAD_CODE |
Impact: Low |
Version History
Introduced in R2013bSee Also
Find defects (-checkers)
| Unreachable 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)