Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a GCC option to warn about writing `this-field` instead of `this->field`?

This following code (containing a vicious bug) compiles with GCC without any warning. But, of course, it doesn't work as expected by the developer (me).

#include <iostream>  struct A {     bool b;     void set(bool b_) { this->b = b_; }     bool get() const { return this-b; } // The bug is here: '-' instead of '->' };  int main() {     A a;     a.set(true);     std::cout << a.get() << std::endl; // Print 1     a.set(false);     std::cout << a.get() << std::endl; // Print 1 too...     return 0; } 

Which warning can I add for the compiler (GCC 4.8) to avoid this kind of typo?

Linked question: Is there any option to force (or warn) the access to member variables/functions with this->?

like image 952
Caduchon Avatar asked Sep 20 '17 10:09

Caduchon


People also ask

How do I enable warnings in GCC?

GCC 4.3+ now has -Q --help=warnings , and you can even specify --help=warnings,C to just print out the C related warnings.

Which option can be used to display compiler warnings?

The warning message for each controllable warning includes the option that controls the warning. That option can then be used with -Werror= and -Wno-error= as described above. (Printing of the option in the warning message can be disabled using the -fno-diagnostics-show-option flag.)

Which option of GCC inhibit all warning messages?

If -Wfatal-errors is also specified, then -Wfatal-errors takes precedence over this option. Inhibit all warning messages. Make all warnings into errors. Make the specified warning into an error.

Which GCC flag is used to enable all compiler warnings?

gcc -Wall enables all compiler's warning messages. This option should always be used, in order to generate better code.


1 Answers

This particular issue is detected by cppcheck:

 $ cppcheck --enable=all this-minus-bool.cxx  Checking this-minus-bool.cxx... [this-minus-bool.cxx:7]: (warning) Suspicious pointer subtraction. Did you intend to write '->'? (information) Cppcheck cannot find all the include files (use --check-config for details) 

This was with no include path given. If I add -I /usr/include/c++/4.8/, the issue is still detected:

 Checking this-minus-bool.cxx... [this-minus-bool.cxx]: (information) Too many #ifdef configurations - cppcheck only checks 12 of 45 configurations. Use --force to check all configurations. [this-minus-bool.cxx:7]: (warning) Suspicious pointer subtraction. Did you intend to write '->'? [/usr/include/c++/4.8/bits/ostream.tcc:335]: (style) Struct '__ptr_guard' has a constructor with 1 argument that is not explicit. [/usr/include/c++/4.8/bits/locale_classes.tcc:248]: (error) Deallocating a deallocated pointer: __c 

and then cppcheck slowly works through the aforementioned #ifdef configurations.

(As a side note, the error in local_classes.tcc is a false positive but this is very hard to tell for an automated tool, as it would need to be aware that the catch block at this site should not be entered when the macro __EXCEPTIONS is unset.)

Disclaimer: I have no other experience with cppcheck.

like image 131
Arne Vogel Avatar answered Oct 17 '22 22:10

Arne Vogel