News for hackers. Stuff that matters.
Re:Useful GCC warning options not enabled by -Wall -Wextra by minexew on Wednesday October 04, 2017 @01:21PM (#15402130)
My absolute favorite for modern C++ is

-Werror=switch

Using this, you can enforce that a switch statement over an enum value is complete (covers all cases) simply by omitting the default case.

Re:Useful GCC warning options not enabled by -Wall -Wextra by asveikau on Wednesday October 04, 2017 @02:27PM (#15402967)
You're not the first person I've seen suggest that this condition is worth flagging, but I don't completely understand why. People who conform to such a style then go on to add a "default" case that is a no-op, which I guess is more explicit, but also feels a little tedious and verbose. Switch statements in C can do all sorts of funky things (vide: Duff's device) so I think omitting a case label for an enum value feels like a much lesser crime. What if you just don't have meaningful things to do for that enum value?
Re:Useful GCC warning options not enabled by -Wall -Wextra by jlg23 on Wednesday October 04, 2017 @03:06PM (#15403437)
> I think omitting a case label for an enum value feels like a much lesser crime.

This is not about the coding police coming to get you but preventing you from shooting yourself into your feet when you extend the enumeration. Quite often you want to cover all cases and write code that signals an error if the default branch is called. CL even has a special version of the CASE macro (CL's "switch") called ECASE[1] with the only difference being that ECASE signals an error on unhandled cases.

[1] http://www.lispworks.com/documentation/HyperSpec/Body/m_case...

Re:Useful GCC warning options not enabled by -Wall -Wextra by balls187 on Wednesday October 04, 2017 @11:40PM (#15406558)
> This is not about the coding police coming to get you but preventing you from shooting yourself into your feet when you extend the enumeration.

This.

And not necessarily yourself, but whomever maintains the software after you.

Of course this doesn't prevent someone from adding a "default" label in some future iteration.

Re:Useful GCC warning options not enabled by -Wall -Wextra by asveikau on Thursday October 05, 2017 @12:10AM (#15406643)
I think you guys are missing something in my perplexed state at this construct. I'm not saying it out of lack of experience or because I disbelieve in the "going out of my way to make things more maintainable though verbosity" aspect. It's just that many times in practice I have rarely seen an honest switch statement that really needs case labels for all possible values. Much more common in my experience, 1 or more cases are no-ops. I think it's pointless to list out the no-ops, but I've seen people advocate just that.

There are exceptions to this "some cases are no-ops" thing. But I think it's the minority of switches I've seen.

From that perspective it does seem like the coding police angle. It's reminiscent of certain things in Java, like making a bunch of implicit conversions into errors, or C#, which forbids implicit fall-through on a switch. Those too are for safety and maintainability. They also make a lot of constructs more ugly. My question is: is it worth it? In this case (unintended pun) I don't think so.

Re:Useful GCC warning options not enabled by -Wall -Wextra by Sean1708 on Thursday October 05, 2017 @08:09AM (#15408450)
Really? I've literally never written (or at least I don't ever remember writing) a case statement without either specifying every possible value or adding a meaningful default case.
Re:Useful GCC warning options not enabled by -Wall -Wextra by minexew on Wednesday October 04, 2017 @03:03PM (#15403404)
I should have clarified more; you'd use this for switches where you need to always handle all cases. Then if a you add a new value to the enum but forget to update the switch, you get a compile-time error instead of a buggy program.
Re:Useful GCC warning options not enabled by -Wall -Wextra by asveikau on Wednesday October 04, 2017 @05:15PM (#15404771)
But the flag affects all switches on enums.
Re:Useful GCC warning options not enabled by -Wall -Wextra by hacking_again on Thursday October 05, 2017 @12:04AM (#15406620)
> What if you just don't have meaningful things to do for that enum value?

Then you use:

  default: break;
It's a small price to pay for the consistency checks elsewhere when you don't have a default.
Re:Useful GCC warning options not enabled by -Wall -Wextra by mnarayan01 on Wednesday October 04, 2017 @02:35PM (#15403063)
Consider static type checking when adding a new value to the enum. If the switch has a default, then obviously you lose this, but it's nice for the others.
Re:Useful GCC warning options not enabled by -Wall -Wextra by chrisseaton on Wednesday October 04, 2017 @02:31PM (#15403031)
You're arguing that there are cases where the warning doesn't apply... well that's why it's just a warning not an error. You're not disallowing using switch in other ways - you're just warning yourself when you do.
Re:Useful GCC warning options not enabled by -Wall -Wextra by asveikau on Wednesday October 04, 2017 @02:39PM (#15403108)
Except that the comment I'm replying to says -Werror, meaning they would like to turn this warning into a build break. That seems a bit like language fascism to me. (And if it's not a build break, leaving the warning on stderr during a build is noise, which will cause you to ignore more important warnings.)