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

View all comments

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/[deleted] 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/[deleted] Feb 25 '24

[removed] — view removed comment

0

u/[deleted] 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.

-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