Skip to content

Seven Clang Warnings, and Three Bugs Still Got Through

Clang 17 gave 7 warnings at -Wall -Wextra on an 88-line C++20 file and missed 3 of 6 planted bugs. Which flags found them, and in what order to promote.

· · 8 min read

Two posts on this blog already ran -Wall -Wextra -Werror against old C++ and old C. One compared GCC and Clang on old C++, the other worked out per-flag promotion for legacy C. Both stopped at the edge of -Wextra. This one starts there, because that's where an owner of the tree should get nervous: the build is green, the gate says strict, and the flags that find the quiet bugs were never switched on.

What did I compile, and on what?

One file, 88 lines including comments, written to look like a long-lived tree. I planted six defects that change behavior at runtime, plus three constructs that C++20 deprecates. One of the three is also a defect, so the file holds eight things to find. It's a specimen, not a survey, so the counts below tell you what these flags can see, not how often they'd fire on your code. The file itself isn't published here, so treat the exact counts as one run on one file. The individual behaviors don't depend on it: a short file with an integer truncation, a comma subscript and a shadowed member shows the same flags, the same C++23 error text and the same sanitizer messages on Apple clang 17.

Environment: Apple clang 17.0.0 (clang-1700.6.3.2) on macOS 26.6.2, arm64. Only that compiler. I didn't run GCC, so everything I say about GCC comes from its manual, and I'll mark it when I do.

The six defects, by what they do when the program runs:

  • Circle::scale(int) hides the virtual Shape::scale(double), so a call through a Shape* quietly reaches the base version.
  • Shape has virtual functions and a non-virtual destructor, and main deletes a Circle through a Shape*.
  • Stats::record declares a local total that shadows the member, so the member never changes.
  • clamp_len assigns an int64_t to an int.
  • pack assigns level * 3 to an unsigned char.
  • grid[row, col] is a comma expression, not a 2D index.

What did -Wall -Wextra say at -std=c++20?

Seven warnings. Here's every one, with my verdict on whether it points at a bug.

Line Flag Bug?
20 -Woverloaded-virtual Yes, the hidden scale
36 -Wdeprecated-this-capture No, [=] still captures this
42 -Wsign-compare No, the loop bound is a small vector
59 -Wdeprecated-comma-subscript Yes, same line as the next one
59 -Wunused-value Yes, "left operand of comma operator has no effect"
64 -Wdeprecated-volatile No, counter++ on a volatile still works
75 -Wdelete-non-abstract-non-virtual-dtor Yes, the delete through the base pointer

Three of the six runtime defects are in that table: the hidden virtual, the comma subscript and the delete. The other three aren't. Nothing at -Wall -Wextra mentions the shadowed member or either truncation.

What do they do? I built the file with warnings silenced and ran it. The program printed the area as 12.566360. Had scale(2.0) reached Circle, the radius would have doubled and the area would be about 50.27. The second line printed 112 705032704 44 3 1 1.166667 legacy, and every number in it after the first is wrong or suspicious:

  • clamp_len(5000000000) returned 705,032,704.
  • pack(100) returned 44, because 300 doesn't fit in eight bits.
  • grid_at(grid, 1, 2) returned 3, which is grid[2]. The comma threw away the row.
  • make_counter()() returned 1, where I'd meant 6. The member total was still 0.

None of those crashed. That's the point of this whole genre of bug.

What does the next ring of flags add?

I added flags one at a time on top of -std=c++20 -Wall -Wextra and counted new warnings.

Added flag New warnings What they were
-Wshadow 1 The shadowed total. A real bug
-Wconversion 3 -Wshorten-64-to-32 and -Wimplicit-int-conversion, both real; -Wsign-conversion, benign here
-Wnon-virtual-dtor 2 Shape and Circle, one issue the delete warning already reported
-Wdouble-promotion 1 float to double in ratio(), harmless
-Wpedantic, -Wcast-qual, -Wformat=2, -Wnull-dereference, -Wold-style-cast 0 Nothing in this file for them to find

The zeros deserve a caveat. My file has no C-style casts, no const removal and no null pointers, so those flags measured nothing. A zero here is not evidence they're quiet on a real tree.

The useful rows are the first two. -Wshadow and -Wconversion between them account for the three defects the default strict set missed. Is that surprising? GCC's manual says the same shape holds there: I read its warning-options page, and neither flag appears in the lists under -Wall or -Wextra. For -Wnon-virtual-dtor, GCC's C++ page recommends -Wdelete-non-virtual-dtor, which -Wall enables, because it warns about the unsafe cases without false positives. That matches what I saw: the narrower flag found the bug, the broader one found the same bug twice.

-Wall -Wextra finds the bugs that look like mistakes. -Wshadow and -Wconversion find the ones that look like code.

SHARE Post on X Bluesky

What changes between C++17 and C++20?

I compiled the same file at four standards with -Wall -Wextra.

Standard Result
-std=c++14 4 warnings
-std=c++17 4 warnings
-std=c++20 7 warnings
-std=c++23 1 hard error, 5 warnings

The three extra warnings at C++20 are the deprecations: implicit this capture through [=], the comma in a subscript, and increment of a volatile. cppreference lists all three among the C++20 core-language changes, with the paper numbers P0806, P1161 and P1152.

At -std=c++23 the comma subscript stops being a warning at all. Clang printed type 'const int *' does not provide a subscript operator and refused to compile. I'd expect that, since C++23 gives a[i, j] a new meaning for types with a multi-argument operator[], but I verified the error, not the paper. The practical lesson is that -Wno-error=deprecated is a loan. Two of those three deprecations can be quietly carried for years. The comma subscript can't, and in my file it was also a bug.

That also exposes a trap in demoting by group. I ran the build with -Werror -Wno-error=deprecated and got four errors: the hidden virtual, the sign compare, the unused value and the delete. If I'd demoted unused-value too, to get the count under the "three tolerable errors" I'd been aiming for, I'd have demoted the one that reports the comma bug at C++20 and below.

What does a sanitizer see that these flags don't?

The two truncations never reach a compiler warning at -Wall -Wextra, and a plain undefined-behavior sanitizer doesn't see them either. I built with -fsanitize=undefined and ran the program: zero reports. Clang's documentation explains why. The undefined group excludes implicit-conversion, because those conversions aren't undefined behavior, only often unintentional. Name it explicitly:

clang++ -std=c++20 -g -fsanitize=implicit-conversion legacy.cpp -o legacy_ic
./legacy_ic

That printed two runtime errors, with the values: 5000000000 changed to 705032704, and 300 changed to 44. It's the clearest of the three tools here. It has a cost, since it only reports on paths the program takes, which is why it complements -Wconversion instead of replacing it.

In what order would I promote them?

Not -Werror for everything on day one. With it, this file fails with seven errors. Three of the seven aren't bugs, and two more are the same comma on the same line. Here's the order I'd use. The counts come from real compiles; the order is my opinion.

  1. Fix the comma subscript first. It's a bug at every standard and a hard error at C++23.
  2. Promote the ones that already found a bug. -Werror=overloaded-virtual, -Werror=delete-non-abstract-non-virtual-dtor, -Werror=unused-value.
  3. Add the ring flags as warnings first. -Wshadow, and -Wconversion with -Wno-sign-conversion so the benign hit stays quiet.
  4. Promote the two that found something. -Werror=shadow, -Werror=shorten-64-to-32, -Werror=implicit-int-conversion.
  5. Leave the rest as warnings and count them. -Wsign-compare, the three deprecations and -Wdouble-promotion stay visible without failing the build.

I ran steps 2 through 4 on the file before fixing anything. It produced six errors and four warnings. The six errors map onto the six bugs I'd planted: the hidden virtual, the delete, the comma, the shadowed member and both truncations. A reviewer wouldn't need to argue with any of them.

Where does this argument break?

It's one 88-line file that I wrote. I chose what to plant, so of course the next ring found it. I'd guess -Wconversion is the loudest flag on this list once it meets a real tree, but that's a guess, because I haven't measured it. A flag that's quiet on a specimen can bury a team in a million-line codebase. The answer to that is the per-flag count from the C article, not a refusal to try.

The second limit is the compiler. Apple's clang 17 isn't GCC 14, and the two don't agree on which flags exist or what they cover. I'd rerun this table on whichever compiler your CI uses before copying a single flag.

The third is more basic. I picked these defects because a compiler can see them. Nothing here says anything about the bugs that aren't syntax.

Which of these flags is off in your build right now? If you can't answer without opening the CMake file, that's the first thing to fix. For the older history of C++ tooling, see Debugging C++ on Linux.

Frequently Asked Questions

Does -Wall -Wextra include -Wshadow and -Wconversion?
No. On an 88-line C++20 file, Apple clang 17 gave 7 warnings at -Wall -Wextra and none for the shadowed member or the two integer truncations. Adding -Wshadow found the first, and -Wconversion found both truncations. GCC's manual does not list either flag under -Wall or -Wextra.
What happens to C++20 deprecation warnings under -Werror?
Each one becomes an error. In my file, implicit this capture through [=], increment of a volatile and a comma inside a subscript produced three of the seven errors. -Wno-error=deprecated demotes them, but the comma subscript is a hard compile error at -std=c++23 in Clang 17.
How can I catch integer truncation at runtime in Clang?
Build with -fsanitize=implicit-conversion. It is not part of -fsanitize=undefined, which reported nothing on my file. The explicit flag printed both truncations with their values: 5000000000 became 705032704, and 300 became 44.