[RFC] Add preferred casting style to coding standards

I noticed today that we don’t have any preferred casting style documented in the coding standards, contrary to my belief: I’ve been asking people to use C+±style casting for as long as I can remember, thinking that was explicitly stated as preferred somewhere.

In most cases, I believe C+±style casts are superior to C-style casts, due to the additional safety checks they come with, yet I regularly see patches containing the latter. I think we need to be explicit about our preferred style and when contributors should diverge from it. I believe the only case we should diverge from the C+±style is for casting to void to suppress unused variable warnings (this is actually covered in the coding standards, under the section on assertions, albeit indirectly).

I’ve created a patch to add this to the document and would appreciate feedback:

https://reviews.llvm.org/D151187

3 Likes

I find myself occasionally being annoyed by C++ style casts around enums, but I think this is a reasonable step to take.

1 Like

So do I, but I don’t think forbidding C-style casts is a reasonable step to take if most developers are uncomfortable with it. IMO it is purely a stylistic issue and should be judged accordingly, i.e. by voting.
(I’ll probably lose anyway because most people just don’t care or trust cppcoreguidelines too much, but I will know that I tried :slight_smile: )

1 Like

+1, actually thought this was the rule already.

I witnessed many bugs caused by C-casts hiding necessary refactoring changes. Saving a few characters is not worth all the potential debugging time down the road.

2 Likes

I am supportive of this. I have had mixed reactions to comments to use C++ style casts in the past, mostly along the lines that C++ style casts are too verbose. I think the change in the PR is a step in the right direction.