rspec/rules/S3562/cfamily/rule.adoc
Fred Tingaud 22b4470f2a
Modify CFamily rules: CPP-4080 Refresh and standardize CppCoreGuidelines references (#3514)
Update all links to C++ Core Guidelines to `e49158a`.

Refresh done using the following script and some manual edits:
db76e34e74/personal/fred-tingaud/rspec/refresh-cppcoreguidelines.py

When re-using this script, be mindful that:
 - it does not cover `shared_content`
 - it does not properly escape inline code in links (e.g., "[=]" or "`mutex`es")
 - it does not change `C++` to `{cpp}` in link titles.

Co-authored-by: Marco Borgeaud <marco.borgeaud@sonarsource.com>
2023-12-20 10:08:18 +01:00

128 lines
2.5 KiB
Plaintext

== Why is this an issue?
For completeness, a ``++switch++`` over the values of an ``++enum++`` must either address each value in the ``++enum++`` or contain a ``++default++`` case. ``++switch++`` statements that are not over ``++enum++`` must end with a ``++default++`` case.
This rule is a more nuanced version of S131.
Use S131 if you want to require a `++default++` case for every `++switch++`
even if it already handles all enumerators of an `++enum++`.
Otherwise, use this rule.
=== Noncompliant code example
[source,cpp]
----
typedef enum {APPLE, GRAPE, KIWI} fruit;
void example(fruit f, int i) {
switch (f) { // Noncompliant; no case for KIWI
case APPLE:
//...
case GRAPE:
//...
case 3: // Noncompliant; case value not in enum
// ...
}
switch (i) { // Noncompliant; no default
case 0:
// ...
case 1:
// ...
}
}
----
=== Compliant solution
[source,cpp]
----
typedef enum {APPLE, GRAPE, KIWI} fruit;
void example(fruit f) {
switch (f) {
case APPLE:
//...
case GRAPE:
//...
default:
// ...
}
switch (i) {
case 0:
// ...
case 1:
// ...
default:
// ...
}
}
----
or
[source,cpp]
----
typedef enum {APPLE, GRAPE, KIWI} fruit;
void example(fruit f) {
switch (f) {
case APPLE:
//...
case GRAPE:
//...
case KIWI:
//...
}
switch (i) {
case 0:
case 1:
// ...
default:
// ...
}
}
----
== Resources
* {cpp} Core Guidelines - https://github.com/isocpp/CppCoreGuidelines/blob/e49158a/CppCoreGuidelines.md#enum2-use-enumerations-to-represent-sets-of-related-named-constants[Enum.2: Use enumerations to represent sets of related named constants]
=== Related rules
* S131
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
* Add a "default" case to this switch or cover all values of the enum.
* Case value not in enum.
* Add a "default" case to this switch.
=== Highlighting
``++switch++`` or ``++case++`` keyword
'''
== Comments And Links
(visible only on this page)
=== relates to: S131
=== on 13 Jun 2017, 08:50:16 Freddy Mallet wrote:
For some implementation details, RSPEC-131 can't support in C/{cpp} the following exception and so a dedicated rule has been created:
* If the switch parameter is an enum and if all the constants of this enum are used in the case statements, then no default clause is expected
This RSPEC must not be covered by any language except C/{cpp} and Objective-C
endif::env-github,rspecator-view[]