Rule enforcing "const" on pointer parameters that aren't modified

The rule ‘Pointer and reference parameters should be “const” if the corresponding object is not modified’ is very valuable in general, but it poses a problem in C whenever you have a “void *” parameter used for threading state to a callback.

I don’t see an option to disable this rule for void pointers, but it would be extremely helpful. In many cases in C where you use function pointers to implement an interface, you always let the caller pass a void pointer along with the function pointer, so they can use it to thread state to their callback. In many cases, they will need to modify the data this pointer points to, and using const there is not an option. However, some callers will have no need for this pointer and will just pass NULL and ignore it within the callback. The end result is that the void pointer in these callbacks triggers the above rule because it’s not modified, but you cannot change the function to use “const void *” for the parameter because it wouldn’t match the proper function type for the interface.

Hi @Maxime_Henrion ,

Thanks for your post and sorry I took this long to answer.

I created a ticket on our side to try to better handle functions passed as pointers. I can’t say whether we’ll go with this approach of skipping void*, but we’ll consider it.

Thank you Fred !

Just to be perfectly clear, I don’t know that it would be reasonable to always skip void pointers in general, but an option to do it would be useful. Alternatively, maybe an option to ignore this rule when the pointer parameter is not referenced at all in the function’s body?

Regardless, thank you for looking into this.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.