File: const-correctness-cxx17.cpp

package info (click to toggle)
llvm-toolchain-15 1%3A15.0.6-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,554,644 kB
  • sloc: cpp: 5,922,452; ansic: 1,012,136; asm: 674,362; python: 191,568; objc: 73,855; f90: 42,327; lisp: 31,913; pascal: 11,973; javascript: 10,144; sh: 9,421; perl: 7,447; ml: 5,527; awk: 3,523; makefile: 2,520; xml: 885; cs: 573; fortran: 567
file content (55 lines) | stat: -rw-r--r-- 1,667 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// RUN: %check_clang_tidy %s misc-const-correctness %t -- -- -std=c++17 -fno-delayed-template-parsing

template <typename L, typename R>
struct MyPair {
  L left;
  R right;
  MyPair(const L &ll, const R &rr) : left{ll}, right{rr} {}
};

void f() {
  // FIXME: Decomposition Decls need special treatment, because they require to use 'auto'
  // and the 'const' should only be added if all elements can be const.
  // The issue is similar to multiple declarations in one statement.
  // Simply bail for now.
  auto [np_local0, np_local1] = MyPair<int, int>(42, 42);
  np_local0++;
  np_local1++;
  // CHECK-FIXES-NOT: auto const [np_local0, np_local1]

  auto [np_local2, p_local0] = MyPair<double, double>(42., 42.);
  np_local2++;
  // CHECK-FIXES-NOT: auto const [np_local2, p_local0]

  auto [p_local1, np_local3] = MyPair<double, double>(42., 42.);
  np_local3++;
  // CHECK-FIXES-NOT: auto const [p_local1, np_local3]

  auto [p_local2, p_local3] = MyPair<double, double>(42., 42.);
  // CHECK-FIXES-NOT: auto const [p_local2, p_local3]
}

void g() {
  int p_local0 = 42;
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'p_local0' of type 'int' can be declared 'const'
  // CHECK-FIXES: int const p_local0 = 42;
}

template <typename SomeValue>
struct DoGooder {
  DoGooder(void *accessor, SomeValue value) {
  }
};
struct Bingus {
  static constexpr auto someRandomConstant = 99;
};
template <typename Foo>
struct HardWorker {
  HardWorker() {
    const DoGooder<int> anInstanceOf(nullptr, Foo::someRandomConstant);
  }
};
struct TheContainer {
  HardWorker<Bingus> m_theOtherInstance;
  // CHECK-FIXES-NOT: HardWorker<Bingus> const m_theOtherInstance
};