Using cpplint to find typos in variable initialization

193 Views Asked by At

I want to check my code to find typos like this:

bool check;
check == true; // should be: check = true;

This is a valid code in C/C++, so I want to use cpplint to find code occurrences of this type.

What cpplint configuration should I use?

2

There are 2 best solutions below

1
Andrejs Cainikovs On BEST ANSWER

This typo indeed can be left unnoticed. I'd suggest you not to rely on default compiler configuration.

Assume following code:

int main()
{
        int check;
        check == 1;
}

When built with gcc main.c -o main the compiler will not produce any warnings at all. (Ubuntu 20.04.1, GCC 9.3.0).

However, when built with gcc main.c -o main -Wall:

main.c: In function ‘main’:
main.c:4:8: warning: statement with no effect [-Wunused-value]
    4 |  check == 1;
      |  ~~~~~~^~~~

You don't need cpplint for this kind of typos. That's an overkill.

0
gnasher729 On

That code will give me two warnings with my compiler settings, which at e both turned into errors.

One warning is for using the == operator without using the result, the other is for using the uninitialised variable check. And obviously any future use of “check” will give a warning again, until the compiler can proof that check is initialised.