﻿ V6007. Expression is always true/false. # V6007. Expression is always true/false.

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:

 CWE-570CWE-571CERT-MSC57-J
 You can look at examples of errors detected by the V6007 diagnostic.

427
14 526

### Do you make errors in the code?

Check your code
with PVS-Studio

Static code analysis
for C, C++, C# and Java This website uses cookies and other technology to provide you a more personalized experience. By continuing the view of our web-pages you accept the terms of using these files. If you don't want your personal data to be processed, please, leave this site. Learn More →
{"ft_lang_label":"__label__en","ft_lang_prob":0.87299705,"math_prob":0.8076059,"size":3012,"snap":"2021-04-2021-17","text_gpt3_token_len":670,"char_repetition_ratio":0.11303192,"word_repetition_ratio":0.064,"special_character_ratio":0.25232404,"punctuation_ratio":0.19086461,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9647855,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-04-18T19:41:58Z\",\"WARC-Record-ID\":\"<urn:uuid:560f7d4d-5441-4790-bfd7-b921bf12a651>\",\"Content-Length\":\"75703\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:644c6ca8-722b-446d-acee-7d1240f96344>\",\"WARC-Concurrent-To\":\"<urn:uuid:c20ea223-e4d7-4061-a80a-2ce34901874d>\",\"WARC-IP-Address\":\"173.0.157.39\",\"WARC-Target-URI\":\"https://pvs-studio.com/en/w/v6007/\",\"WARC-Payload-Digest\":\"sha1:TV65TZ2J3OYIIJAWEPB57DU4U6722ICT\",\"WARC-Block-Digest\":\"sha1:VUCCSGRK6FEVRGUUDANMLO5KROL4T5MF\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-17/CC-MAIN-2021-17_segments_1618038860318.63_warc_CC-MAIN-20210418194009-20210418224009-00224.warc.gz\"}"}