docopt.cpp icon indicating copy to clipboard operation
docopt.cpp copied to clipboard

Move return so that switch has default case

Open eaaltonen opened this issue 3 years ago • 3 comments

Commit

  • Move return so that switch has default case

When a project using docopt.cpp is built with -Werror=switch-default option, there's an error

error: switch missing default case [-Werror=switch-default]

which this fixes.

Signed-off-by: Eero Aaltonen [email protected]

eaaltonen avatar May 30 '22 08:05 eaaltonen

This style is intentional.

There is another warning (-Wswitch) that warns if you forget to handle any enum case, but it gets disabled if you add a default (because that's signaling that you intend to cover "everything else not yet mentioned"). However, that's not the case here, as we want to be sure that we always explicitly handle every possible case.

I personally prefer Wswitch-enum which warns even if you have a default, but that warning tends to get very noisy on legacy code-bases so is less used (I don't know this for sure, but just anecdotally).

I personally prefer the style as it is (as it's compatible with both Wswitch and Wswitch-enum). I'll leave this open to hear other opinions though!

jaredgrubb avatar May 31 '22 23:05 jaredgrubb

I changed the commit so that the warning/error is silenced using a gcc pragma instead. Would you find that acceptable?

eaaltonen avatar Apr 11 '24 10:04 eaaltonen

@jaredgrubb Care to take a look? By suppressing the warning the library could be used also by code that compiles with switch-default.

eaaltonen avatar Apr 16 '24 11:04 eaaltonen