xref: /llvm-project/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-templates.cpp (revision f40f4fcee506deacda0594362509ee7dddcf5e37)
1 // RUN: %check_clang_tidy %s misc-const-correctness %t -- \
2 // RUN:   -config="{CheckOptions: {\
3 // RUN:   misc-const-correctness.TransformValues: true, \
4 // RUN:   misc-const-correctness.TransformReferences: true, \
5 // RUN:   misc-const-correctness.WarnPointersAsValues: false, \
6 // RUN:   misc-const-correctness.TransformPointersAsValues: false} \
7 // RUN:   }" -- -fno-delayed-template-parsing
8 
9 template <typename T>
type_dependent_variables()10 void type_dependent_variables() {
11   T value = 42;
12   auto &ref = value;
13   T &templateRef = value;
14 
15   int value_int = 42;
16   // CHECK-MESSAGES:[[@LINE-1]]:3: warning: variable 'value_int' of type 'int' can be declared 'const'
17   // CHECK-FIXES: int const value_int
18 }
instantiate_template_cases()19 void instantiate_template_cases() {
20   type_dependent_variables<int>();
21   type_dependent_variables<float>();
22 }
23 
24 namespace gh57297{
25 // The expression to check may not be the dependent operand in a dependent
26 // operator.
27 
28 // Explicitly not declaring a (templated) stream operator
29 // so the `<<` is a `binaryOperator` with a dependent type.
30 struct Stream { };
f()31 template <typename T> void f() { T t; Stream x; x << t; }
32 } // namespace gh57297
33 
34 namespace gh70323{
35 // A fold expression may contain the checked variable as it's initializer.
36 // We don't know if the operator modifies that variable because the
37 // operator is type dependent due to the parameter pack.
38 
39 struct Stream {};
40 template <typename... Args>
concatenate1(Args...args)41 void concatenate1(Args... args)
42 {
43     Stream stream;
44     (stream << ... << args);
45 }
46 
47 template <typename... Args>
concatenate2(Args...args)48 void concatenate2(Args... args)
49 {
50     Stream stream;
51     (args << ... << stream);
52 }
53 
54 template <typename... Args>
concatenate3(Args...args)55 void concatenate3(Args... args)
56 {
57     Stream stream;
58     (..., (stream << args));
59 }
60 } // namespace gh70323
61 
62 namespace gh60895 {
63 
64 template <class T> void f1(T &&a);
65 template <class T> void f2(T &&a);
f1(T && a)66 template <class T> void f1(T &&a) { f2<T>(a); }
f2(T && a)67 template <class T> void f2(T &&a) { f1<T>(a); }
f()68 void f() {
69   int x = 0;
70   // CHECK-MESSAGES:[[@LINE-1]]:3: warning: variable 'x' of type 'int' can be declared 'const'
71   // CHECK-FIXES: int const x = 0;
72   f1(x);
73 }
74 
75 } // namespace gh60895
76