Re: [PATCH] D25316: [clang-tidy] Enhance modernize-use-auto to casts
On Mon, Oct 31, 2016 at 2:55 PM, Malcolm Parsons wrote: > malcolm.parsons added inline comments. > > > > Comment at: clang-tidy/modernize/UseAutoCheck.cpp:404 > +[](const Expr *Expr) { return Expr->getType(); }, > +"use auto when initializing with new to avoid " > +"duplicating the type name"); > > aaron.ballman wrote: >> malcolm.parsons wrote: >> > aaron.ballman wrote: >> > > Quote use of `auto` and `new` in the diagnostic since they're syntax >> > > rather than english. >> > A lot of clang-tidy diagnostics don't quote syntax/functions/types: >> > >> > ``` >> > "do not use reinterpret_cast" >> > "pass by value and use std::move" >> > "use nullptr" >> > "the shrink_to_fit method should be used " >> > "use std::move to transfer ownership" >> > "auto_ptr is deprecated, use unique_ptr instead" >> > "use auto when declaring iterators" >> > "use range-based for loop instead" >> > "use emplace_back instead of push_back" >> > "prefer a lambda to std::bind" >> > ... >> > ``` >> clang-tidy hasn't always done a good job of following the conventions that >> clang uses for its diagnostics, but the reason I pointed this wording out >> specifically is because things like "new" are a valid word to use in an >> English sentence too, which makes the diagnostic text harder to understand >> without the quotes. > The diagnostic with 'new' isn't new. Correct, but you are adding a new instance of it, which is a good time to fix things up. > Let's cleanup the diagnostics in another patch. I'm fine with that. ~Aaron ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D25316: [clang-tidy] Enhance modernize-use-auto to casts
malcolm.parsons added inline comments. Comment at: clang-tidy/modernize/UseAutoCheck.cpp:404 +[](const Expr *Expr) { return Expr->getType(); }, +"use auto when initializing with new to avoid " +"duplicating the type name"); aaron.ballman wrote: > malcolm.parsons wrote: > > aaron.ballman wrote: > > > Quote use of `auto` and `new` in the diagnostic since they're syntax > > > rather than english. > > A lot of clang-tidy diagnostics don't quote syntax/functions/types: > > > > ``` > > "do not use reinterpret_cast" > > "pass by value and use std::move" > > "use nullptr" > > "the shrink_to_fit method should be used " > > "use std::move to transfer ownership" > > "auto_ptr is deprecated, use unique_ptr instead" > > "use auto when declaring iterators" > > "use range-based for loop instead" > > "use emplace_back instead of push_back" > > "prefer a lambda to std::bind" > > ... > > ``` > clang-tidy hasn't always done a good job of following the conventions that > clang uses for its diagnostics, but the reason I pointed this wording out > specifically is because things like "new" are a valid word to use in an > English sentence too, which makes the diagnostic text harder to understand > without the quotes. The diagnostic with 'new' isn't new. Let's cleanup the diagnostics in another patch. Repository: rL LLVM https://reviews.llvm.org/D25316 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D25316: [clang-tidy] Enhance modernize-use-auto to casts
aaron.ballman added inline comments. Comment at: clang-tidy/modernize/UseAutoCheck.cpp:404 +[](const Expr *Expr) { return Expr->getType(); }, +"use auto when initializing with new to avoid " +"duplicating the type name"); malcolm.parsons wrote: > aaron.ballman wrote: > > Quote use of `auto` and `new` in the diagnostic since they're syntax rather > > than english. > A lot of clang-tidy diagnostics don't quote syntax/functions/types: > > ``` > "do not use reinterpret_cast" > "pass by value and use std::move" > "use nullptr" > "the shrink_to_fit method should be used " > "use std::move to transfer ownership" > "auto_ptr is deprecated, use unique_ptr instead" > "use auto when declaring iterators" > "use range-based for loop instead" > "use emplace_back instead of push_back" > "prefer a lambda to std::bind" > ... > ``` clang-tidy hasn't always done a good job of following the conventions that clang uses for its diagnostics, but the reason I pointed this wording out specifically is because things like "new" are a valid word to use in an English sentence too, which makes the diagnostic text harder to understand without the quotes. Repository: rL LLVM https://reviews.llvm.org/D25316 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D25316: [clang-tidy] Enhance modernize-use-auto to casts
malcolm.parsons added a comment. In https://reviews.llvm.org/D25316#583651, @aaron.ballman wrote: > 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). Tests added in r285601. Repository: rL LLVM https://reviews.llvm.org/D25316 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D25316: [clang-tidy] Enhance modernize-use-auto to casts
malcolm.parsons added inline comments. Comment at: clang-tidy/modernize/UseAutoCheck.cpp:404 +[](const Expr *Expr) { return Expr->getType(); }, +"use auto when initializing with new to avoid " +"duplicating the type name"); aaron.ballman wrote: > Quote use of `auto` and `new` in the diagnostic since they're syntax rather > than english. A lot of clang-tidy diagnostics don't quote syntax/functions/types: ``` "do not use reinterpret_cast" "pass by value and use std::move" "use nullptr" "the shrink_to_fit method should be used " "use std::move to transfer ownership" "auto_ptr is deprecated, use unique_ptr instead" "use auto when declaring iterators" "use range-based for loop instead" "use emplace_back instead of push_back" "prefer a lambda to std::bind" ... ``` Repository: rL LLVM https://reviews.llvm.org/D25316 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D25316: [clang-tidy] Enhance modernize-use-auto to casts
aaron.ballman added a comment. 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). Comment at: clang-tidy/modernize/UseAutoCheck.cpp:404 +[](const Expr *Expr) { return Expr->getType(); }, +"use auto when initializing with new to avoid " +"duplicating the type name"); Quote use of `auto` and `new` in the diagnostic since they're syntax rather than english. Comment at: clang-tidy/modernize/UseAutoCheck.cpp:413 +}, +"use auto when initializing with a cast to avoid duplicating the type " +"name"); Same here. Repository: rL LLVM https://reviews.llvm.org/D25316 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D25316: [clang-tidy] Enhance modernize-use-auto to casts
This revision was automatically updated to reflect the committed changes. Closed by commit rL285579: [clang-tidy] Enhance modernize-use-auto to casts (authored by malcolm.parsons). Changed prior to commit: https://reviews.llvm.org/D25316?vs=76404&id=76405#toc Repository: rL LLVM https://reviews.llvm.org/D25316 Files: clang-tools-extra/trunk/clang-tidy/modernize/UseAutoCheck.cpp clang-tools-extra/trunk/clang-tidy/modernize/UseAutoCheck.h clang-tools-extra/trunk/docs/ReleaseNotes.rst clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-use-auto.rst clang-tools-extra/trunk/test/clang-tidy/modernize-use-auto-cast-remove-stars.cpp clang-tools-extra/trunk/test/clang-tidy/modernize-use-auto-cast.cpp clang-tools-extra/trunk/test/clang-tidy/modernize-use-auto-new.cpp Index: clang-tools-extra/trunk/clang-tidy/modernize/UseAutoCheck.cpp === --- clang-tools-extra/trunk/clang-tidy/modernize/UseAutoCheck.cpp +++ clang-tools-extra/trunk/clang-tidy/modernize/UseAutoCheck.cpp @@ -23,6 +23,7 @@ const char IteratorDeclStmtId[] = "iterator_decl"; const char DeclWithNewId[] = "decl_new"; +const char DeclWithCastId[] = "decl_cast"; /// \brief Matches variable declarations that have explicit initializers that /// are not initializer lists. @@ -208,27 +209,18 @@ /// \brief This matcher returns declaration statements that contain variable /// declarations with written non-list initializer for standard iterators. StatementMatcher makeIteratorDeclMatcher() { - return declStmt( - // At least one varDecl should be a child of the declStmt to ensure - // it's a declaration list and avoid matching other declarations, - // e.g. using directives. - has(varDecl()), - unless(has(varDecl(anyOf( - unless(hasWrittenNonListInitializer()), hasType(autoType()), - unless(hasType( - isSugarFor(anyOf(typedefIterator(), nestedIterator(), - iteratorFromUsingDeclaration()) + return declStmt(unless(has( + varDecl(anyOf(unless(hasWrittenNonListInitializer()), +unless(hasType(isSugarFor(anyOf( +typedefIterator(), nestedIterator(), +iteratorFromUsingDeclaration()) .bind(IteratorDeclStmtId); } StatementMatcher makeDeclWithNewMatcher() { return declStmt( - has(varDecl()), unless(has(varDecl(anyOf( unless(hasInitializer(ignoringParenImpCasts(cxxNewExpr(, - // Skip declarations that are already using auto. - anyOf(hasType(autoType()), - hasType(pointerType(pointee(autoType(), // FIXME: TypeLoc information is not reliable where CV // qualifiers are concerned so these types can't be // handled for now. @@ -243,6 +235,26 @@ .bind(DeclWithNewId); } +StatementMatcher makeDeclWithCastMatcher() { + return declStmt( + unless(has(varDecl(unless(hasInitializer(explicitCastExpr())) + .bind(DeclWithCastId); +} + +StatementMatcher makeCombinedMatcher() { + return declStmt( + // At least one varDecl should be a child of the declStmt to ensure + // it's a declaration list and avoid matching other declarations, + // e.g. using directives. + has(varDecl()), + // Skip declarations that are already using auto. + unless(has(varDecl(anyOf(hasType(autoType()), + hasType(pointerType(pointee(autoType(, + hasType(referenceType(pointee(autoType(, + anyOf(makeIteratorDeclMatcher(), makeDeclWithNewMatcher(), +makeDeclWithCastMatcher())); +} + } // namespace UseAutoCheck::UseAutoCheck(StringRef Name, ClangTidyContext *Context) @@ -257,8 +269,7 @@ // Only register the matchers for C++; the functionality currently does not // provide any benefit to other languages, despite being benign. if (getLangOpts().CPlusPlus) { -Finder->addMatcher(makeIteratorDeclMatcher(), this); -Finder->addMatcher(makeDeclWithNewMatcher(), this); +Finder->addMatcher(makeCombinedMatcher(), this); } } @@ -313,7 +324,9 @@ << FixItHint::CreateReplacement(Range, "auto"); } -void UseAutoCheck::replaceNew(const DeclStmt *D, ASTContext *Context) { +void UseAutoCheck::replaceExpr(const DeclStmt *D, ASTContext *Context, + std::function GetType, + StringRef Message) { const auto *FirstDecl = dyn_cast(*D->decl_begin()); // Ensure that there is at least one VarDecl within the DeclStmt. if (!FirstDecl) @@ -328,13 +341,13 @@ if (!V) return; -const auto *NewExpr = cast(V->getInit()->Ign
[PATCH] D25316: [clang-tidy] Enhance modernize-use-auto to casts
malcolm.parsons updated this revision to Diff 76404. malcolm.parsons added a comment. Change doc to say "C-style casts". https://reviews.llvm.org/D25316 Files: clang-tidy/modernize/UseAutoCheck.cpp clang-tidy/modernize/UseAutoCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/modernize-use-auto.rst test/clang-tidy/modernize-use-auto-cast-remove-stars.cpp test/clang-tidy/modernize-use-auto-cast.cpp test/clang-tidy/modernize-use-auto-new.cpp Index: test/clang-tidy/modernize-use-auto-new.cpp === --- test/clang-tidy/modernize-use-auto-new.cpp +++ test/clang-tidy/modernize-use-auto-new.cpp @@ -15,6 +15,10 @@ // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use auto when initializing with new // CHECK-FIXES: static auto *a_static = new MyType(); + long long *ll = new long long(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with new + // CHECK-FIXES: auto *ll = new long long(); + MyType *derived = new MyDerivedType(); void *vd = new MyType(); Index: test/clang-tidy/modernize-use-auto-cast.cpp === --- /dev/null +++ test/clang-tidy/modernize-use-auto-cast.cpp @@ -0,0 +1,120 @@ +// RUN: %check_clang_tidy %s modernize-use-auto %t -- -- \ +// RUN: -std=c++11 -I %S/Inputs/modernize-use-auto + +struct A { + virtual ~A() {} +}; + +struct B : public A {}; + +struct C {}; + +void f_static_cast() { + long l = 1; + int i1 = static_cast(l); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto i1 = static_cast(l); + + const int i2 = static_cast(l); + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: const auto i2 = static_cast(l); + + long long ll = static_cast(l); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto ll = static_cast(l); + unsigned long long ull = static_cast(l); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto ull = static_cast(l); + unsigned int ui = static_cast(l); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto ui = static_cast(l); + long double ld = static_cast(l); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto ld = static_cast(l); + + A *a = new B(); + B *b1 = static_cast(a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto *b1 = static_cast(a); + + B *const b2 = static_cast(a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto *const b2 = static_cast(a); + + const B *b3 = static_cast(a); + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: const auto *b3 = static_cast(a); + + B &b4 = static_cast(*a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto &b4 = static_cast(*a); + + const B &b5 = static_cast(*a); + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: const auto &b5 = static_cast(*a); + + B &b6 = static_cast(*a), &b7 = static_cast(*a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto &b6 = static_cast(*a), &b7 = static_cast(*a); + + // Don't warn when non-cast involved + long double cast = static_cast(l), noncast = 5; + + // Don't warn when auto is already being used. + auto i3 = static_cast(l); + auto *b8 = static_cast(a); + auto &b9 = static_cast(*a); +} + +void f_dynamic_cast() { + A *a = new B(); + B *b1 = dynamic_cast(a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto *b1 = dynamic_cast(a); + + B &b2 = dynamic_cast(*a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto &b2 = dynamic_cast(*a); +} + +void f_reinterpret_cast() { + auto *a = new A(); + C *c1 = reinterpret_cast(a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto *c1 = reinterpret_cast(a); + + C &c2 = reinterpret_cast(*a); + // CH
[PATCH] D25316: [clang-tidy] Enhance modernize-use-auto to casts
alexfh accepted this revision. alexfh added a comment. This revision is now accepted and ready to land. Sorry for the delay. Feel free to ping earlier. One more comment, otherwise looks good. Comment at: docs/clang-tidy/checks/modernize-use-auto.rst:163 +The check handles ``static_cast``, ``dynamic_cast``, ``const_cast``, +``reinterpret_cast``, functional casts and c style casts. + nit: "C-style casts" https://reviews.llvm.org/D25316 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D25316: [clang-tidy] Enhance modernize-use-auto to casts
malcolm.parsons added a comment. ping. https://reviews.llvm.org/D25316 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D25316: [clang-tidy] Enhance modernize-use-auto to casts
malcolm.parsons updated this revision to Diff 74476. malcolm.parsons added a comment. Combine matchers https://reviews.llvm.org/D25316 Files: clang-tidy/modernize/UseAutoCheck.cpp clang-tidy/modernize/UseAutoCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/modernize-use-auto.rst test/clang-tidy/modernize-use-auto-cast-remove-stars.cpp test/clang-tidy/modernize-use-auto-cast.cpp test/clang-tidy/modernize-use-auto-new.cpp Index: test/clang-tidy/modernize-use-auto-new.cpp === --- test/clang-tidy/modernize-use-auto-new.cpp +++ test/clang-tidy/modernize-use-auto-new.cpp @@ -15,6 +15,10 @@ // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use auto when initializing with new // CHECK-FIXES: static auto *a_static = new MyType(); + long long *ll = new long long(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with new + // CHECK-FIXES: auto *ll = new long long(); + MyType *derived = new MyDerivedType(); void *vd = new MyType(); Index: test/clang-tidy/modernize-use-auto-cast.cpp === --- /dev/null +++ test/clang-tidy/modernize-use-auto-cast.cpp @@ -0,0 +1,120 @@ +// RUN: %check_clang_tidy %s modernize-use-auto %t -- -- \ +// RUN: -std=c++11 -I %S/Inputs/modernize-use-auto + +struct A { + virtual ~A() {} +}; + +struct B : public A {}; + +struct C {}; + +void f_static_cast() { + long l = 1; + int i1 = static_cast(l); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto i1 = static_cast(l); + + const int i2 = static_cast(l); + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: const auto i2 = static_cast(l); + + long long ll = static_cast(l); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto ll = static_cast(l); + unsigned long long ull = static_cast(l); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto ull = static_cast(l); + unsigned int ui = static_cast(l); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto ui = static_cast(l); + long double ld = static_cast(l); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto ld = static_cast(l); + + A *a = new B(); + B *b1 = static_cast(a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto *b1 = static_cast(a); + + B *const b2 = static_cast(a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto *const b2 = static_cast(a); + + const B *b3 = static_cast(a); + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: const auto *b3 = static_cast(a); + + B &b4 = static_cast(*a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto &b4 = static_cast(*a); + + const B &b5 = static_cast(*a); + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: const auto &b5 = static_cast(*a); + + B &b6 = static_cast(*a), &b7 = static_cast(*a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto &b6 = static_cast(*a), &b7 = static_cast(*a); + + // Don't warn when non-cast involved + long double cast = static_cast(l), noncast = 5; + + // Don't warn when auto is already being used. + auto i3 = static_cast(l); + auto *b8 = static_cast(a); + auto &b9 = static_cast(*a); +} + +void f_dynamic_cast() { + A *a = new B(); + B *b1 = dynamic_cast(a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto *b1 = dynamic_cast(a); + + B &b2 = dynamic_cast(*a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto &b2 = dynamic_cast(*a); +} + +void f_reinterpret_cast() { + auto *a = new A(); + C *c1 = reinterpret_cast(a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto *c1 = reinterpret_cast(a); + + C &c2 = reinterpret_cast(*a); + // CHECK-MESSAGES: :[[@
[PATCH] D25316: [clang-tidy] Enhance modernize-use-auto to casts
alexfh added inline comments. Comment at: clang-tidy/modernize/UseAutoCheck.cpp:252-254 + anyOf(hasType(autoType()), +hasType(pointerType(pointee(autoType(, +hasType(referenceType(pointee(autoType()) This seems to be repeated almost verbatim three times already. It might make sense to merge the three matchers to avoid redundant computation. Comment at: test/clang-tidy/modernize-use-auto-cast-remove-stars.cpp:2 +// RUN: %check_clang_tidy %s modernize-use-auto %t -- \ +// RUN: -config="{CheckOptions: [{key: modernize-use-auto.RemoveStars, value: '1'}]}" \ +// RUN: -- -std=c++11 Prazek wrote: > malcolm.parsons wrote: > > Prazek wrote: > > > What is the difference between this test, and next test? > > > The name indicate that it removes star, but I see a lot of test that > > > doesn't use star > > > and that seem to be duplicated with the next one. > > I could remove the duplicated tests, but the expected fixes are different > > so I'd like to test with and without star removal. > so I think the best thing to do here, is to merge 2 files together, add > second RUN: with different -check-prefix and use this prefix in the tests. At > least this is how the tests in LLVM works, not sure if this is implemented in > check_clang_tidy. Custom -check-prefixes should be supported by the check_clang_tidy.py in order for this to work. It seems out of the scope of this patch. https://reviews.llvm.org/D25316 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D25316: [clang-tidy] Enhance modernize-use-auto to casts
malcolm.parsons updated this revision to Diff 74163. malcolm.parsons added a comment. Really mention in release notes. https://reviews.llvm.org/D25316 Files: clang-tidy/modernize/UseAutoCheck.cpp clang-tidy/modernize/UseAutoCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/modernize-use-auto.rst test/clang-tidy/modernize-use-auto-cast-remove-stars.cpp test/clang-tidy/modernize-use-auto-cast.cpp test/clang-tidy/modernize-use-auto-new.cpp Index: test/clang-tidy/modernize-use-auto-new.cpp === --- test/clang-tidy/modernize-use-auto-new.cpp +++ test/clang-tidy/modernize-use-auto-new.cpp @@ -15,6 +15,10 @@ // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use auto when initializing with new // CHECK-FIXES: static auto *a_static = new MyType(); + long long *ll = new long long(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with new + // CHECK-FIXES: auto *ll = new long long(); + MyType *derived = new MyDerivedType(); void *vd = new MyType(); Index: test/clang-tidy/modernize-use-auto-cast.cpp === --- /dev/null +++ test/clang-tidy/modernize-use-auto-cast.cpp @@ -0,0 +1,120 @@ +// RUN: %check_clang_tidy %s modernize-use-auto %t -- -- \ +// RUN: -std=c++11 -I %S/Inputs/modernize-use-auto + +struct A { + virtual ~A() {} +}; + +struct B : public A {}; + +struct C {}; + +void f_static_cast() { + long l = 1; + int i1 = static_cast(l); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto i1 = static_cast(l); + + const int i2 = static_cast(l); + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: const auto i2 = static_cast(l); + + long long ll = static_cast(l); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto ll = static_cast(l); + unsigned long long ull = static_cast(l); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto ull = static_cast(l); + unsigned int ui = static_cast(l); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto ui = static_cast(l); + long double ld = static_cast(l); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto ld = static_cast(l); + + A *a = new B(); + B *b1 = static_cast(a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto *b1 = static_cast(a); + + B *const b2 = static_cast(a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto *const b2 = static_cast(a); + + const B *b3 = static_cast(a); + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: const auto *b3 = static_cast(a); + + B &b4 = static_cast(*a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto &b4 = static_cast(*a); + + const B &b5 = static_cast(*a); + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: const auto &b5 = static_cast(*a); + + B &b6 = static_cast(*a), &b7 = static_cast(*a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto &b6 = static_cast(*a), &b7 = static_cast(*a); + + // Don't warn when non-cast involved + long double cast = static_cast(l), noncast = 5; + + // Don't warn when auto is already being used. + auto i3 = static_cast(l); + auto *b8 = static_cast(a); + auto &b9 = static_cast(*a); +} + +void f_dynamic_cast() { + A *a = new B(); + B *b1 = dynamic_cast(a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto *b1 = dynamic_cast(a); + + B &b2 = dynamic_cast(*a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto &b2 = dynamic_cast(*a); +} + +void f_reinterpret_cast() { + auto *a = new A(); + C *c1 = reinterpret_cast(a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto *c1 = reinterpret_cast(a); + + C &c2 = reinterpret_cast(*a); + // CHEC
[PATCH] D25316: [clang-tidy] Enhance modernize-use-auto to casts
malcolm.parsons updated this revision to Diff 74162. malcolm.parsons added a comment. Use std::function https://reviews.llvm.org/D25316 Files: clang-tidy/modernize/UseAutoCheck.cpp clang-tidy/modernize/UseAutoCheck.h docs/clang-tidy/checks/modernize-use-auto.rst test/clang-tidy/modernize-use-auto-cast-remove-stars.cpp test/clang-tidy/modernize-use-auto-cast.cpp test/clang-tidy/modernize-use-auto-new.cpp Index: test/clang-tidy/modernize-use-auto-new.cpp === --- test/clang-tidy/modernize-use-auto-new.cpp +++ test/clang-tidy/modernize-use-auto-new.cpp @@ -15,6 +15,10 @@ // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use auto when initializing with new // CHECK-FIXES: static auto *a_static = new MyType(); + long long *ll = new long long(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with new + // CHECK-FIXES: auto *ll = new long long(); + MyType *derived = new MyDerivedType(); void *vd = new MyType(); Index: test/clang-tidy/modernize-use-auto-cast.cpp === --- /dev/null +++ test/clang-tidy/modernize-use-auto-cast.cpp @@ -0,0 +1,120 @@ +// RUN: %check_clang_tidy %s modernize-use-auto %t -- -- \ +// RUN: -std=c++11 -I %S/Inputs/modernize-use-auto + +struct A { + virtual ~A() {} +}; + +struct B : public A {}; + +struct C {}; + +void f_static_cast() { + long l = 1; + int i1 = static_cast(l); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto i1 = static_cast(l); + + const int i2 = static_cast(l); + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: const auto i2 = static_cast(l); + + long long ll = static_cast(l); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto ll = static_cast(l); + unsigned long long ull = static_cast(l); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto ull = static_cast(l); + unsigned int ui = static_cast(l); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto ui = static_cast(l); + long double ld = static_cast(l); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto ld = static_cast(l); + + A *a = new B(); + B *b1 = static_cast(a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto *b1 = static_cast(a); + + B *const b2 = static_cast(a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto *const b2 = static_cast(a); + + const B *b3 = static_cast(a); + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: const auto *b3 = static_cast(a); + + B &b4 = static_cast(*a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto &b4 = static_cast(*a); + + const B &b5 = static_cast(*a); + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: const auto &b5 = static_cast(*a); + + B &b6 = static_cast(*a), &b7 = static_cast(*a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto &b6 = static_cast(*a), &b7 = static_cast(*a); + + // Don't warn when non-cast involved + long double cast = static_cast(l), noncast = 5; + + // Don't warn when auto is already being used. + auto i3 = static_cast(l); + auto *b8 = static_cast(a); + auto &b9 = static_cast(*a); +} + +void f_dynamic_cast() { + A *a = new B(); + B *b1 = dynamic_cast(a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto *b1 = dynamic_cast(a); + + B &b2 = dynamic_cast(*a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto &b2 = dynamic_cast(*a); +} + +void f_reinterpret_cast() { + auto *a = new A(); + C *c1 = reinterpret_cast(a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto *c1 = reinterpret_cast(a); + + C &c2 = reinterpret_cast(*a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: us
[PATCH] D25316: [clang-tidy] Enhance modernize-use-auto to casts
alexfh requested changes to this revision. alexfh added inline comments. This revision now requires changes to proceed. Comment at: clang-tidy/modernize/UseAutoCheck.cpp:331 +void UseAutoCheck::replaceExpr(const DeclStmt *D, ASTContext *Context, + TypeFn GetType, StringRef Message) { const auto *FirstDecl = dyn_cast(*D->decl_begin()); It seems that `replaceExpr` can be implemented in a non-generic way: `GetType` could be a `std::function` and the use of `ExprType` in `cast`could be replaced with a check whether `V->getInit()->IgnoreParenImpCasts()->getStmtClass()` is a specific `StmtClass`. WDYT? https://reviews.llvm.org/D25316 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D25316: [clang-tidy] Enhance modernize-use-auto to casts
malcolm.parsons marked 15 inline comments as done. malcolm.parsons added inline comments. Comment at: test/clang-tidy/modernize-use-auto-cast.cpp:14 + long l = 1; + int i1 = static_cast(l); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name Prazek wrote: > How do you handle cases like > > long l = static_cast(i1); > > I would expect it to produce warning, but not produce fixit (because it might > change the behavior of program), but I still would like to get information > about it because it seems like a bug. The check requires the VarDecl and Initializer to have the same type, so there is no warning. I don't think this check should warn about it. https://reviews.llvm.org/D25316 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D25316: [clang-tidy] Enhance modernize-use-auto to casts
malcolm.parsons updated this revision to Diff 74134. malcolm.parsons added a comment. Fix ast matcher Add test Fix parameter case Inline lambdas Doc fixes https://reviews.llvm.org/D25316 Files: clang-tidy/modernize/UseAutoCheck.cpp clang-tidy/modernize/UseAutoCheck.h docs/clang-tidy/checks/modernize-use-auto.rst test/clang-tidy/modernize-use-auto-cast-remove-stars.cpp test/clang-tidy/modernize-use-auto-cast.cpp test/clang-tidy/modernize-use-auto-new.cpp Index: test/clang-tidy/modernize-use-auto-new.cpp === --- test/clang-tidy/modernize-use-auto-new.cpp +++ test/clang-tidy/modernize-use-auto-new.cpp @@ -15,6 +15,10 @@ // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use auto when initializing with new // CHECK-FIXES: static auto *a_static = new MyType(); + long long *ll = new long long(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with new + // CHECK-FIXES: auto *ll = new long long(); + MyType *derived = new MyDerivedType(); void *vd = new MyType(); Index: test/clang-tidy/modernize-use-auto-cast.cpp === --- /dev/null +++ test/clang-tidy/modernize-use-auto-cast.cpp @@ -0,0 +1,120 @@ +// RUN: %check_clang_tidy %s modernize-use-auto %t -- -- \ +// RUN: -std=c++11 -I %S/Inputs/modernize-use-auto + +struct A { + virtual ~A() {} +}; + +struct B : public A {}; + +struct C {}; + +void f_static_cast() { + long l = 1; + int i1 = static_cast(l); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto i1 = static_cast(l); + + const int i2 = static_cast(l); + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: const auto i2 = static_cast(l); + + long long ll = static_cast(l); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto ll = static_cast(l); + unsigned long long ull = static_cast(l); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto ull = static_cast(l); + unsigned int ui = static_cast(l); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto ui = static_cast(l); + long double ld = static_cast(l); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto ld = static_cast(l); + + A *a = new B(); + B *b1 = static_cast(a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto *b1 = static_cast(a); + + B *const b2 = static_cast(a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto *const b2 = static_cast(a); + + const B *b3 = static_cast(a); + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: const auto *b3 = static_cast(a); + + B &b4 = static_cast(*a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto &b4 = static_cast(*a); + + const B &b5 = static_cast(*a); + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: const auto &b5 = static_cast(*a); + + B &b6 = static_cast(*a), &b7 = static_cast(*a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto &b6 = static_cast(*a), &b7 = static_cast(*a); + + // Don't warn when non-cast involved + long double cast = static_cast(l), noncast = 5; + + // Don't warn when auto is already being used. + auto i3 = static_cast(l); + auto *b8 = static_cast(a); + auto &b9 = static_cast(*a); +} + +void f_dynamic_cast() { + A *a = new B(); + B *b1 = dynamic_cast(a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto *b1 = dynamic_cast(a); + + B &b2 = dynamic_cast(*a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto &b2 = dynamic_cast(*a); +} + +void f_reinterpret_cast() { + auto *a = new A(); + C *c1 = reinterpret_cast(a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto *c1 = reinterpret_cast(a); + + C &c2 = reinterpret_cast(*a)
[PATCH] D25316: [clang-tidy] Enhance modernize-use-auto to casts
malcolm.parsons added inline comments. Comment at: clang-tidy/modernize/UseAutoCheck.cpp:346 -const auto *NewExpr = cast(V->getInit()->IgnoreParenImpCasts()); -// Ensure that every VarDecl has a CXXNewExpr initializer. -if (!NewExpr) +const auto *Expr = cast(V->getInit()->IgnoreParenImpCasts()); +// Ensure that every VarDecl has an ExprType initializer. aaron.ballman wrote: > I think you should use `dyn_cast` here because `ExprType` may vary from call > to call and produce an invalid cast otherwise. The `cast` was safe in the existing check because `makeDeclWithNewMatcher` creates a matcher that ensures all initializers are `CxxNewExpr`. `makeDeclWithCastMatcher` needs to be fixed. Comment at: clang-tidy/modernize/UseAutoCheck.cpp:404 Result.Nodes.getNodeAs(DeclWithNewId)) { -replaceNew(Decl, Result.Context); +auto GetType = [](const CXXNewExpr *Expr) { + return Expr->getType(); aaron.ballman wrote: > Just inline the lambda expression (below as well). OK. Comment at: clang-tidy/modernize/UseAutoCheck.h:30 + void replaceExpr(const DeclStmt *D, ASTContext *Context, TypeFn GetType, + StringRef message); aaron.ballman wrote: > s/message/Message Oops. Comment at: docs/clang-tidy/checks/modernize-use-auto.rst:150 +Frequently, when a variable is declared and initialized with a cast, the +variable type has to be written twice: in the declaration type and in the +cast expression. In this cases, the declaration type can be replaced with aaron.ballman wrote: > s/has to be/is It's a copy and paste from the section above; I'll fix both. Comment at: docs/clang-tidy/checks/modernize-use-auto.rst:164-165 +``reinterpret_cast``, functional casts and c style casts. +It does not handle function templates that behave as casts, such as +``llvm::dyn_cast``, ``boost::lexical_cast`` or ``gsl::narrow_cast``. + aaron.ballman wrote: > Should this be moved into Known Limitations? OK. https://reviews.llvm.org/D25316 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D25316: [clang-tidy] Enhance modernize-use-auto to casts
aaron.ballman added inline comments. Comment at: clang-tidy/modernize/UseAutoCheck.cpp:346 -const auto *NewExpr = cast(V->getInit()->IgnoreParenImpCasts()); -// Ensure that every VarDecl has a CXXNewExpr initializer. -if (!NewExpr) +const auto *Expr = cast(V->getInit()->IgnoreParenImpCasts()); +// Ensure that every VarDecl has an ExprType initializer. I think you should use `dyn_cast` here because `ExprType` may vary from call to call and produce an invalid cast otherwise. Comment at: clang-tidy/modernize/UseAutoCheck.cpp:404 Result.Nodes.getNodeAs(DeclWithNewId)) { -replaceNew(Decl, Result.Context); +auto GetType = [](const CXXNewExpr *Expr) { + return Expr->getType(); Just inline the lambda expression (below as well). Comment at: clang-tidy/modernize/UseAutoCheck.h:28 void replaceIterators(const DeclStmt *D, ASTContext *Context); - void replaceNew(const DeclStmt *D, ASTContext *Context); + template + void replaceExpr(const DeclStmt *D, ASTContext *Context, TypeFn GetType, Having the template declaration in the header, but the definition in the source file, is living a bit too close to the edge for my tastes. Since there are no function definitions in the header file, I suppose this may be okay, however. Comment at: clang-tidy/modernize/UseAutoCheck.h:30 + void replaceExpr(const DeclStmt *D, ASTContext *Context, TypeFn GetType, + StringRef message); s/message/Message Comment at: docs/clang-tidy/checks/modernize-use-auto.rst:150 +Frequently, when a variable is declared and initialized with a cast, the +variable type has to be written twice: in the declaration type and in the +cast expression. In this cases, the declaration type can be replaced with s/has to be/is Comment at: docs/clang-tidy/checks/modernize-use-auto.rst:164-165 +``reinterpret_cast``, functional casts and c style casts. +It does not handle function templates that behave as casts, such as +``llvm::dyn_cast``, ``boost::lexical_cast`` or ``gsl::narrow_cast``. + Should this be moved into Known Limitations? https://reviews.llvm.org/D25316 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits