xref: /llvm-project/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-cxx17.cpp (revision 46ae26e7eb7095faeda6d6c15470c256f9294c48)
1 // RUN: %check_clang_tidy %s misc-const-correctness %t -- -- -std=c++17 -fno-delayed-template-parsing
2 
3 template <typename L, typename R>
4 struct MyPair {
5   L left;
6   R right;
MyPairMyPair7   MyPair(const L &ll, const R &rr) : left{ll}, right{rr} {}
8 };
9 
f()10 void f() {
11   // FIXME: Decomposition Decls need special treatment, because they require to use 'auto'
12   // and the 'const' should only be added if all elements can be const.
13   // The issue is similar to multiple declarations in one statement.
14   // Simply bail for now.
15   auto [np_local0, np_local1] = MyPair<int, int>(42, 42);
16   np_local0++;
17   np_local1++;
18   // CHECK-FIXES-NOT: auto const [np_local0, np_local1]
19 
20   auto [np_local2, p_local0] = MyPair<double, double>(42., 42.);
21   np_local2++;
22   // CHECK-FIXES-NOT: auto const [np_local2, p_local0]
23 
24   auto [p_local1, np_local3] = MyPair<double, double>(42., 42.);
25   np_local3++;
26   // CHECK-FIXES-NOT: auto const [p_local1, np_local3]
27 
28   auto [p_local2, p_local3] = MyPair<double, double>(42., 42.);
29   // CHECK-FIXES-NOT: auto const [p_local2, p_local3]
30 }
31 
g()32 void g() {
33   int p_local0 = 42;
34   // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'p_local0' of type 'int' can be declared 'const'
35   // CHECK-FIXES: int const p_local0 = 42;
36 }
37 
38 template <typename SomeValue>
39 struct DoGooder {
DoGooderDoGooder40   DoGooder(void *accessor, SomeValue value) {
41   }
42 };
43 struct Bingus {
44   static constexpr auto someRandomConstant = 99;
45 };
46 template <typename Foo>
47 struct HardWorker {
HardWorkerHardWorker48   HardWorker() {
49     const DoGooder<int> anInstanceOf(nullptr, Foo::someRandomConstant);
50   }
51 };
52 struct TheContainer {
53   HardWorker<Bingus> m_theOtherInstance;
54   // CHECK-FIXES-NOT: HardWorker<Bingus> const m_theOtherInstance
55 };
56