![]() |
![]() |
1505 | ![]() |
||||
![]() | |||||||
| CMA maintenance checks | |||||||
This object / function is only used in the same translation unit where
it is defined. As this function is not used outside of this
translation unit, it would be better practice to reduce its visibility
by declaring it static in C or moving it into an unnamed
namespace in C++.
// TU1.cpp
int sort ()
{
// implement sort specific to TU1
}
void foo()
{
sort ();
}
// TU2.cpp
void bar()
{
}
A new library is added with an external function 'Sort', and is to be
used in TU2.
Example:
// TU1.cpp
int sort ()
{
// implement sort specific to TU1
}
void foo()
{
sort ();
}
// TU2.cpp
int sort (); // typo here. 'S' not 's' in sort.
void bar()
{
sort ();
}
The code still compiles and links, however the logic is flawed.
Restricting the visibility of function 'sort' in TU1 would have
resulted in a link error, as sort would no longer be available to
other translation units.
For Example:
// TU1.cpp
static int sort ()
{
// implement sort specific to TU1
}
void foo()
{
sort ();
}
// TU2.cpp
int sort (); // typo here. 'S' not 's' in sort.
void bar()
{
sort (); // OK: Causes link error, 'sort' is not defined
}
MISRA-C:2004 Rules applicable to message 1505:
| Rule 8.10 (Required) | All declarations and definitions of objects or functions at file scope shall have internal linkage unless external linkage is required. |
See also:
![]() | ||
| QA·C Source Code Analyser 8.1
MISRA-C:2004 Compliance Module 3.2 © 2012 Programming Research. www.programmingresearch.com | Personality Groups | Glossary | Message Index | MISRA-C:2004 Rule Index | Contents |