Our website uses cookies to enhance your browsing experience.
Accept
to the top
close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
** By clicking this button you agree to our Privacy Policy statement
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
* By clicking this button you agree to our Privacy Policy statement

close form
Free PVS‑Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

close form
To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

close form
I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

Your message has been sent. We will email you at


If you do not see the email in your inbox, please check if it is filtered to one of the following folders:

  • Promotion
  • Updates
  • Spam

Webinar: Evaluation - 05.12

>
>
>
V6007. Expression is always true/false.
menu mobile close menu
Analyzer diagnostics
General Analysis (C++)
General Analysis (C#)
General Analysis (Java)
Micro-Optimizations (C++)
Diagnosis of 64-bit errors (Viva64, C++)
Customer specific requests (C++)
MISRA errors
AUTOSAR errors
OWASP errors (C++)
OWASP errors (C#)
Problems related to code analyzer
Additional information
toggle menu Contents

V6007. Expression is always true/false.

May 14 2018

The analyzer detects potentially incorrect conditional expressions, which are always true/false when calculating.

Case 1.

Incorrectly formatted condition is always true or false.

Such conditions do not necessarily indicate a bug, but they need reviewing.

Example of incorrect code:

String str = ...;
if (!str.equals("#") || !str.isEmpty()){
    ...
} else {
    ...
}

The else branch in this code will never be executed because regardless of what value the 'str' variable refers to, one of the two comparisons with a string will always be true. To fix this error, we need to use operator && instead of ||. This is the fixed version of the code:

This is the fixed version of the code:

String str = ...;
if (!str.equals("#") && !str.isEmpty()){
    ...
} else {
    ...
}

Case 2.

Two conditional successive operators contain mutex conditions.

Examples of mutex conditions:

  • "A == B" and "A != B";
  • "A > B" and "A <= B";
  • "A < B" and "B < A";
  • and so on.

This error can occur as a result of a typo or bad refactoring.

Consider the following example of incorrect code:

if (x == y)
  if (y != x)
    DoSomething(x, y);

In this fragment, the 'DoSomething' method will never be called because the second condition will always be false when the first one is true. One of the variables used in the comparison is probably wrong. In the second condition, for example, variable 'z' should have been used instead of 'x':

if (x == y)
  if (y != z)
    DoSomething(x, y);

Case 3.

A longer and shorter string is being searched in the expression. With that, the shorter string is a part of the longer one. In the result, one of the comparisons is redundant or contains an error.

Consider the following example:

if (str.contains("abc") || str.contains("abcd"))

If substring "abc" is found, the check will not execute any further. If substring "abc" is not found, then searching for longer substring "abcd" does not make sense either.

To fix this error, we need to make sure that the substrings were defined correctly or delete extra checks, for example:

if (str.contains("abc"))

Here's another example:

if (str.contains("abc"))
  Foo1();
else if (str.contains("abcd"))
  Foo2();

In this code, function 'Foo2' will never be called. We can fix the error by reversing the check order to make the program search for the longer substring first and then search for the shorter one:

if (str.contains("abcd"))
  Foo2();
else if (str.contains("abc"))
  Foo1();

This diagnostic is classified as:

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