assigned a value that is never used
Saturday, 12 August 2017 11:01 amWhilst writing some (C++) code, to parse a file that I only roughly know the contents of, I have been printing variables at various points in the code.
I realised that this printing was masking the fact that some variables are not being used, so I inserted some conditional code
#ifdef CODE_ANALYSIS
// Printing variables with CPLDebug can hide
// the fact that they are not otherwise used ...
#define CPLDebug(...)
#endif // CODE_ANALYSIS
so that compilers and other code analysers would not see the printing function, so correctly give warnings like:
[VRC.cpp:976]: (style) Variable 'nNotTIS' is assigned a value that is never used.
That was fine. Until I hit this block of code:
if ( this->nMapID == 8 ) {
int nNotTIS = VRReadInt(fp);
CPLDebug("Viewranger",
"Pay as you go; skipping value %d=x%08x before Tile Index",
nNotTIS, nNotTIS); // See * below
nTileIndexStart+=4;
}
The variable nNotTIS *is* only used in the print, but = VRReadInt(fp) has a side effect (moving the file pointer) that is intended. In a strong sense, I do never use value read from the file and the warning is correct.
I think I will add a comment like
// Expect a warning like
// Variable 'nNotTIS' is assigned a value that is never used.
but is there a better option ?
* Unfortunately CPLDebug() does not support %m$ in the format string, eg "... %1$d=x%1$08x ...", so I have to pass nNotTIS twice if I want to see the decimal and hex values.
I realised that this printing was masking the fact that some variables are not being used, so I inserted some conditional code
#ifdef CODE_ANALYSIS
// Printing variables with CPLDebug can hide
// the fact that they are not otherwise used ...
#define CPLDebug(...)
#endif // CODE_ANALYSIS
so that compilers and other code analysers would not see the printing function, so correctly give warnings like:
[VRC.cpp:976]: (style) Variable 'nNotTIS' is assigned a value that is never used.
That was fine. Until I hit this block of code:
if ( this->nMapID == 8 ) {
int nNotTIS = VRReadInt(fp);
CPLDebug("Viewranger",
"Pay as you go; skipping value %d=x%08x before Tile Index",
nNotTIS, nNotTIS); // See * below
nTileIndexStart+=4;
}
The variable nNotTIS *is* only used in the print, but = VRReadInt(fp) has a side effect (moving the file pointer) that is intended. In a strong sense, I do never use value read from the file and the warning is correct.
I think I will add a comment like
// Expect a warning like
// Variable 'nNotTIS' is assigned a value that is never used.
but is there a better option ?
* Unfortunately CPLDebug() does not support %m$ in the format string, eg "... %1$d=x%1$08x ...", so I have to pass nNotTIS twice if I want to see the decimal and hex values.