4 ms·
-Wswitch ¶ Warn whenever a switch statement has an index of enumerated type and lacks a case for one or more of the named codes of that enumeration. (The pr
by jpc0 2y ago
-Wswitch ¶
Warn whenever a switch statement has an index of enumerated type and lacks a case for one or more of the named codes of that enumeration. (The presence of a default label prevents this warning.) case labels that do not correspond to enumerators also provoke warnings when this option is used, unless the enumeration is marked with the flag_enum attribute. This warning is enabled by -Wall.
<https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wswitch https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#inde...>
The compiler can do that... And it's included in -Wall. It's not on by default but is effectively on in any codebase where anyone cares...
Please don't argue about "but I don't need to add a flag in Rust" it's not rust, there's reasons the standard committee finds valid for why and honestly your welcome to implement your own compiler that turns it on by default just like the rust compiler which has no standard because "the compiler is the standard".
- tialaramex 2y agoMISRA won't be OK with that. MISRA requires that you explicitly write the default reject. So -Wswitch doesn't get it done even though I agree that if C had (which it did not) standardized this requirement that would get you what you need. C also lacks Rust's non_exhaustive trait. If the person making a published Goose type says it's non-exhaustive then in their code nothing changes, all their code needs to account for all the values of type Goose as before - but everybody else using that type must accept that the author said it's non-exhaustive, so they cannot account for all values of this type except by writing a default handler. So e.g if I publish an AmericanPublicHoliday type when Rust 1.0 ships in 2015, and I mark it non-exhaustive since by definition new holidays may be added, you can't write code to just handle each of the holidays separately, you must have a default handler. When I add Juneteenth to the type, your code is fine, that's a holiday you must handle with your default handler, which you were obliged to write. On the other hand IPAddr, the IP address, is an ordinary exhaustive type, if you handle both IPv6Addr and IPv4Addr you've got a complete handling of IPAddr.
- tialaramex 2y agoUgh too late to catch myself, non_exhaustive is an attribute, not a trait.
- lnkl 2y ago"MISRA requires that you explicitly write the default reject." You can always use -Wswitch-enum then.
- IshKebab 2y ago> Please don't argue about "but I don't need to add a flag in Rust" Why not? It's a big issue. You say it's "on in any codebase where anyone cares", and I agree with that but in my experience most C++ developers don't care. I regularly have to work with other people's C++ where they don't have -Wall -Werror. It's never an issue in Rust. Also I don't buy that they couldn't fix this because it would be a breaking change. That's just an excuse for not bothering. They've made backwards incompatible changes in the past, e.g. removing checked exceptions, changing `auto`, changing the behaviour around operator==. They can just use the standard version, just like Rust uses Editions. Of course they won't, because the C++ standards committee is still very much "we don't need seatbelts, just drive well like me".
- atq2119 2y ago> I regularly have to work with other people's C++ where they don't have -Wall -Werror. To be fair, -Werror is kind of terrible. The set of warnings is very sensitive to the compiler version, so as soon as people work on the project with more than one compiler or even more than one version of the same compiler, it just becomes really impractical. An acceptable compromise can be that -Werror is enabled in CI, but it really shouldn't be the default at least in open-source projects.
- j1elo 2y agoA common trope that is probably ignored or even unknown to uninformed C/C++ programmers, is that -Werr should be used for debug builds (as you use during development) and never for release builds (as otherwise it will most probably break compilation in future releases of the compiler)
- motorest 2y ago> A common trope that is probably ignored or even unknown to uninformed C/C++ programmers, is that -Werr (...) Not even that. -Wall -Werror should be limited to local builds, and should never touch any build config that is invoked by any pipeline.
- SubjectToChange 2y ago>...honestly your welcome to implement your own compiler that turns it on by default just like the rust compiler which has no standard because "the compiler is the standard". The C and C++ standards are quite minimal and whether or not an implementation is "compliant" or not is often a matter of opinion. And unlike other language standards (e.g. Java or Ada) there isn't even a basic conformance test suite for implementations to test against. Hence why Clang had to be explicitly designed for GCC compatibility, particularly for C++. Merely having a "language standard" guarantees very little. For instance, automated theorem proving languages like Coq (Rocq now, I suppose)/Isabelle/Lean have no official language standard, but they far more defined and rigorous than C or C++ ever could be. A formal standard is a useful broker for proprietary implementations, but it has dubious value for a language centered around an open source implementation.
- felipellrocha 2y agobut I don't need to add a flag in Rust
- n144q 2y ago> It's not on by default but is effectively on in any codebase where anyone cares... Then why is this a MISRA rule by itself? Shouldn't it just be "every codebase must compile with -Wall or equivalent"?
- jpc0 2y agoI wouldn't be surprised if you could justify in a review the compiling with -Wall (probably more explicitly) catches this and therefore you can disregard the rule. Not all compilers have a -Wall equivalent, GCC, Clang and MSVC does but RANDOM_EMBEDDED_CHIP's custom compiler might not and that is a valid target for MISRA compliance. I doubt every single thing that needs MISRA get's compiled with an industry standard compiler, I wouldn't be surprised that GCC is the exception for most companies targeting MISRA compliance.