1

Old-style casts should be avoided for a number of reasons. And gcc offers the handy -Wold-style-casts to detect them. This works fine, but for non-class types, constructor-style casts behave exactly like old-style casts, minus the warning.

Example:

struct T {};
int main() {
  int const* a = nullptr;
  using Tp = T*; // float*, unsigned*, pick whatever
  Tp b = Tp(a); // <---- hidden old-style cast: (Tp)a
  *b = T{}; // ub
}

This program compiles without problems, and even exhibits UB. The constructor-syntax would have been fine if Tp had been a class type and had a constructor that accepted a's type.

I'm trying to detect only the cases where the syntax would hide an old-style cast in addition to what -Wold-style-cast does.

bitmask
  • 32,434
  • 14
  • 99
  • 159
  • 5
    A c-style cast is `(type)object`, `type(object)` is called a functional style cast, and not covered by the warning – NathanOliver Oct 14 '21 at 12:03
  • @NathanOliver Yes, I realise that it is not covered, I'm trying to find a way to *also* get warnings for `type(object)` iff type is a non-class type. – bitmask Oct 14 '21 at 12:07
  • You could just start using C++-style casts instead. `static_cast(a)` would cause an error without any compiler flags. – NathanOliver Oct 14 '21 at 12:09
  • @bitmask You'll need to modify the compiler to add the warning that you want. – eerorika Oct 14 '21 at 12:09
  • @NathanOliver: I do. Unfortunately the code base is full of them. – bitmask Oct 14 '21 at 12:10
  • @eerorika Is this an unreasonable thing to want? I realise the difficulty in handling such cases in case of e.g. function templates, I was just hoping there was a mechanism to at least catch the more obvious instances of this construct. – bitmask Oct 14 '21 at 12:13
  • @bitmask `Is this an unreasonable thing to want?` To want such warning? I don't think it's unreasonable. But that doesn't mean that what you want exists. – eerorika Oct 14 '21 at 12:16
  • you could also create a `clang-tidy` rule for this type of casts (as far as I know there isn't one already) – ben10 Oct 14 '21 at 12:40

1 Answers1

2

There is a bugzilla entry Bug 69818 - warn for C++ functional cast expression on pointer or reference (i.e. add -Wfunctional-cast) proposing such a feature for gcc.

Two alternatives are suggested by Chris Studholme

A) warn on all functional cast expressions where the destination is a pointer or reference type, or

B) warn on all functional cast expressions where the destination is a pointer or reference type and the equivalent static_cast<>() expression would produce an error.

and in a later comment

Another option might be to extend -Wold-style-cast to cover these problematic uses of functional casts, instead of adding a new warning.

Though, in the thread there is no definite consensus on what would be the prefered way, it is "Not yet assigned to anyone", and the last contribution is from 2018 (the bug is filed for 5.2.1).

To my knowledge nothing changed in newer versions of gcc.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • That sounds close to what I was looking for which would confirm that there is no way to do this in gcc. However, the limitation to only pointer/ref types (as opposed to non-class types) eludes me a bit. – bitmask Oct 14 '21 at 12:54