rspec/rules/S5184/cfamily/rule.adoc

40 lines
1.1 KiB
Plaintext
Raw Normal View History

2021-04-28 16:49:39 +02:00
The RAII idiom associates the lifetime of a resource with the lifetime of an object: The resource is acquired when the object is created, and released when it is destroyed.
If the object that controls the resource lifetime is a temporary, chances are it will get destroyed while the resource should still be in use, leading to resource corruption. This rule detects temporaries that look like RAII objects.
2021-04-28 16:49:39 +02:00
== Noncompliant Code Example
----
void f() {
scoped_lock{myMutex}; // Noncompliant. The mutex will be locked then immediately unlocked
protectedCode(); // This code is not protected by the mutex
}
----
2021-04-28 16:49:39 +02:00
== Compliant Solution
----
void f() {
scoped_lock lock{myMutex}; // Compliant
protectedCode();
// The mutex is correctly released at this point
}
----
2021-04-28 16:49:39 +02:00
== See
* https://github.com/isocpp/CppCoreGuidelines/blob/036324/CppCoreGuidelines.md#es84-dont-try-to-declare-a-local-variable-with-no-name[{cpp} Core Guidelines ES.84] - Dont (try to) declare a local variable with no name
ifdef::env-github,rspecator-view[]
'''
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]