Warnings as errors is a compiler configuration that prevents builds from succeeding while diagnostic warnings remain. It turns cleanup into a practical guard against accumulating technical debt. section 12.10
A warning-heavy program
A small C++ program triggers warnings for NULL arithmetic, unused variables and parameters, an unused label, and an uninitialized pointer. Treating these warnings as errors stops the build until the code is corrected. The cleaned version initializes its data, removes misleading code, uses 0 instead of NULL, and uses the computed result. section 12.10
warning-bad.cpp: In function 'int compute(int*, int)':
warning-bad.cpp:6:14: warning: NULL used in arithmetic [-Wpointer-arith]
6 | if (*x > NULL) #1
| ^~~~
warning-bad.cpp:5:10: warning: unused variable 'pos' [-Wunused-variable]
5 | int* pos = x;
| ^~~
warning-bad.cpp:4:25: warning: unused parameter 'y' [-Wunused-parameter]
4 | int compute(int* x, int y) {
| ~~~~^
warning-bad.cpp: In function 'int main()':
warning-bad.cpp:13:9: warning: unused variable 'res' [-Wunused-variable]
13 | int res = compute(x, 0); #2
| ^~~
warning-bad.cpp:14:5: warning: label 'std' defined but not used [-Wunused-
label]
14 | std:cout << "hello, world\n";
| ^~~
warning-bad.cpp:13:22: warning: 'x' is used uninitialized [-Wuninitialized]
13 | int res = compute(x, 0); #3
| ~~~~~~~^~~~~~
Inspect the compiler diagnostics that expose multiple problems in the example program. section 12.10
Apply the setting cautiously:
- Clean existing warnings before enabling the strict setting when possible. section 12.10
- Otherwise enable additional warnings one at a time and fix them progressively. section 12.10
- Use the highest warning level the team can handle without creating alert fatigue. section 12.10
