rspec/rules/S830/cfamily/rule.adoc

73 lines
1.2 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
Having a single declaration of a type, object or function allows the compiler to detect incompatible types for the same entity.
Normally, this will mean declaring an external identifier in a header file that will be included in any file where the identifier is defined or used.
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,cpp]
2021-04-28 16:49:39 +02:00
----
// header.hpp
extern int16_t a;
// file1.cpp
#include "header.hpp"
extern int16_t b;
// file2.cpp
#include "header.hpp"
extern int32_t b; // Noncompliant, compiler may not detect the error
int32_t a; // Compliant, compiler will detect the error
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,cpp]
2021-04-28 16:49:39 +02:00
----
// header.hpp
extern int16_t a; // Compliant, declared once in a header
extern int32_t b; // Compliant, declared once in a header
// file1.cpp
#include "header.hpp"
// file2.cpp
#include "header.hpp"
----
== Resources
2021-04-28 16:49:39 +02:00
* MISRA {cpp}:2008, 3-2-3
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Move the declaration of "xxx" to a header file.
'''
== Comments And Links
(visible only on this page)
=== relates to: S826
=== relates to: S828
=== relates to: S829
=== relates to: S831
endif::env-github,rspecator-view[]