[previous] 3424 [next] Statement contains a redundant & or | at top level.
Redundancy

This bitwise '&' or | operator is redundant, but perhaps a logical rather than a bitwise operation was intended ?.

If an operation generates no side effects and the result is discarded, the operation can usually be considered redundant. QA·C identifies this situation with message 3422. However if the operator happens to be a logical && or logical ||, the situation is a little different.

A logical '&&' or || operator is sometimes used as a shorthand way of implementing a chain of operations where successive operations are only performed when a previous condition is fulfilled. The technique relies on the fact that the right hand operand of a logical && operator is only evaluated if the left hand operand is non-zero, and the right hand operand of a logical || operator is only evaluated if the left hand operand is zero.

For example:


/*PRQA S 584,3103,3120,3210,3398,3408,3415,3416,3440,3447,4130,4131 ++ */

extern int a;
extern int b;

extern int bar(int n);

extern void foo(void)
{
    a +  b;                 /* Message 3112 */ /* No side effect */
    a &  b;                 /* Message 3112 */ /* No side effect */
    a |  b;                 /* Message 3112 */ /* No side effect */
    a && b;                 /* Message 3112 */ /* No side effect */
    a || b;                 /* Message 3112 */ /* No side effect */

    a +  bar(1);            /* Message 3422 */ /* Redundant operation */
    a -  bar(1);            /* Message 3422 */ /* Redundant operation */
    a *  bar(1);            /* Message 3422 */ /* Redundant operation */
    a /  bar(1);            /* Message 3422 */ /* Redundant operation */
    a %  bar(1);            /* Message 3422 */ /* Redundant operation */
    a ^  bar(1);            /* Message 3422 */ /* Redundant operation */
    a << bar(1);            /* Message 3422 */ /* Redundant operation */
    a == bar(1);            /* Message 3422 */ /* Redundant operation */

    a &  bar(1);            /* Message 3424 */ /* Redundant operation */
    a &  bar(1) & bar(2);   /* Message 3424 */ /* Redundant operation */
    a |  bar(1);            /* Message 3424 */ /* Redundant operation */
    a |  bar(1) | bar(2);   /* Message 3424 */ /* Redundant operation */

    a && bar(1) && bar(2);  /*              */
    a || bar(1) || bar(2);  /*              */
}

See also:

QA·C Source Code Analyser 8.1.2
© 2013 Programming Research.
www.programmingresearch.com
Personality Groups | Glossary | Message Index Contents