[RFC][clang-tidy] New check for widening pointer cast

Hello!

I am working on a new check for clang-tidy.

It will warn about dangerous casts when there is a pointer cast and the result type is larger than the source data.

A typical case where I think a warning is OK:

    struct A {
        int A;
    };

    struct AB {
        int A;
        int B;
    };

    void warn1() {
        struct A A1;
        struct AB *AB1 = (struct AB *)&A1; // <- WARN
    }

There is no UB in that specific code as far as I know. But imho it's dangerous.

My work-in-progress code can be seen in the attached patch.

Initially I only warn about record types but maybe simple types (short,int,..) can be checked also.

Best regards,
Daniel Marjamäki

..................................................................................................................
Daniel Marjamäki Senior Engineer
Evidente ES East AB Warfvinges väg 34 SE-112 51 Stockholm Sweden

Mobile: +46 (0)709 12 42 62
E-mail: Daniel.Marjamaki@evidente.se

www.evidente.se

160315-widening-pointer-cast.diff (6.6 KB)