Imagix 4D User Guide


Useless Assignments

The Useless Assignments report shows assignments to global, static and local variables where the assigned value is never used, either because the variable is set again before it is read, or because the variable is never read after it is set. The existence of useless assignments might indicate that some logic is faulty or missing. Consider the following example:

int globalA = 1;
int globalB = 1;

int func2(int paramX, int paramY) {

    paramY = globalB;
    globalA = paramY;
    return paramX;
}

int func1(void) {
    int localX, localY, localZ;

    localX = 1;
    localY = 1;
    localZ = 1;
    globalB = 2;

    localZ = func2(localX, localY);

    return localZ;
}

The report indicates the specific variable assignments where the variable itself is subsequently set again before it is read (localZ), or where the variable is never subsequently read (globalA). These are considered to be directly useless assignments and will always be listed, regardless of whether transitively useless assignments are selected.

Useless Assignments

Settings:
        Global Variables:        displayed
        Static Variables:        displayed
        Local Variables:         displayed
        Transitively Useless:    not displayed
        Union/Bitfield Members:  separate

Variable                                          File (Line)
    Function                                          File (Line)
       Assignment

globalA                                           useless_asgn.c (2)
    func2                                             useless_asgn.c (5)
        8 globalA = paramY;

localZ                                            useless_asgn.c (13)
    func1                                             useless_asgn.c (12)
       17 localZ = 1;

Enabling the display of transitively useless assignments extends the assignments that are listed. Notice that the assignments of paramY and globalB are now included in the report. The assignment of globalA in func2 is directly useless. Since the assignment of paramY only contributes to the useless assignment of globalA, this upstream assignment is considered to be transitively useless. One step further upstream, the same is true for the use of globalB in setting the value of paramY.

If you enable the explanation for each result, the reported reason can help you understand why a given assignment is useless.

Useless Assignments

Settings:
        Global Variables:        displayed
        Static Variables:        displayed
        Local Variables:         displayed
        Transitively Useless:    displayed
        Union/Bitfield Members:  separate

Variable                                          File (Line)
    Function                                          File (Line)
       Assignment

globalA                                           useless_asgn.c (2)
    func2                                             useless_asgn.c (5)
        8 globalA = paramY;
        Reason: reaches end of control flow without use 

globalB                                           useless_asgn.c (3)
    func1                                             useless_asgn.c (12)
       18 globalB = 2;
        Reason: reaches end of control flow without use 

localZ                                            useless_asgn.c (13)
    func1                                             useless_asgn.c (12)
       17 localZ = 1;
        Reason: reaches end of control flow without use 

paramY                                            useless_asgn.c (5)
    func2                                             useless_asgn.c (5)
        7 paramY = globalB;
        Reason: transitively useless because useless assignment at line 8 in function func2