Clang Project

clang_source_code/test/SemaCXX/warn-max-unsigned-zero.cpp
1// RUN: %clang_cc1 -fsyntax-only -Wmax-unsigned-zero -verify %s -std=c++11
2// RUN: %clang_cc1 -fsyntax-only -Wmax-unsigned-zero %s -std=c++11 -fdiagnostics-parseable-fixits 2>&1 | FileCheck %s
3
4namespace std {
5template <typename T>
6T max(const T &, const T &);
7}
8
9void test(unsigned u) {
10  auto a = std::max(55u, 0u);
11  // expected-warning@-1{{taking the max of a value and unsigned zero is always equal to the other value}}
12  // expected-note@-2{{remove call to max function and unsigned zero argument}}
13  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:12-[[@LINE-3]]:20}:""
14  // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:24-[[@LINE-4]]:28}:""
15  auto b = std::max(u, 0u);
16  // expected-warning@-1{{taking the max of a value and unsigned zero is always equal to the other value}}
17  // expected-note@-2{{remove call to max function and unsigned zero argument}}
18  // fix-it:"/usr/local/google/home/rtrieu/clang/open/llvm/tools/clang/test/SemaCXX/warn-max-unsigned-zero.cpp":{13:12-13:20}:""
19  // fix-it:"/usr/local/google/home/rtrieu/clang/open/llvm/tools/clang/test/SemaCXX/warn-max-unsigned-zero.cpp":{13:22-13:26}:""
20  auto c = std::max(0u, 55u);
21  // expected-warning@-1{{taking the max of unsigned zero and a value is always equal to the other value}}
22  // expected-note@-2{{remove call to max function and unsigned zero argument}}
23  // fix-it:"/usr/local/google/home/rtrieu/clang/open/llvm/tools/clang/test/SemaCXX/warn-max-unsigned-zero.cpp":{16:12-16:20}:""
24  // fix-it:"/usr/local/google/home/rtrieu/clang/open/llvm/tools/clang/test/SemaCXX/warn-max-unsigned-zero.cpp":{16:21-16:24}:""
25  auto d = std::max(0u, u);
26  // expected-warning@-1{{taking the max of unsigned zero and a value is always equal to the other value}}
27  // expected-note@-2{{remove call to max function and unsigned zero argument}}
28  // fix-it:"/usr/local/google/home/rtrieu/clang/open/llvm/tools/clang/test/SemaCXX/warn-max-unsigned-zero.cpp":{19:12-19:20}:""
29  // fix-it:"/usr/local/google/home/rtrieu/clang/open/llvm/tools/clang/test/SemaCXX/warn-max-unsigned-zero.cpp":{19:21-19:24}:""
30}
31
32void negative_test(signed s) {
33  auto a = std::max(0, s);
34  auto b = std::max(s, 0);
35  auto c = std::max(22, 0);
36  auto d = std::max(0, 22);
37}
38
39template <unsigned x>
40unsigned template_test() {
41  return std::max(x, 0u);
42  // expected-warning@-1{{taking the max of a value and unsigned zero is always equal to the other value}}
43  // expected-note@-2{{remove call to max function and unsigned zero argument}}
44  // fix-it:"/usr/local/google/home/rtrieu/clang/open/llvm/tools/clang/test/SemaCXX/warn-max-unsigned-zero.cpp":{33:10-33:18}:""
45  // fix-it:"/usr/local/google/home/rtrieu/clang/open/llvm/tools/clang/test/SemaCXX/warn-max-unsigned-zero.cpp":{33:20-33:24}:""
46}
47
48int a = template_test<0>() + template_test<1>() + template_test<2>();
49
50#define comp(x,y) std::max(x, y)
51
52int b = comp(0, 1);
53int c = comp(0u, 1u);
54int d = comp(2u, 0u);
55
56