rspec/rules/S3491/cfamily/rule.adoc

41 lines
986 B
Plaintext
Raw Normal View History

2021-04-28 16:49:39 +02:00
By contract, chaining the 'Address of' operator ``++&++`` with the 'Indirection' operator ``++*++`` results in a return to the initial value. Thus, such combinations are confusing at best, and bugs at worst.
2021-04-28 16:49:39 +02:00
== Noncompliant Code Example
----
int *ptr = ...;
int *result1 = &(*ptr); //Noncompliant
int *result2 = &*ptr; //Noncompliant
int value = 4;
int result3 = *(&value); //Noncompliant
int result4 = *&value; //Noncompliant
----
2021-04-28 16:49:39 +02:00
== Compliant Solution
----
int *ptr = ...;
int *result1 = ptr;
int *result2 = ptr;
int value = 4;
int result3 = value;
int result4 = value;
----
2021-04-28 16:49:39 +02:00
== Exceptions
No issue is raised when the ``++*++`` or ``++&++`` operators are overloaded or when both operators are not located in the same piece of code (one being generated by a macro expansion and the other one located in the main source code for instance).
ifdef::rspecator-view[]
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::rspecator-view[]