Rule 0-1-6 Variables being given values that are never used.

Moderators: david ward, misra cpp

Post Reply
LordMordac
Posts: 2
Joined: Tue Jan 14, 2020 8:05 pm
Company: nvidia

Rule 0-1-6 Variables being given values that are never used.

Post by LordMordac » Fri Jan 17, 2020 5:34 pm

The actual rule is:
A project shall not contain instances of non-volatile variables being given values that are never subsequently used.
There has been a lot of discussion as to what that sentence actually means. One camp believes that this rule applies to DU dataflow anomalies only, and the other believes that it applies to DU and DD dataflow anomalies.

For example the following code contains a DD dataflow anomaly, but not a DU dataflow anomaly. Is it a Rule 0-1-6 violation?

Code: Select all

int32_t foobar (int32_t const value)
{
    int32_t returnValue = value;
    if (value < 0)
    {
        returnValue = 0;
    }
    return returnValue;
}
This debate is influencing discussions on the merits of variable initialization.

misra cpp
Posts: 151
Joined: Mon Jun 02, 2008 1:55 pm
Company: MISRA

Re: Rule 0-1-6 Variables being given values that are never used.

Post by misra cpp » Wed Jan 22, 2020 10:24 am

The rule was intended to address DU anomalies only.

Note that the rationale in the MISRA standard for this rule starts “Technically known as a DU dataflow anomaly…”
Posted by and on behalf of
the MISRA C++ Working Group

chgros
Posts: 1
Joined: Wed Jan 22, 2020 11:13 pm
Company: Synopsys Inc.

Re: Rule 0-1-6 Variables being given values that are never used.

Post by chgros » Fri Jan 24, 2020 5:25 pm

So, just to make sure, this is compliant according to this rule:

Code: Select all

int i = 1;
i = 2;
return i;

misra cpp
Posts: 151
Joined: Mon Jun 02, 2008 1:55 pm
Company: MISRA

Re: Rule 0-1-6 Variables being given values that are never used.

Post by misra cpp » Wed Apr 22, 2020 5:16 pm

This is a DD anomaly, not DU, so is compliant with 0-1-6. However it ought to be regarded as having dead-code, and be reported by 0-1-9.

Unfortunately, the current wording of 0-1-9 which talks about 'removing statements' means that this isn't dead-code by 0-1-9's definition. We are looking at this for the next version
Posted by and on behalf of
the MISRA C++ Working Group

Post Reply

Return to “6.0 Language independent issues (C++)”