From db04833fcabea67dd38182fe81fab902015c1e8c Mon Sep 17 00:00:00 2001 From: Malcolm Parsons Date: Mon, 31 Oct 2016 14:43:37 +0000 Subject: [clang-tidy] Enhance modernize-use-auto to casts Summary: Extend modernize-use-auto to cases when a variable is assigned with a cast. e.g. Type *Ptr1 = dynamic_cast(Ptr2); http://llvm.org/PR25499 Reviewers: angelgarcia, aaron.ballman, klimek, Prazek, alexfh Subscribers: Prazek, Eugene.Zelenko, cfe-commits Tags: #clang-tools-extra Differential Revision: https://reviews.llvm.org/D25316 llvm-svn: 285579 --- .../test/clang-tidy/modernize-use-auto-cast.cpp | 120 +++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 clang-tools-extra/test/clang-tidy/modernize-use-auto-cast.cpp (limited to 'clang-tools-extra/test/clang-tidy/modernize-use-auto-cast.cpp') diff --git a/clang-tools-extra/test/clang-tidy/modernize-use-auto-cast.cpp b/clang-tools-extra/test/clang-tidy/modernize-use-auto-cast.cpp new file mode 100644 index 00000000000..06ab3a20075 --- /dev/null +++ b/clang-tools-extra/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: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto &c2 = reinterpret_cast(*a); +} + +void f_const_cast() { + const A *a1 = new A(); + A *a2 = const_cast(a1); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto *a2 = const_cast(a1); + A &a3 = const_cast(*a1); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto &a3 = const_cast(*a1); +} + +void f_cstyle_cast() { + auto *a = new A(); + C *c1 = (C *)a; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto *c1 = (C *)a; + + C &c2 = (C &)*a; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto &c2 = (C &)*a; +} + +void f_functional_cast() { + long l = 1; + int i1 = int(l); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name + // CHECK-FIXES: auto i1 = int(l); + + B b; + A a = A(b); +} -- cgit v1.2.3