rspec/rules/S825/cfamily/rule.adoc

40 lines
1.1 KiB
Plaintext
Raw Normal View History

2021-04-28 16:49:39 +02:00
The scope of objects shall be restricted to functions where possible. File scope shall only be used where objects need to have either internal or external linkage. Where objects are declared at file scope MISRA C 2004 Rule 8.10 applies. It is considered good practice to avoid making identifiers global except where necessary.
Whether objects are declared at the outermost or innermost block is largely a matter of style.
2021-04-28 16:49:39 +02:00
== Noncompliant Code Example
----
int temp;
int function() {
temp = someFunction1();
temp += someFunction2();
// ...
return temp;
}
----
2021-04-28 16:49:39 +02:00
== Compliant Solution
----
int function() {
int temp;
temp = someFunction1();
temp += someFunction2();
// ...
return temp;
}
----
2021-04-28 16:49:39 +02:00
== See
* MISRA C:2004, 8.7 - Objects shall be defined at block scope if they are only accessed from within a single function.
* MISRA C:2012, 8.9 - An object should be defined at block scope if its identifier only appears in a single function
* https://wiki.sei.cmu.edu/confluence/x/XTZGBQ[CERT, DCL53-J.] - Minimize the scope of variables
* https://wiki.sei.cmu.edu/confluence/x/z9YxBQ[CERT, DCL19-C.] - Minimize the scope of variables and functions