This isn't a specific bug, but more a question about philosophy of
static analysis. If there is a better place to discuss this please
tell me.
I am not very familiar with exceptions; if they are a reasonable way
to resolve this please say so.
I am programming defensively - allowing for possible cases that
currently cannot occur - which means that static analysis indicates
that I am creating unexecutable blocks of code.
Specifically, I am writing a function which returns a value to
indicate success, or possibly (which sort of) failure. At present it
does not fail, always returning "success". Thus if the calling code
tests the return value, cppcheck warns that the test is always true
(or false).
Conceptually, the bug is in the function definition, since it does
not return all intended values, but currently cppcheck warns me about
each function call, since they attempt to handle return values that
are not currently returned.
It would be better if I could encourage cppcheck to warn me if
the function always returns the same value, or perhaps if the return
value is a bool or an enum, when there are values that are never returned.
Perhaps one way to do this would be a comment listing other potential
return values that callers should be prepared to handle ?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
So in this case you don't want that cppcheck looks at the function definition and determines what the return value is.
Your suggestion to add a comment is reasonable imho. I am not against to handle some comments or configuration. If possible it should be some kind of standard like microsoft SAL. C++ contracts. ACSL.
Just a wild idea... if you want you could try to add this code in the function definition:
#ifdef __cppcheck__#define cppcheck_rand_return(MINVAL,MAXVAL) \ if (rand()%1) return (rand() % (1 + MAXVAL - MINVAL)) + MINVAL;#else#define cppcheck_rand_return(MINVAL,MAXVAL)#endif// your functionintdostuff(){cppcheck_rand_return(-5,-1);...yourordinarycode...}
you would need to define __cppcheck__ during cppcheck analysis for that to work. If I understand you correctly the cppcheck_rand_return() should prevent the false positives.
It would be better if I could encourage cppcheck to warn me if the function always returns the same value, or perhaps if the return value is a bool or an enum, when there are values that are never returned.
Yes in general it's suspicious if a function always returns the same value.
Unfortunately I would think that there are situations when a function always returns the same value by intention. For instance in overridden virtual methods and in callback functions.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I guess you could also hide the function definition for cppcheck. Put the definition in another source file. That should fix the false positive about checking the return value. But maybe you don't want to do that.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
This isn't a specific bug, but more a question about philosophy of
static analysis. If there is a better place to discuss this please
tell me.
I am not very familiar with exceptions; if they are a reasonable way
to resolve this please say so.
I am programming defensively - allowing for possible cases that
currently cannot occur - which means that static analysis indicates
that I am creating unexecutable blocks of code.
Specifically, I am writing a function which returns a value to
indicate success, or possibly (which sort of) failure. At present it
does not fail, always returning "success". Thus if the calling code
tests the return value, cppcheck warns that the test is always true
(or false).
Conceptually, the bug is in the function definition, since it does
not return all intended values, but currently cppcheck warns me about
each function call, since they attempt to handle return values that
are not currently returned.
It would be better if I could encourage cppcheck to warn me if
the function always returns the same value, or perhaps if the return
value is a bool or an enum, when there are values that are never returned.
Perhaps one way to do this would be a comment listing other potential
return values that callers should be prepared to handle ?
Interesting question.
So in this case you don't want that cppcheck looks at the function definition and determines what the return value is.
Your suggestion to add a comment is reasonable imho. I am not against to handle some comments or configuration. If possible it should be some kind of standard like microsoft SAL. C++ contracts. ACSL.
Just a wild idea... if you want you could try to add this code in the function definition:
you would need to define
__cppcheck__
during cppcheck analysis for that to work. If I understand you correctly the cppcheck_rand_return() should prevent the false positives.Yes in general it's suspicious if a function always returns the same value.
Unfortunately I would think that there are situations when a function always returns the same value by intention. For instance in overridden virtual methods and in callback functions.
I guess you could also hide the function definition for cppcheck. Put the definition in another source file. That should fix the false positive about checking the return value. But maybe you don't want to do that.