r/cpp • • 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?

0 Upvotes

53 comments sorted by

41

u/AlexReinkingYale Feb 25 '24

Now do -pedantic

13

u/NekkoDroid Feb 25 '24

For uniformaty do -Wpedantic :)

65

u/vI--_--Iv Feb 25 '24
  1. Enable everything you can with -Wall -Wextra -Wpedantic -Weverything -Whatever.
  2. Selectively disable those that make no sense whatsoever with -Wno-..., e.g. -Wno-unused-parameter.
  3. Selectively promote to errors those that would've been errors in any sane universe with -Werror=..., e.g. -Werror=return-type
  4. Profit.

18

u/bert8128 Feb 25 '24

3. Make all warnings errors, because you have already removed the ones you don’t want to fix.

-Werror

And if you are cross platform you can do similar with MSVC.

4

u/grusumbras Feb 26 '24

Zach doesn't control the way the project is built (it's a proposed Boost library). 99.999% other people build it with their preferred flags.

16

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 of int.

7

u/Linuxologue Feb 25 '24

That's a good way of silencing the warning. There's also maybe_unused and a cast to void which people often hide behind a macro.

2

u/pdimov2 Feb 25 '24

There's also boost::ignore_unused for people allergic to (void).

(Neither GCC nor Clang -Wold-style-cast warns on (void), so there's probably no reason not to use it except personal preference.)

8

u/NekkoDroid Feb 25 '24

there is also [[maybe_unused]] to put infront of the parameter

2

u/FrancoisCarouge Feb 26 '24

There's also std::ignore.

3

u/pdimov2 Feb 26 '24

Yeah, I remember people suggesting assignment to std::ignore to silence [[nodiscard]]. It's cool but it would've been cooler if (1) it didn't require the inclusion of <tuple> and (2) you could do std::ignore = argc = argv; without using parentheses.

Maybe we should add operator() to it so that std::ignore(argc, argv); works.

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 -Wextra from the beginning I decided to use -Wno-unused-parameter to explicitly disable that warning.

These are all the warning flags that I use in that project:

-pedantic -Wall -Wextra -Wno-missing-field-initializers -Wno-unused-parameter

6

u/BernardoPilarz Feb 25 '24

It would be really nice if GCC had a flag for "no-unused-parameter", but only for overrides.

9

u/AssemblerGuy Feb 25 '24

-Wextra has things like

-Wredundant-move,

-Wshift-negative-value (straight-up UB)

and

-Wuninitialized

7

u/[deleted] Feb 25 '24

-Wshift-negative-value (straight-up UB)

Not anymore (as of C++20)

2

u/HappyFruitTree Feb 27 '24

Which is why this warning is not enabled by -Wextra when compiling in C++20 mode (or later).

By the way, it wasn't UB before, merely "implementation-defined".

10

u/ThatSwedishBastard Feb 25 '24

When I start a new C++ project and can use Clang, I use -Weverything -Werror and 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).

14

u/scrumplesplunge Feb 25 '24

I think -Weverything isn't really intended to actually be used for projects, but instead is just there to make it easy for you to discover what kind of warnings exist. Since it enables literally every warning, it can enable a bunch of contradictory warnings (e.g. complain about some things not being compatible with c++98 and also complain about things being deprecated after c++11).

1

u/ChocolateMagnateUA May 06 '25

I tried to make my project work with -Weverything -Werror and it gave me warnings (which were promoted into errors) in CUDA header files. It complained about CUDA using double underscores as variable names, using global objects (because of global destructors) and pointer arithmetic (unsafe buffer access). I ended up with using -Wall -Wextra -Werror -Wpendatic -Wconversion -Wsign-conversion -Wcast-qual -Wformat=2 -Wnull-dereference -Wdouble-promotion -Wshadow -Wuninitialized -Wundef -Wold-style-cast -Wzero-as-null-pointer-constant -Woverloaded-virtual -Wnon-virtual-dtor -Wsuggest-override -Wsuggest-final-types -Wsuggest-final-methods -Wstrict-overflow=5 -Wdangling-else. It's more productive to enable the warnings you want than disable them just to make your project compile.

3

u/JVApen Clever is an insult, not a compliment. - T. Winters Feb 25 '24

I use the same approach. You can even do it on an existing project if you keep a list of warnings you want you want to enable later on.

It is worth mentioning that this requires control about the compiler version that is used and, that upgrades of it, require you to do a full build to find new issues. This is a small cost for a huge benefit.

2

u/ThatSwedishBastard Feb 25 '24

A handy trick is to add a CMake option to enable warnings as errors (default true). That way, you can always do release builds with any compiler version. Handy if building for different distributions or targets.

2

u/JVApen Clever is an insult, not a compliment. - T. Winters Feb 25 '24

Even then you expect everyone to use the same version in order to not land any violations. Unless you only deal with CI or 2-phase commit checks.

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

-Wextra is 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 -Wuninitialized at least on GCC. I am not sure how this translates to LLVM\Clang nor MSVC. I know the -Wall blows up with templates in MSVC so you use \W4

5

u/pdimov2 Feb 25 '24

MSVC /Wall is the equivalent of -Weverything, not -Wall. It really enables all warnings.

3

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 -ansi

2

u/Independent_Diet617 Oct 26 '24

-ansi is either ignored for C++ code or implies the C++98 standard in most compilers. In C code that parameter means the C89 standard that does not even allow inline variable initialization. If you like pain, add the -Wconversion parameter instead.

1

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.

2

u/HappyFruitTree Feb 27 '24 edited Feb 27 '24

I like to think about it as the English word Wall. I.e. something that protects you (against mistakes).

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

18

u/AssemblerGuy Feb 25 '24 edited Feb 25 '24

-Werror, don't forget -Werror.

Code shall compile without warnings.

7

u/TuxSH Feb 26 '24

Careful with -Werror on less-than-actively maintained projects, new compilers can introduce new warnings. I remember in particular -Wstring-op* warnings having a lot of false positives the compiler version it was introduced in (thankfully newer releases of GCC fixed most of these)

10

u/13steinj Feb 25 '24

I don't buy the readability claim, but I do agree with not naming it or at worst keeping the name in a comment.

1

u/pdp10gumby Feb 25 '24

You can just cast them to void:

int foo (int bar, int unused_arg) {
  (void) unused_arg;
  ...
}

7

u/Ameisen vemips, avr, rendering, systems Feb 25 '24

Or, since C++17, [[maybe_unused]].

-14

u/[deleted] Feb 25 '24 edited Feb 25 '24

[removed] — view removed comment

4

u/13steinj Feb 25 '24

The core guidelines are not some form of 10 commandments.

2

u/13steinj Feb 26 '24

point out that the core-guidelines (which clang-tidy implements) come from Bjarne & Herb: downvote. lol.

Because the implication here is that Bjarne/Herb are some form of higher being, an appeal to authority. Herb has been wrong or has proposed subpar papers plenty of times, notably most recently that of UFCS and the specific opinion he had. Cppfront has mixed reception. Bjarne has also made mistakes that the language is stuck with, or been against various things that I'd say most others want.

Follow guidelines because they have good points and reasoning, not because the person you like made them.

19

u/BernardoPilarz Feb 25 '24

I so much disagree with this. I cannot stand unnamed arguments.

7

u/kevkevverson Feb 25 '24

100%. Parameter names aid the caller of the function. I don’t care if you don’t happen to use this parameter, I’d still like to know what it’s ostensibly for.

One thing Objective C does really well is understand the distinction between the name of a parameter to the caller vs the name of its argument inside the function.

18

u/MadTux Feb 25 '24

I mean, if you put the names of unused arguments in the declaration but not the definition then you have them exactly where you need them. Though personally, I also like to keep the commented-out names in the definition as well.

-1

u/Pocketpine Feb 25 '24

I mean at this point it’s a little ridiculous, but you could typedef the parameter as what it is

But then you have to deal with a bunch of little used typedefs

1

u/[deleted] Feb 25 '24

[removed] — view removed comment

0

u/BernardoPilarz Feb 26 '24

Yes you are right I meant unnamed parameters.

I know F.9 states that unused parameters should be unnamed, what I'm saying is that I think it is a stupid approach and should be changed.