Why did one file get two different verdicts?
I keep hearing the same plan from C++ teams with an old tree: add -Wall -Wextra -Werror to the build, fix whatever turns red, done. So I wrote a file that looks like what those trees contain and ran the plan against it. 57 lines, 2004-style C++: a raw char name[32] with strcpy, a hand-rolled container holding Record**, std::auto_ptr, a register local, an int v; that only gets assigned on one branch, a switch with a missing break, sprintf into an 8-byte buffer, and a delete where delete[] was meant. I planted every one of those on purpose, so this is a specimen, not a survey. Treat the numbers that way.
Then I compiled it with GCC 13.3.0 and Clang 18.1.3, both at -std=c++17 -Wall -Wextra, and counted.
| Compiler | Warnings | Which flags |
|---|---|---|
| GCC 13.3 | 5 | sign-compare (2), register, deprecated-declarations, implicit-fallthrough |
| Clang 18.1 | 7 | sign-compare (2), sometimes-uninitialized (2), register, deprecated-declarations, mismatched-new-delete |
Add -Werror and GCC fails the build with 5 errors, Clang with 7. Fine. But look at what each one is failing on.
Which warnings were actually bugs?
Of the defects I planted, the file has four that change behaviour at runtime: the uninitialized v, the delete on a new[] array, the fall-through, and the sprintf overflow. (The strcpy is the same class as the sprintf.)
Clang caught two: -Wsometimes-uninitialized on v, twice, once per branch that skips the assignment, and -Wmismatched-new-delete on the destructor. GCC caught one, -Wimplicit-fallthrough, and only because -Wextra is on; at plain -Wall it reports four warnings and none of them is a bug. Neither compiler said a word about sprintf writing an argument of unknown length into char line[8]. That's not a criticism of either; it's a fact about what a compiler warning is and isn't.
So here's the uncomfortable arithmetic. Under -Wall -Wextra -Werror, GCC breaks the build on five things, one of which matters. Clang breaks it on seven, two of which matter. Both of them break it on register and on std::auto_ptr, which are not bugs at all. They're 2004 C++ being judged by 2017 rules.
Isn't that what you wanted, though? Maybe. Keep reading.
Was it the standard that broke, not the code?
I recompiled the same file at -std=c++98, the standard it was written for. GCC at -Wall -Wextra drops to three warnings: the two sign-compares and the fall-through. Clang drops to five: sign-compare, the two uninitialized reads, and the mismatched delete. The register warning and the auto_ptr deprecation vanish on both, because at C++98 neither is deprecated.
In other words, two of the five things that would have turned GCC's build red are there because somebody changed -std=. The code didn't move. The ruler did.
That matters for how you sequence the work. A tree that's been on -std=gnu++98 for twenty years and gets -std=c++17 -Werror in the same commit is going to fail on syntax nobody considers a defect, and the fastest fix is -Wno-error=register -Wno-error=deprecated-declarations. I tried exactly that on GCC: -Werror -Wno-error=sign-compare -Wno-error=deprecated-declarations -Wno-error=register leaves one error, the fall-through. Which, for this file, is the right one to be left with. I've done this on real trees, and the standard bump and the warning bump should never share a commit; each has its own list of casualties.
Under -Werror, two compilers failed the same C++ file for different reasons, and each one missed a bug the other caught.
Does clang-tidy fill the gap?
Partly, and the way it fills it tells you how to use it.
With its default check set, clang-tidy on the file at -std=c++17 reported three findings, and all three were clang-diagnostic-*, which is to say the compiler's own warnings relayed through a different front door. Adding --warnings-as-errors='*' made it exit non-zero. No new information, just a second place for the same verdict.
Turn on real check groups and it changes. With bugprone-*,modernize-*,cert-*,cppcoreguidelines-* the count went to 33 on 57 lines. A few of them are the bugs:
cppcoreguidelines-init-variablesonint v;bugprone-switch-missing-default-case, adjacent to the fall-through if not the same thingcppcoreguidelines-owning-memory, twice, pointing at the rawnewthat leads to the mismatched deletecert-err33-con an ignoredprintfreturn, which I'd call noise in this file and real in a logging library
The rest are style: four modernize-use-trailing-return-type, four cppcoreguidelines-pro-type-vararg for the printf calls, three magic-number complaints, two C-array complaints. With -checks='*' the total hit 73. Seventy-three findings on a file with four bugs. If you promote the wildcard to errors you've built a gate that's 95 percent formatting opinion, and I've written before about what happens when a linter's job gets confused with a test's.
GCC's analyzer is a different tool with a different flag
One more run, because it changes the picture for GCC users. g++ -fanalyzer on the same file reported what -Wall -Wextra didn't: -Wanalyzer-use-of-uninitialized-value twice, -Wanalyzer-mismatching-deallocation once, plus leak and null-dereference findings around the hand-rolled container. So GCC can see the two bugs Clang saw. It just doesn't look unless you ask with a separate flag that isn't part of any -W group and costs real compile time.
Which leaves the sprintf. Nothing in this article's runs flagged it. Sanitizers would at runtime, -D_FORTIFY_SOURCE would at runtime, a reviewer would at a glance. A gate that's only a compiler flag has a ceiling, and it's lower than the confidence -Werror tends to project.
What should -Werror promote, then?
My rule, and it's one you can argue with: promote named warnings, never the group, and promote the ones that have found a bug in your tree in the last year. For the older history of C++ tooling, see Debugging C++ on Linux.
For this file, on Clang, that's two flags:
clang++ -std=c++17 -Wall -Wextra \
-Werror=sometimes-uninitialized \
-Werror=mismatched-new-delete \
-c legacy.cpp
That exits 1 on the two real defects and lets the five style warnings scroll past as warnings, where a human or a weekly count can deal with them. On GCC the equivalent is -Werror=implicit-fallthrough, plus -fanalyzer in a nightly job rather than in every compile. Then widen one flag at a time, with a count of what each promotion breaks, the way I described for a legacy C tree. The C and C++ stories are the same story; the C++ version just has more deprecated syntax to trip over.
What I'd avoid: a blanket -Werror on a tree you didn't write, turned on in the same week as a standard bump, with the intention of "fixing whatever's red". On this file that's 12 errors across two compilers for four bugs, and four of the twelve are complaints about the year the code was written. The build goes red, the team adds -Wno-error for the loudest flag, and the one flag that mattered rides along in the exception.
Which of your compiler's warnings has caught a real bug this year? If you can name it, promote that one. If you can't, you don't have the data to promote any of them yet.