![]() |
![]() |
1533 | ![]() |
||||
![]() | |||||||
| CMA maintenance checks | |||||||
This object / function is only used in one translation unit. As this
function is not used outside of that translation unit, it would be
better practice to reduce its visibility by moving its definition into
that translation unit and: declaring it static in C or
moving it into an unnamed namespace in C++.
// TU1.h
int sort ();
// TU1.cpp
int sort ()
{
// implement sort only used in TU2
}
// TU2.cpp
#include "TU1.h"
void foo()
{
sort ();
}
// TU3.cpp
void bar()
{
}
A new library is added with an external function 'Sort', and is to be
used in TU3.
Example:
int sort ();
// TU1.cpp
int sort ()
{
// implement sort only used in TU2
}
// TU2.cpp
#include "TU1.h"
void foo()
{
sort ();
}
// TU3.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 'sort' in TU1 by moving it to TU2 would
have resulted in a link error, as sort would no longer be available to
other translation units.
Example:
// TU2.cpp
static int sort ()
{
// implement sort specific to TU2
}
void foo()
{
sort ();
}
// TU3.cpp
int sort (); // typo here. 'S' not 's' in sort.
void bar()
{
sort (); // OK: Causes link error, 'sort' is not defined
}
No MISRA C:2012 Rules applicable to message 1533
See also:
![]() | ||
| QA·C Source Code Analyser 8.1
MISRA C:2012 Compliance Module 1.0 © 2012 Programming Research. www.programmingresearch.com | Personality Groups | Glossary | Message Index | MISRA C:2012 Rule Index | Contents |