Main Content

MISRA C:2012 Rule 17.7

The value returned by a function having non-void return type shall be used

Description

Rule Definition

The value returned by a function having non-void return type shall be used.

Rationale

You can unintentionally call a function with a non-void return type but not use the return value. Because the compiler allows the call, you might not catch the omission. This rule forbids calls to a non-void function where the return value is not used. If you do not intend to use the return value of a function, explicitly cast the return value to void.

Polyspace Implementation

The checker flags functions with non-void return if the return value is not used or not explicitly cast to a void type.

The checker does not flag the functions memcpy, memset, memmove, strcpy, strncpy, strcat, strncat because these functions simply return a pointer to their first arguments.

Troubleshooting

If you expect a rule violation but do not see it, refer to Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

unsigned int cutOff(unsigned int val) {
    if (val > 10 && val < 100) {
        return val;
    }
    else {
        return 0;
    }
}

unsigned int getVal(void);

void func2(void) {
    unsigned int val = getVal(), res;
    cutOff(val);           /* Non-compliant */
    res = cutOff(val);     /* Compliant */
    (void)cutOff(val);     /* Compliant */
}

In this example, the rule is violated when the return value of cutOff is not used subsequently.

The rule is not violated when the return value is:

  • Assigned to another variable.

  • Explicitly cast to void.

Check Information

Group: Function
Category: Required
AGC Category: Readability

Version History

Introduced in R2014b