rspec/rules/S806/cfamily/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

74 lines
1.6 KiB
Plaintext

== Why is this an issue?
Defining variables in the minimum block scope possible reduces the visibility of those variables and therefore reduces the possibility that these identifiers will be used accidentally. A corollary of this is that global objects (including singleton function objects) shall be used in more than one function.
=== Noncompliant code example
[source,cpp]
----
void f ( int32_t k )
{
int32_t i = k * k; // Noncompliant, visibility could be reduced
{
std::cout << i << std::endl;
}
}
----
=== Compliant solution
[source,cpp]
----
void f ( int32_t k )
{
{
int32_t i = k * k; // Compliant
std::cout << i << std::endl;
}
}
----
== Resources
* MISRA {cpp}:2008, 3-4-1
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Scope of this declaration should be reduced.
'''
== Comments And Links
(visible only on this page)
=== relates to: S1899
=== relates to: S5955
=== is related to: S3798
=== relates to: S831
=== is related to: S825
=== on 19 Feb 2016, 22:45:44 Evgeny Mandrikov wrote:
\[~ann.campbell.2] looks very similar to RSPEC-2392
=== on 22 Feb 2016, 16:52:23 Ann Campbell wrote:
Actually [~evgeny.mandrikov] RSPEC-2392 used to be a subtask of RSPEC-1899 but was promoted because RSPEC-1899, like this rule, is only about moving vars down from global to local scope. In JavaScript they can move in both directions.
And because this is a MISRA rule, there are of course related rules (RSPEC-825). Since neither this rule nor RSPEC-825 is implemented yet, would you like to roll them all together?
endif::env-github,rspecator-view[]