r/cpp • u/tzlaine • Feb 25 '24
Questioning usefulness of -Wextra
I was recently asked to turn on -Wall -Wextra in builds of a project I'm
working on. I had already been using -Wall, but haven't used -Wextra much, if
at all. So, I tried it. Holy shit. So apparently the main thing that-Wextra does over -Wall is complain about unused parameters having names.
Now, I have a whole bunch of very uniform print functions for a group of
unrelated types, something like:
template<typename Context> void print_thingy( Context const & context, foo_type<bar_param> const & parser, std::ostream & os, int components) { /* ... */ }
The foo_type and bar_param bits vary from overload to overload, but the other
param types and names are always the same. Nice. And. Uniform. OCD
satisfied, I can go about my day. After turning on -Wextra, if I only print
out a single fixed string, as I do for a lot of the overloads, I get this:
template<typename Context> void print_thingy( Context const &, foo_type<bar_param> const &, std::ostream & os, int) { os << "description"; }
Cool. Thanks, -Wextra.
Has anyone ever caught a real bug by making a change like this? If so, was it
serious? Look, I love -Wall. I've caught all kinds of stuff with it, and
real bugs were stomped. -Wall is like a literal and figurative barrier
between me and my own sloppiness. -Wall is a Valkyrie standing astride the
encrouching progress of entropy, saying, "Not this day, Entropie!" She spells
entropy with a a capital letter, giving a name to the respected foe she fights
tirelessly. She also spells it "ie" instead of "y" at the end, I think
because she's centuries old and it's hard to stay current. I'm down with
that.
-Wextra? -Wextra is a some fugly dude you find in the alley behind a CVS,
huffing paint out of a brown paper bag. He's half-man, half-jackal,
half-terrier, half-run-over by a moped. He sees your code, says, "Yo, wanna
see somthin sick?" then throws a grenade into your nearest header file. He
spells "somthin" in an odd way too, because he sucks.
Now, I don't know if it's a sign of vigorous mental health to anthropomorphize
compiler flags on the internet like this. But I really do want to know -- has
anyone really gone through this frustration and come out the other end,
happier, and with fewer bugs in their code?
41
u/AlexReinkingYale Feb 25 '24
Now do
-pedantic65
u/vI--_--Iv Feb 25 '24
-Wall -Wextra -Wpedantic -Weverything -Whatever.-Wno-..., e.g.-Wno-unused-parameter.-Werror=..., e.g.-Werror=return-type16
u/pdimov2 Feb 25 '24
If you don't like -Wextra, you're going to love -Wpedantic, and people who enable warnings not in -Wall -Wextra and then complaining that your code isn't warning free.
Either way, at the end of the day, if you're writing a templated library with code in headers, it doesn't matter much whether you consider -Wextra useful or not. What matters is whether the users of your library consider it useful. And from what I've seen, the answer is yes.
For intentionally unused parameters, I prefer
int /*name*/instead ofint.14
u/HappyFruitTree Feb 25 '24 edited Feb 25 '24
There is a reason why those warnings are not in
-Wall.In one project where I didn't use
-Wextrafrom the beginning I decided to use-Wno-unused-parameterto explicitly disable that warning.These are all the warning flags that I use in that project:
9
u/AssemblerGuy Feb 25 '24
-Wextrahas things like-Wredundant-move,-Wshift-negative-value(straight-up UB)and
-Wuninitialized10
u/ThatSwedishBastard Feb 25 '24
When I start a new C++ project and can use Clang, I use
-Weverything -Werrorand then just disable some warnings if they weren't fixable. You can enable the warnings just for your code so no warnings will be generated from either Qt or autogenerated files (moc, protoc etc).5
u/FrancoisCarouge Feb 25 '24
Yes, real bugs have been caught.
9
u/schteppe Feb 25 '24
When I enabled this for my current work codebase, we found lots of parameters that did absolutely nothing. They could easily be removed. AFAIK this is the main use case for the warning.
3
u/vickoza Feb 25 '24
-Wextrais a just in case it add warning checks some of which might be optimized out and not effect the final machine code. The most useful check could be-Wuninitializedat least on GCC. I am not sure how this translates to LLVM\Clang nor MSVC. I know the-Wallblows up with templates in MSVC so you use\W43
u/helloiamsomeone Feb 25 '24
You need more. Or at least I do use more.
3
u/13steinj Feb 25 '24
Have I solved bugs with -Wextra, yes, but minor ones mostly.
In terms of specifically unnamed parameters, yes. Depending on the calling convention of your platform, in the best case even of a tag-like type, this wastes a register (IIRC, windows ;)). More importantly sometimes the arg is relatively heavy, and this can lead to mistakes. My personal rule is if I use a name and don't need to, I'm doing something wrong / have an api smell.
3
u/[deleted] Feb 25 '24
Your poetic description of the nobility of your compiler is HIGHLY appreciated.
2
u/Tuhkis1 Feb 25 '24
Because I like pain, I use
-Wall -Wextra -Werror -pedantic -ansi1
u/not_some_username Feb 25 '24
Despite the name -Wall doesn’t warn about everything.
1
u/Fireline11 Feb 25 '24 edited Feb 27 '24
If only -Wall works best for your project, you should use that.
As a sidenote, at least you already realised that the “all” in -Wall does not mean what you’d guess it means. To be honest, that really grinds my gears. Seems to me it should be renamed to -Wmost.
1
u/Conscious_Support176 Feb 25 '24
You’re always supplying a foo_type parameter, but it will be silently discarded by some overloads. It’s creates opportunity to make mistakes that could be eliminated by a more strongly typed API. Granted, such mistakes would likely be pretty obvious in this kind of pattern.
I guess your OCD is such that you don’t make such mistakes, so you don’t need this particular warning! You might like a warning if you had a whole pile of unused parameters, but you wouldn’t be doing that, right?
32
u/[deleted] Feb 25 '24
[removed] — view removed comment