Insure++ Reference - DEAD_CODE
This error is generated when code is not evaluated, has no effect, or
is unreachable.
Insure++ distinguishes between several types of dead code as
follows:
Type of error
|
Explanation
|
emptystmt
|
The statement is empty.
|
emptyloopbody
|
Loop body is empty.
|
noeffect
|
Code has no effect.
|
notevaluated
|
Code is not evaluated.
|
Error messages are classified according to this scheme and can be
selectively enabled or disabled. By default, this error category is
suppressed.
The following code shows a very tricky, well-disguised error that
demonstrates how hard it is to find problems of this type without
Insure++. The initialization function get_glob
is never called by this code. Because func_X2 is
declared as a static function in the Global class,
it can be called directly by main . This is in fact
what happens, meaning that line 23 is interpreted as only a call to
func_X2 . Therefore, an error is generated since the
call to get_glob is never evaluated.
1: /*
2: * File: deadcode.C
3: */
4: #include <iostream.h>
5:
6: class Global {
7: public:
8: int j;
9: static int func_X2(int i);
10: };
11:
12: int Global::func_X2(int i) {
13: return i*2;
14: }
15:
16: Global *get_glob() {
17: cerr << "Initializing..."
18: << endl;
19: return (Global *) 0;
20: }
21:
22: int main() {
23: get_glob()->func_X2(2);
24: return 0;
25: }
1. [deadcode.C:23] **DEAD_CODE(notevaluated)**
2. Code is not evaluated
>> get_glob()->func_X2(2);
- Source line at which the problem was detected.
- Description of the problem and the expression that is incorrect.
This problem can be solved by replacing line 23 with a direct call to
func_X2 :
Global::func_X2(2);
or by not making func_X2 a static function.
In some cases, it may be that the dead code was never intended to be
called. If that is the case, the dead code should be eliminated for
clarity.
The following code illustrates several other types of
DEAD_CODE errors, this time in C.
1: /*
2: * File: deadcode.c
3: */
4: int main()
5: {
6: int i = 0;
7:
8: ; |
9: i; |
|-->
10: for (i; i; i) |
11: ; |
12: return 0;
13: }
1. [deadcode.c:8] **DEAD_CODE(emptystmt)**
2. Statement is empty
>> ;
[deadcode.c:9] **DEAD_CODE(noeffect)**
Code has no effect
>> i;
[deadcode.c:10] **DEAD_CODE(noeffect)**
For loop initializer has no effect
>> for (i; i; i)
[deadcode.c:10] **DEAD_CODE(noeffect)**
For loop increment has no effect
>> for (i; i; i)
[deadcode.c:11] **DEAD_CODE(emptyloopbody)**
Loop body is empty (may be okay)
>> ;
- Source line at which the problem was detected.
- Description of the problem and the expression that is incorrect.
These errors are usually corrected by removing the superfluous
statement or by modifying the statement so that it does what it was
intended to do, e.g add a missing increment operator. An empty loop
body may be useful in certain situations. In such a case, you may want
to suppress that subcategory of DEAD_CODE .
COPY_WILD
DELETE_MISMATCH
|