rspec/rules/S6191/cfamily/rule.adoc

45 lines
911 B
Plaintext
Raw Normal View History

2021-04-28 16:49:39 +02:00
The behavior of volatile types in compound expressions that both read and write the same variable (for instance ``a{plus}{plus}`` or ``++a+=2++``, where ``++a++`` is a ``++volatile int++``) is not well defined, and is deprecated since {cpp}20.
Such expressions should be replaced by explicit operations that only read or write the value.
2021-04-28 16:49:39 +02:00
== Noncompliant Code Example
2022-02-04 17:28:24 +01:00
[source,cpp]
2021-04-28 16:49:39 +02:00
----
void f1(int volatile* p) {
++(*p); // Noncompliant
}
void f2(volatile int& in) {
in += 2; // Noncompliant
}
void f3(volatile int& in) {
int i = in = 2; // Noncompliant
}
----
2021-04-28 16:49:39 +02:00
== Compliant Solution
2022-02-04 17:28:24 +01:00
[source,cpp]
2021-04-28 16:49:39 +02:00
----
void f1(int volatile* p) {
auto val = *p; // One access to read the register
*p = val + 1; // One access to write to it (and potentially overwrite another change)
}
void f2(volatile int& in) {
auto val = in;
in = val + 2;
}
void f3(volatile int& in) {
in = 2;
int i = in;
}
----