rspec/rules/S3715/cfamily/rule.adoc
Fred Tingaud d3cfe19d7e
Fix broken or dangerous backquotes
Co-authored-by: Marco Borgeaud <89914223+marco-antognini-sonarsource@users.noreply.github.com>
2023-10-30 10:33:56 +01:00

114 lines
1.7 KiB
Plaintext

== Why is this an issue?
Proprietary compiler extensions can be handy, but they commit you to always using that compiler. This rule raises an issue when GNU extensions are used, such as:
* Ternary operator with omitted second operand
* Case ranges in switch statements
* Expression statements, i.e. code blocks producing value
* Index range in array initializers
* A array initializer without ``++=++``
* A structure member initializer with a colon
* Decimal floating points numbers ``++_Decimal32++``, ``++_Decimal64++``, and ``++_Decimal128++``
* Structures and union without named data members
=== Noncompliant code example
[source,cpp]
----
struct S {
int f;
};
struct S s[] = {
[0] { // Noncompliant
f : 0 // Noncompliant
}
[1 ... 3] = { // CHECK :8 :11 S3715:use of GNU array range extension
.f = 2
}
};
int fun(int p) {
switch (p) {
case 0 ... 1: // Noncompliant
do_the_thing();
break;
case 2:
//...
}
p = ({ // Noncompliant
int a = 10, b = 20;
(a * b) + 10;
});
return p ?: 0; // Noncompliant
}
_Decimal32 d32; // Noncompliant
struct Empty {}; // Noncompliant in C
----
=== Compliant solution
[source,cpp]
----
struct S {
int f;
};
struct S s[] = {
[0] = {
.f = 0
},
[1] = {
.f = 2
}
[2] = {
.f = 2
},
[3] = {
.f = 2
}
};
int fun(int p) {
switch (p) {
case 0:
case 1:
do_the_thing();
break;
case 2:
//...
}
int a = 10, b = 20;
p = (a * b) + 10;
return p ? p: 0;
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Replace the use of this GNU extension with standard syntax.
=== Highlighting
the extension
endif::env-github,rspecator-view[]