summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/test/clang-tidy/misc-static-assert.cpp
blob: e63d7f9236b4f2e9c2960cc8bc319fe9f3371cac (plain)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// RUN: $(dirname %s)/check_clang_tidy.sh %s misc-static-assert %t
// REQUIRES: shell

void abort() {}
#ifdef NDEBUG
#define assert(x) 1
#else
#define assert(x)                                                              \
  if (!(x))                                                                    \
  abort()
#endif

#define ZERO_MACRO 0

#define my_macro() assert(0 == 1)
// CHECK-FIXES: #define my_macro() assert(0 == 1)

constexpr bool myfunc(int a, int b) { return a * b == 0; }

class A {
public:
  bool method() { return true; }
};

class B {
public:
  constexpr bool method() { return true; }
};

template <class T> void doSomething(T t) {
  assert(myfunc(1, 2));
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be replaced by static_assert() [misc-static-assert]
  // CHECK-FIXES: {{^  }}static_assert(myfunc(1, 2), "");

  assert(t.method());
  // CHECK-FIXES: {{^  }}assert(t.method());
}

int main() {
  my_macro();
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be
  // CHECK-FIXES: {{^  }}my_macro();

  assert(myfunc(1, 2) && (3 == 4));
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be
  // CHECK-FIXES: {{^  }}static_assert(myfunc(1, 2) && (3 == 4), "");

  int x = 1;
  assert(x == 0);
  // CHECK-FIXES: {{^  }}assert(x == 0);

  A a;
  B b;

  doSomething<A>(a);
  doSomething<B>(b);

  assert(false);
  // CHECK-FIXES: {{^  }}assert(false);

  assert(ZERO_MACRO);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be
  // CHECK-FIXES: {{^  }}static_assert(ZERO_MACRO, "");

  assert(0 && "Don't report me!");
  // CHECK-FIXES: {{^  }}assert(0 && "Don't report me!");

  assert(false && "Don't report me!");
  // CHECK-FIXES: {{^  }}assert(false && "Don't report me!");

  assert(ZERO_MACRO && "Report me!");
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be
  // CHECK-FIXES: {{^  }}static_assert(ZERO_MACRO , "Report me!");

  assert(10==5 && "Report me!");
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be
  // CHECK-FIXES: {{^  }}static_assert(10==5 , "Report me!");

  return 0;
}
OpenPOWER on IntegriCloud