2023-05-03 11:06:20 +02:00
== Why is this an issue?
2023-08-03 09:11:13 +02:00
The GNU compiler gives the possibility to specify a range of consecutive values in a `case` label, for example: `+case: 1 ... 5+`.
2021-04-28 16:49:39 +02:00
2023-08-03 09:11:13 +02:00
However, if the values are written in decreasing order, i.e., from the larger value to the smaller one, the range will evaluate as empty. So the `case` body will never be executed.
2021-04-28 18:08:03 +02:00
2023-08-03 09:11:13 +02:00
== How to fix it
2021-04-28 16:49:39 +02:00
2023-08-03 09:11:13 +02:00
The rule raises an issue when a range specified in a `case` label is in decreasing order. Swap the values to define a valid range.
=== Code examples
==== Noncompliant code example
[source,cpp,diff-id=1,diff-type=noncompliant]
2021-04-28 16:49:39 +02:00
----
switch (i) {
case 0:
//...
break;
case 1 ... 2:
//...
break;
2023-08-03 09:11:13 +02:00
case 5 ... 3: // Noncompliant: it evaluates as empty, so the code will never be executed
2021-04-28 16:49:39 +02:00
//...
break;
----
2023-08-03 09:11:13 +02:00
==== Compliant solution
2021-04-28 18:08:03 +02:00
2023-08-03 09:11:13 +02:00
[source,cpp,diff-id=1,diff-type=compliant]
2021-04-28 16:49:39 +02:00
----
switch (i) {
case 0:
//...
break;
case 1 ... 2:
//...
break;
case 3 ... 5
//...
break;
----
2021-04-28 18:08:03 +02:00
2023-08-03 09:11:13 +02:00
== Resources
=== Documentation
* GCC - https://gcc.gnu.org/onlinedocs/gcc/Case-Ranges.html[Case Ranges]
2021-04-28 18:08:03 +02:00
2021-09-20 15:38:42 +02:00
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Reverse or otherwise refactor this range; it doesn't match any value.
=== Highlighting
``++x...y++``
2021-09-20 15:38:42 +02:00
endif::env-github,rspecator-view[]