2
typedef struct A {} A;
typedef struct B {} B;

void doStuff(A* pA) {};

int main() {
   B b;
   doStuff(&b);
}

This code compiles (albeit with a warning). Is there any way (a compiler option, or perhaps by changing the definition of doStuff) for it not to compile?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
RcnSc
  • 128
  • 5

1 Answers1

3

Edit: you can make specific warnings being treated as errors in GCC/Clang, with this flag: -Werror=<warning name>.

You can treat warnings as errors in GCC (or Clang), by using the -Werror flag. Other compilers have their own respective flags.

Then, you would get something like this:

prog.c: In function 'doStuff':
prog.c:4:17: error: unused parameter 'pA' [-Werror=unused-parameter]
    4 | void doStuff(A* pA) {};
      |              ~~~^~
prog.c: In function 'main':
prog.c:8:12: error: passing argument 1 of 'doStuff' from incompatible pointer type [-Werror=incompatible-pointer-types]
    8 |    doStuff(&b);
      |            ^~
      |            |
      |            B * {aka struct B *}
prog.c:4:17: note: expected 'A *' {aka 'struct A *'} but argument is of type 'B *' {aka 'struct B *'}
    4 | void doStuff(A* pA) {};
      |              ~~~^~
cc1: all warnings being treated as errors

Live Demo

gsamaras
  • 71,951
  • 46
  • 188
  • 305