Skip to content

Commit d40c6f0

Browse files
Add rspec
1 parent 74b9e25 commit d40c6f0

File tree

2 files changed

+103
-24
lines changed

2 files changed

+103
-24
lines changed

rules/S8216/cfamily/metadata.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
{
2-
"title": "FIXME",
2+
"title": "Code should not rely on features beyond the configured C++ standard",
33
"type": "CODE_SMELL",
44
"status": "ready",
55
"remediation": {
66
"func": "Constant\/Issue",
77
"constantCost": "5min"
88
},
99
"tags": [
10+
"lock-in"
1011
],
1112
"defaultSeverity": "Major",
1213
"ruleSpecification": "RSPEC-8216",
1314
"sqKey": "S8216",
1415
"scope": "All",
15-
"defaultQualityProfiles": ["Sonar way"],
16+
"defaultQualityProfiles": [
17+
],
1618
"quickfix": "unknown",
1719
"code": {
1820
"impacts": {

rules/S8216/cfamily/rule.adoc

Lines changed: 99 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,121 @@
1-
FIXME: add a description
2-
3-
// If you want to factorize the description uncomment the following line and create the file.
4-
//include::../description.adoc[]
1+
Code should not rely on features beyond the configured {cpp} standard
52

63
== Why is this an issue?
74

8-
FIXME: remove the unused optional headers (that are commented out)
5+
Relying on features that are not part of the configured {cpp} standard leads to non-portable builds and fragile code. Some toolchains accept such code as extensions or for backward compatibility, while others strictly reject it under the same standard setting. This rule flags:
6+
7+
* Uses of features from future {cpp} standards (compared to the configured standard).
8+
* Uses of features removed in the configured {cpp} standard.
9+
* Uses of C-only constructs that some {cpp} implementations accept as extensions.
910

10-
//=== What is the potential impact?
11+
Keeping code within the configured standard ensures consistency across compilers and prevents accidental lock-in to permissive extensions.
1112

1213
== How to fix it
13-
//== How to fix it in FRAMEWORK NAME
1414

15-
=== Code examples
15+
Replace uses of out-of-scope features with standard-conforming alternatives that exist in the configured {cpp} standard. If the newer feature (or a removed feature) is truly required, upgrade (or align) the project’s configured standard and all toolchains consistently.
1616

17-
==== Noncompliant code example
17+
=== Defaulted comparison operators
1818

19+
Defaulted comparison operators are introduced in {cpp}20. When the configured standard is earlier than {cpp}20, using defaulted comparison operators is noncompliant. Under {cpp}20 or later, the same code is compliant.
20+
21+
==== Noncompliant code example
1922
[source,cpp,diff-id=1,diff-type=noncompliant]
2023
----
21-
FIXME
24+
struct Account {
25+
int id;
26+
bool operator==(const Account&) const = default; // Noncompliant before {cpp}20
27+
};
2228
----
2329

2430
==== Compliant solution
25-
2631
[source,cpp,diff-id=1,diff-type=compliant]
2732
----
28-
FIXME
33+
struct Account {
34+
int id;
35+
bool operator==(const Account& other) const { return id == other.id; }
36+
};
37+
----
38+
39+
=== Using enum declaration
40+
41+
The "using enum" declaration is a {cpp}20 feature. When the configured standard is earlier than {cpp}20, it is noncompliant to rely on it. Under {cpp}20 or later, the same code is compliant.
42+
43+
==== Noncompliant code example
44+
[source,cpp,diff-id=2,diff-type=noncompliant]
45+
----
46+
enum class Status { Ok, Error };
47+
using enum Status; // Noncompliant before {cpp}20
48+
auto s = Ok;
49+
----
50+
51+
==== Compliant solution
52+
[source,cpp,diff-id=2,diff-type=compliant]
53+
----
54+
enum class Status { Ok, Error };
55+
auto s = Status::Ok;
56+
----
57+
58+
=== Lambda template parameter list
59+
60+
Lambda template parameter lists are available starting in {cpp}20. When the configured standard is earlier than {cpp}20, this usage is noncompliant. Under {cpp}20 or later, the same code is compliant.
61+
62+
==== Noncompliant code example
63+
[source,cpp,diff-id=3,diff-type=noncompliant]
64+
----
65+
auto mapValue = []<typename T>(T t) { return t; }; // Noncompliant before {cpp}20
66+
----
67+
68+
==== Compliant solution
69+
[source,cpp,diff-id=3,diff-type=compliant]
70+
----
71+
auto mapValue = [](auto t) { return t; };
72+
----
73+
74+
=== C array designators in {cpp}
75+
76+
Some C-only constructs, such as array designators, may be accepted by {cpp} compilers as extensions, but they are not part of the {cpp} standard and should be avoided in {cpp} code.
77+
78+
==== Noncompliant code example
79+
[source,cpp,diff-id=4,diff-type=noncompliant]
80+
----
81+
int values[3] = { [1] = 5 }; // Noncompliant in {cpp}
82+
----
83+
84+
==== Compliant solution
85+
[source,cpp,diff-id=4,diff-type=compliant]
86+
----
87+
int values[3] = {};
88+
values[1] = 5;
89+
----
90+
91+
=== Removed library feature
92+
93+
Some features are removed in newer standards. For example, ``++std::auto_ptr++`` was removed in {cpp}17; using it when the configured standard has removed it is noncompliant.
94+
95+
==== Noncompliant code example
96+
[source,cpp,diff-id=5,diff-type=noncompliant]
97+
----
98+
#include <memory>
99+
100+
std::auto_ptr<int> p(new int(42)); // Noncompliant in {cpp}17 and later
101+
----
102+
103+
==== Compliant solution
104+
[source,cpp,diff-id=5,diff-type=compliant]
105+
----
106+
#include <memory>
107+
108+
std::unique_ptr<int> p = std::make_unique<int>(42);
29109
----
30110

31-
//=== How does this work?
111+
=== Limitations
32112

33-
//=== Pitfalls
113+
This version primarily reports extensions starting from {cpp}20 and later. For example, it does not yet report use of a {cpp}17-only feature when the configured standard is {cpp}14. This limitation is temporary and will be addressed in a future update.
34114

35-
//=== Going the extra mile
115+
== Resources
36116

117+
=== Related rules
37118

38-
//== Resources
39-
//=== Documentation
40-
//=== Articles & blog posts
41-
//=== Conference presentations
42-
//=== Standards
43-
//=== External coding guidelines
44-
//=== Benchmarks
119+
* S3715 - GNU extensions should not be used
120+
* S8230 - MSVC-specific extensions should not be used
121+
* S8231 - Non-standard attributes should not be used

0 commit comments

Comments
 (0)