Use auto when declaring variables that are initialized by calling a templated
function that returns its explicit first argument.
Fixes PR26763.
Differential D27166
[clang-tidy] Enhance modernize-use-auto to templated function casts malcolm.parsons on Nov 28 2016, 8:49 AM. Authored by
Details Use auto when declaring variables that are initialized by calling a templated Fixes PR26763.
Diff Detail
Event TimelineComment Actions They already say this:
Does that cover it? Comment Actions I'm not very sure, because cats may be interpreted as C++ language constructs only, but not framework provided ones. Comment Actions gosh, I found it too late. Jakub started to work on the same problem and he have made initial check for it. long a = lexical_cast<long>(z), b = lexical_cast<int>(y); Other test: Base *p = dyn_cast<Derived>(b); Comment Actions Yes; replaceExpr() checks that the variable has the same unqualified type as the initializer and the same canonical type as other variables in the declaration. Comment Actions Can you add this small test?
Comment Actions Cool! lib/Analysis/AssumptionCache.cpp:120 - for (const BasicBlock &B : cast<Function>(*I.first)) + for (const BasicBlock &B : auto<Function>(*I.first)) Comment Actions I ran it on some of clang-tools-extra: unittests/clang-tidy/ClangTidyDiagnosticConsumerTest.cpp:17 - const VarDecl *Var = Result.Nodes.getNodeAs<VarDecl>("var"); + const auto *Var = Result.Nodes.getNodeAs<VarDecl>("var");
The check should be ignoring implicit VarDecls. Comment Actions There is still one more problem: /home/prazek/llvm/lib/Analysis/ScalarEvolution.cpp:2442:11: warning: use auto when initializing with a template cast to avoid duplicating the type name [modernize-use-auto] const auto **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size()); ^ auto The same thing for normal casts, so I guess it is not only problem with this patch /home/prazek/llvm/lib/IR/User.cpp:156:3: warning: use auto when initializing with a cast to avoid duplicating the type name [modernize-use-auto] auto **HungOffOperandList = static_cast<Use **>(Storage); ^ auto There is also problem with function pointers /home/prazek/llvm/lib/ExecutionEngine/MCJIT/MCJIT.cpp:520:9: warning: use auto when initializing with a cast to avoid duplicating the type name [modernize-use-auto] int (*PF)(int, char **, const char **) = ^ auto So these problems occur for cast like functions and for cast. Do you see simple fix for it? Comment Actions Any suggestions for rewriting this matcher? // Skip declarations that are already using auto. unless(has(varDecl(anyOf(hasType(autoType()), hasType(pointerType(pointee(autoType()))), hasType(referenceType(pointee(autoType())))))))
The warning is correct, but the fixit is wrong. Comment Actions Use qualType(hasDescendant(autoType())) to fix skipping of declarations that Comment Actions Yep, it is not worth fixing it. Have you add test cases to the cases that we discussed?
Comment Actions OK.
I'll try to add some test cases tonight. Comment Actions LGTM, thanks for all the fixes. |
Ideally, this should go to ASTMatchers.h (with a proper test and documentation).