V728. An excessive check can be simplified. The '||' operator is surrounded by opposite expressions 'x' and '!x'.

The analyzer has detected code that can be simplified. The left and right operands of the '||' operation are expressions with opposite meanings. This code is redundant and can be simplified by reducing the number of checks.

Here's an example of redundant code:

if (!Name || (Name && Name[0] == 0))

In the "Name && Name[0] == 0" expression, the 'Name' check is excessive because before it, the expression '!Name', which is opposite to it, is checked, these expressions being separated by the '||' operation. Consequently, the excessive check in the parentheses can be omitted to simplify the code:

if (!Name || Name[0] == 0)

Redundancy may indicate there is an error in the code: it might be that a wrong variable is used in the expression, so the correct version of the code should really look something like this:

if (!Foo || (Name && Name[0] == 0))

The analyzer outputs this warning not only for 'x' and '!x' constructs, but for other expressions with opposite meanings as well. For example:

if (a > 5 || (a <= 5 && b))

According to Common Weakness Enumeration, potential errors found by using this diagnostic are classified as CWE-570.

You can look at examples of errors detected by the V728 diagnostic.

Do you make errors in the code?

Check your code
with PVS-Studio

Static code analysis
for C, C++, and C#

goto PVS-Studio;
We use cookies for the analysis of events to improve our content and make user interaction more convenient. By continuing the view of our web-pages you accept the terms of using these files. You can find out more about cookie-files and privacy policy or close the notification, by clicking on the button. Learn More →
Do not show