![]() |
![]() |
3424 | ![]() |
||||
![]() | |||||||
| 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. QAC 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); /* */
}
No MISRA C:2012 Rules applicable to message 3424
See also:
![]() | ||
| QA·C Source Code Analyser 8.1
MISRA C:2012 Compliance Module 1.0 © 2012 Programming Research. www.programmingresearch.com | Personality Groups | Glossary | Message Index | MISRA C:2012 Rule Index | Contents |