xref: /llvm-project/clang-tools-extra/test/clang-tidy/infrastructure/nolint.cpp (revision 5fa742eeed0821baf864d23f237ff1b481e1ae11)
1 // REQUIRES: static-analyzer
2 // RUN: %check_clang_tidy %s google-explicit-constructor,clang-diagnostic-unused-variable,clang-analyzer-core.UndefinedBinaryOperatorResult,modernize-avoid-c-arrays,cppcoreguidelines-avoid-c-arrays,cppcoreguidelines-pro-type-member-init %t -- -extra-arg=-Wunused-variable -- -I%S/Inputs/nolint
3 
4 #include "trigger_warning.h"
5 void I(int& Out) {
6   int In;
7   A1(In, Out);
8 }
9 // CHECK-MESSAGES-NOT: trigger_warning.h:{{.*}} warning
10 // CHECK-MESSAGES-NOT: :[[@LINE-4]]:{{.*}} note
11 
12 class A { A(int i); };
13 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: single-argument constructors must be marked explicit
14 
15 class B { B(int i); }; // NOLINT
16 
17 class C { C(int i); }; // NOLINT(for-some-other-check)
18 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: single-argument constructors must be marked explicit
19 
20 class C1 { C1(int i); }; // NOLINT()
21 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: single-argument constructors must be marked explicit
22 
23 class C2 { C2(int i); }; // NOLINT(*)
24 
25 class C3 { C3(int i); }; // NOLINT(not-closed-bracket-is-treated-as-skip-all
26 
27 class C4 { C4(int i); }; // NOLINT(google-explicit-constructor)
28 
29 class C5 { C5(int i); }; // NOLINT(some-check, google-explicit-constructor)
30 
31 class C6 { C6(int i); }; // NOLINT without-brackets-skip-all
32 
33 // Other NOLINT* types (e.g. NEXTLINE) should not be misconstrued as a NOLINT:
34 class C7 { C7(int i); }; // NOLINTNEXTLINE
35 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: single-argument constructors must be marked explicit
36 
37 // NOLINT must be UPPERCASE:
38 // NOLINTnextline
39 class C8 { C8(int i); }; // nolint
40 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: single-argument constructors must be marked explicit
41 
42 // Unrecognized marker:
43 // NOLINTNEXTLINEXYZ
44 class C9 { C9(int i); }; // NOLINTXYZ
45 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: single-argument constructors must be marked explicit
46 
47 // C-style comments are supported:
48 class C10 { C10(int i); }; /* NOLINT */
49 /* NOLINT */ class C11 { C11(int i); };
50 
51 // Multiple NOLINTs in the same comment:
52 class C12 { C12(int i); }; // NOLINT(some-other-check) NOLINT(google-explicit-constructor)
53 class C13 { C13(int i); }; // NOLINT(google-explicit-constructor) NOLINT(some-other-check)
54 class C14 { C14(int i); }; // NOLINTNEXTLINE(some-other-check) NOLINT(google-explicit-constructor)
55 
56 // NOLINTNEXTLINE(google-explicit-constructor) NOLINT(some-other-check)
57 class C15 { C15(int i); };
58 
59 // Any text after a NOLINT expression is treated as a comment:
60 class C16 { C16(int i); }; // NOLINT: suppress check because <reason>
61 class C17 { C17(int i); }; // NOLINT(google-explicit-constructor): suppress check because <reason>
62 
63 // NOLINT must appear in its entirety on one line:
64 class C18 { C18(int i); }; /* NOL
65 INT */
66 // CHECK-MESSAGES: :[[@LINE-2]]:13: warning: single-argument constructors must be marked explicit
67 
68 /* NO
69 LINT */ class C19 { C19(int i); };
70 // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: single-argument constructors must be marked explicit
71 
72 // Spaces between items in the comma-separated check list are ignroed:
73 class C20 { C20(int i); }; // NOLINT( google-explicit-constructor )
74 class C21 { C21(int i); }; // NOLINT( google-explicit-constructor , some-other-check )
75 class C22 { C22(int i); }; // NOLINT(google-explicit- constructor)
76 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: single-argument constructors must be marked explicit
77 
78 // If there is a space between "NOLINT" and the bracket, it is treated as a regular NOLINT:
79 class C23 { C23(int i); }; // NOLINT (some-other-check)
80 
81 void f() {
82   int i;
83 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: unused variable 'i' [clang-diagnostic-unused-variable]
84   int j; // NOLINT
85   int k; // NOLINT(clang-diagnostic-unused-variable)
86 }
87 
88 #define MACRO(X) class X { X(int i); };
89 MACRO(D)
90 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: single-argument constructors must be marked explicit
91 MACRO(E) // NOLINT
92 
93 #define MACRO_NOARG class F { F(int i); };
94 MACRO_NOARG // NOLINT
95 
96 #define MACRO_NOLINT class G { G(int i); }; // NOLINT
97 MACRO_NOLINT
98 
99 // Check that we can suppress diagnostics about macro arguments (as opposed to
100 // diagnostics about the macro contents itself).
101 #define MACRO_SUPPRESS_DIAG_FOR_ARG_1(X)                \
102   class X {                                             \
103     X(int i); /* NOLINT(google-explicit-constructor) */ \
104   };
105 
106 MACRO_SUPPRESS_DIAG_FOR_ARG_1(G1)
107 
108 #define MACRO_SUPPRESS_DIAG_FOR_ARG_2(X)                          \
109   struct X { /* NOLINT(cppcoreguidelines-pro-type-member-init) */ \
110     int a = 0;                                                    \
111     int b;                                                        \
112   };
113 
114 MACRO_SUPPRESS_DIAG_FOR_ARG_2(G2)
115 
116 #define DOUBLE_MACRO MACRO(H) // NOLINT
117 DOUBLE_MACRO
118 
119 class D1 { D1(int x); }; // NOLINT(google*)
120 class D2 { D2(int x); }; // NOLINT(*explicit-constructor)
121 class D3 { D3(int x); }; // NOLINT(*explicit*)
122 class D4 { D4(int x); }; // NOLINT(-explicit-constructor)
123 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: single-argument constructors must be marked explicit
124 class D5 { D5(int x); }; // NOLINT(google*,-google*)
125 class D6 { D6(int x); }; // NOLINT(*,-google*)
126 
127 int array1[10];
128 // CHECK-MESSAGES: :[[@LINE-1]]:1: warning: do not declare C-style arrays, use 'std::array' instead [cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays]
129 
130 int array2[10];  // NOLINT(cppcoreguidelines-avoid-c-arrays)
131 // CHECK-MESSAGES: :[[@LINE-1]]:1: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
132 
133 int array3[10];  // NOLINT(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays)
134 int array4[10];  // NOLINT(*-avoid-c-arrays)
135 
136 // CHECK-MESSAGES: Suppressed 36 warnings (36 NOLINT)
137