Extend modernize-use-auto to cases when a variable is assigned with a cast.
e.g.
Type *Ptr1 = dynamic_cast<Type*>(Ptr2);
Differential D25316
[clang-tidy] Enhance modernize-use-auto to casts malcolm.parsons on Oct 6 2016, 5:19 AM. Authored by
Details Extend modernize-use-auto to cases when a variable is assigned with a cast. e.g.
Diff Detail
Event TimelineComment Actions I think will be good idea to handle LLVM casts and getAs<> methods too. There are plenty of them in LLVM/Clang/etc code used for variable initialization. See also D17765 for Boost related ideas. Comment Actions Please add tests with long long p = static_cast<long long>(4); and the same with const at beginning. I remember I had problems with this last time (Type->SourceRange was returning only source range for the first token. Comment Actions BuiltinTypeLoc only returns the first token: SourceRange getLocalSourceRange() const { return SourceRange(getBuiltinLoc(), getBuiltinLoc()); } The existing check fails too: -long long *ll = new long long(); +auto long *ll = new long long(); Comment Actions Interesting! I remember checking it half year ago, and it was working (SourceRange was returning all tokens from first one Maybe it was introduced in that patch. Anyway I think this have to be fixed somehow. Either by playing with lexer, or by fixing sourceRange Comment Actions Awesome to see this patch. After this one will make it to upstream, it will be much easier for me to do same with template functions.
Comment Actions I was trying to match such functions: varDecl(hasType(type().bind("type")), hasInitializer(callExpr(callee( functionDecl(isInstantiated(), hasTemplateArgument( 0, refersToType(type(equalsBoundNode("type"))))) .bind("fn")))))
Comment Actions I did some implementation long ago for boost-lexical-last here: https://reviews.llvm.org/D17765 Finder->addMatcher( declStmt(has(varDecl(hasInitializer(callExpr(callee( functionDecl(hasName(LEXICAL_CAST_NAME))))), unless(hasType(autoType()))))) .bind("same_type"), this); Finder->addMatcher( declStmt(has(varDecl(hasInitializer(implicitCastExpr(has(callExpr( callee(functionDecl(hasName(LEXICAL_CAST_NAME)))))))))) .bind("different_type"), this);
Comment Actions I was trying to avoid specifying the name of the function so that it works for any templated function/member function that returns its first template type.
Comment Actions Good idea. I am not sure how it will work when you will run it on big codebase, so I wanted to make it configurable for any function name. But if it will work for 99% of useful cases, then it will be great!
Comment Actions BTW I think changing the commit name by removing bug ID would be good, because it would be more clear that this is a feature commit, not a bug fix.
Comment Actions Sorry for the delay. Feel free to ping earlier. One more comment, otherwise looks good.
Comment Actions Does this check properly work in the presence of macros? Those are sometimes more common in casting operations, so a few explicit tests would be good (those tests could be follow-on work if it turns out that this check doesn't play nicely with macros).
|