V770. Possible use of a left shift operator instead of a comparison operator.


The analyzer detected a potential typo that deals with using operators '<<' and '<<=' instead of '<' and '<=', respectively, in a loop condition.

Consider the following example:

void Foo(std::vector<int> vec)
{
  for (size_t i = 0; i << vec.size(); i++) // <=
  {
    // Something
  }
}

The "i << vec.size()" expression evaluates to zero, which is obviously an error because the loop body will not execute even once. Fixed code:

void Foo(std::vector<int> vec)
{
  for (size_t i = 0; i < vec.size(); i++) 
  {
    // Something
  }
}

Note. Using right-shift operations (>>, >>=) is considered a normal situation, as they are used in various algorithms, for example computing the number of bits with the value 1, for example:

size_t num;
unsigned short var = N;
for (num = var & 1 ; var >>= 1; num += var & 1);

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


Bugs Found

Checked Projects
354
Collected Errors
13 290