简体   繁体   中英

Using cpplint to find typos in variable initialization

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM