This is an archive of the discontinued LLVM Phabricator instance.

Suggest automated replacements of C-style casts with C++ casts.
ClosedPublic

Authored by alexfh on Jul 11 2014, 6:08 PM.

Details

Summary

This patch implements a subset of possible replacements of C-style
casts with const_cast/static_cast/reinterpret_cast. This should cover a large
portion of cases in real code. Handling of a few more cases may be implemented
eventually.

Diff Detail

Event Timeline

alexfh updated this revision to Diff 11333.Jul 11 2014, 6:08 PM
alexfh retitled this revision from to Suggest automated replacements of C-style casts with C++ casts..
alexfh updated this object.
alexfh edited the test plan for this revision. (Show Details)
alexfh added reviewers: djasper, sbenza.
alexfh added a subscriber: Unknown Object (MLST).
djasper added inline comments.Jul 12 2014, 4:23 AM
clang-tidy/google/AvoidCStyleCastsCheck.cpp
38

nit: Grammatically, I'd prefer "needsConstCast"

71

Create a test where the cast is inside a macro. I think you'll want to use the spelling location (or completely abort as the required cast type might actually be different between different macro invocations).

121

Though benign now, don't fall through here.

test/clang-tidy/avoid-c-style-casts.cpp
9

What happens for the following code?

template <typename T> void f(T t) { int i = (int)t; }
f(1);
f(1.0);

(With the first call to f() hopefully leading to a message about a redundant cast and the second call leading to a reinterpret_cast)

alexfh updated this revision to Diff 11339.Jul 12 2014, 11:49 AM

Don't analyze casts in template instantiations. Warn on casts in macros, but don't suggest fixes.

clang-tidy/google/AvoidCStyleCastsCheck.cpp
38

It used to be named like this before a few rounds of refactoring. Renamed.

71

To the moment we completely ignored casts in macros. Maybe we should warn, just refrain from suggesting fixes. I've implemented it this way now, please take a look.

121

We definitely need to turn on -Wimplicit-fallthrough in LLVM/Clang.

test/clang-tidy/avoid-c-style-casts.cpp
9

Good point. Now that I avoid matches in template instantiations, fixes are not suggested for casts to/from dependent types.

djasper accepted this revision.Jul 14 2014, 12:06 AM
djasper edited edge metadata.

Looks good. Nice!

clang-tidy/google/AvoidCStyleCastsCheck.cpp
86

I'd probably prefer "if (!ParenRange ...) return".

This revision is now accepted and ready to land.Jul 14 2014, 12:06 AM
alexfh closed this revision.Jul 14 2014, 12:45 AM