liveBook · Manning

Warnings as errors

As explained in 100 C++ Mistakes and How to Avoid Them

Warnings as errors is a compiler configuration that treats diagnostic warnings as build-blocking errors.

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

How 100 C++ Mistakes and How to Avoid Them covers it

How deep it goes
Explains the configuration, warning examples, rollout choices, and alert-fatigue trade-off without detailing compiler-specific commands.
Examples
Uses C++ diagnostics for NULL arithmetic, unused code, an uninitialized pointer, and a cleaned-up replacement program.
What it assumes
Assumes basic compiler use and C++ code reading while teaching warning cleanup from concrete mistakes.
What you can do after
Operate/configure a warning policy, clean diagnostics incrementally, and prevent new warnings from entering builds.
How it connects
Connects compiler warnings with technical debt, static code analysis, uninitialized variables, and incremental remediation.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 C++ Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.