It's usually better for your code to describe as accurately as possible what's going on. You're getting this warning because the const
in const int foo();
is basically meaningless. The API only seems clearer if you don't know what the const
keyword means. Don't overload meaning like that; static
is bad enough as it is, and there's no reason to add the potential for more confusion.
const char *
means something different than const int
does, which is why your tool doesn't complain about it. The former is a pointer to a constant string, meaning any code calling the function returning that type shouldn't try to modify the contents of the string (it might be in ROM for example). In the latter case, the system has no way to enforce that you not make changes to the returned int
, so the qualifier is meaningless. A closer parallel to the return types would be:
const int foo();
char * const foo2();
which will both cause your static analysis to give the warning - adding a const qualifier to a return value is a meaningless operation. It only makes sense when you have a a reference parameter (or return type), like your const char *
example.
In fact, I just made a little test program, and GCC even explicitly warns about this problem:
test.c:6: warning: type qualifiers ignored on function return type
So it's not just your static analysis program that's complaining.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…