Re: Warning on useless 'const' in a function declaration

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 07/10/2024 16:24, R. Diez via Gcc-help wrote:
Hi all:

Sometimes, I forget to remove useless 'const' modifiers in function declarations. A simplified example is:

file.h:

int f ( const int a );  // 'const' is useless here.


file.cpp:

int f ( const int a )
{
   return a;
}


Is there a way to make GCC issue warnings about those useless 'const' modifiers? At the very least, it would save some head scratching during code reviews.

Apparently, clang-tidy has option 'readability-avoid-const-params-in-decls' for such a warning:

https://clang.llvm.org/extra/clang-tidy/checks/readability/avoid-const-params-in-decls.html

Thanks in advance,
   rdiez


You might not /want/ the "const" there, but it is not useless. It means you are not allowed to change the value of the parameter inside the function. Some people see that as a useful thing, because they see modification of parameter values as unexpected and potentially confusing or easily misread. But if you didn't mean to have it there, then it becomes clutter that distracts from the code.

I am curious as to why you would need to remove such a qualifier, however - if you don't like it, why was it there in the first place?

(In general, clang-tidy has more stylistic checks than gcc or the clang compiler. It is quite reasonable to run clang-tidy in addition to your compiler, as a linter and style checker.)




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux