[previous] 0757 [next] [C] 'return' expression points to a more heavily qualified type.
Constraint violations REFERENCE - ISO:C90-6.6.6.4 The return Statement - Semantics, ISO:C90-6.3.16.1 Simple Assignment - Constraints

This is a constraint error

This function is defined with a return type of pointer type but has a return expression which points to a more heavily qualified type.

In an assignment operation, the type pointed to by the object on the left of the assignment operator must have all the type qualifiers of the type pointed to by the expression on the right. The same principle applies to a return expression. It is not permissible to return an expression of type "const TYPE *" (pointer to const TYPE) or "volatile TYPE *" (pointer to volatile TYPE) in a function defined with a return type of "TYPE *" (pointer to TYPE); to do so would permit the type qualification restrictions to be violated.

On the other hand, it is perfectly legitimate to return an expression of type "TYPE *" in a function with a return type of "const TYPE *" or "volatile TYPE *" or even "const volatile TYPE *".

For example:


/*PRQA S 2006,2214,3120,3132,3227,3408,3447,3602 ++ */

extern int                *gpi;
extern const int          *gpci;
extern volatile int       *gpvi;
extern const volatile int *gpcvi;


int * fpi(int mode)
{
    if      (mode == 0)        return(gpi);         /*              */
    else if (mode == 1)        return(gpci);        /* Message 0757 */
    else if (mode == 2)        return(gpvi);        /* Message 0757 */
    else                       return(gpcvi);       /* Message 0757 */
}


const int * fpci(int mode)
{
    if      (mode == 0)        return(gpi);         /*              */
    else if (mode == 1)        return(gpci);        /*              */
    else if (mode == 2)        return(gpvi);        /* Message 0757 */
    else                       return(gpcvi);       /* Message 0757 */
}

volatile int * fpvi(int mode)
{
    if      (mode == 0)        return(gpi);         /*              */
    else if (mode == 1)        return(gpci);        /* Message 0757 */
    else if (mode == 2)        return(gpvi);        /*              */
    else                       return(gpcvi);       /* Message 0757 */
}

const volatile int * fpcvi(int mode)
{
    if      (mode == 0)        return(gpi);         /*              */
    else if (mode == 1)        return(gpci);        /*              */
    else if (mode == 2)        return(gpvi);        /*              */
    else                       return(gpcvi);       /*              */
}


MISRA-C:2004 Rules applicable to message 0757:

Rule  1.1  (Required) All code shall conform to ISO/IEC 9899:1990 C programming language, ISO 9899, amended and corrected by ISO/IEC 9899/COR1:1995, ISO/IEC 9899/AMD1:1995, and ISO/IEC 9899/COR2: 1996192


See also:

QA·C Source Code Analyser 8.1
MISRA-C:2004 Compliance Module 3.2
© 2012 Programming Research.
www.programmingresearch.com
Personality Groups | Glossary | Message Index | MISRA-C:2004 Rule Index Contents