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"
5void 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
12class A { A(int i); };
13// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: single-argument constructors must be marked explicit
14
15class B { B(int i); }; // NOLINT
16
17class C { C(int i); }; // NOLINT(for-some-other-check)
18// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: single-argument constructors must be marked explicit
19
20class C1 { C1(int i); }; // NOLINT()
21// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: single-argument constructors must be marked explicit
22
23class C2 { C2(int i); }; // NOLINT(*)
24
25class C3 { C3(int i); }; // NOLINT(not-closed-bracket-is-treated-as-skip-all
26
27class C4 { C4(int i); }; // NOLINT(google-explicit-constructor)
28
29class C5 { C5(int i); }; // NOLINT(some-check, google-explicit-constructor)
30
31class C6 { C6(int i); }; // NOLINT without-brackets-skip-all
32
33// Other NOLINT* types (e.g. NEXTLINE) should not be misconstrued as a NOLINT:
34class 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
39class 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
44class 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:
48class C10 { C10(int i); }; /* NOLINT */
49/* NOLINT */ class C11 { C11(int i); };
50
51// Multiple NOLINTs in the same comment:
52class C12 { C12(int i); }; // NOLINT(some-other-check) NOLINT(google-explicit-constructor)
53class C13 { C13(int i); }; // NOLINT(google-explicit-constructor) NOLINT(some-other-check)
54class C14 { C14(int i); }; // NOLINTNEXTLINE(some-other-check) NOLINT(google-explicit-constructor)
55
56// NOLINTNEXTLINE(google-explicit-constructor) NOLINT(some-other-check)
57class C15 { C15(int i); };
58
59// Any text after a NOLINT expression is treated as a comment:
60class C16 { C16(int i); }; // NOLINT: suppress check because <reason>
61class C17 { C17(int i); }; // NOLINT(google-explicit-constructor): suppress check because <reason>
62
63// NOLINT must appear in its entirety on one line:
64class C18 { C18(int i); }; /* NOL
65INT */
66// CHECK-MESSAGES: :[[@LINE-2]]:13: warning: single-argument constructors must be marked explicit
67
68/* NO
69LINT */ 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:
73class C20 { C20(int i); }; // NOLINT( google-explicit-constructor )
74class C21 { C21(int i); }; // NOLINT( google-explicit-constructor , some-other-check )
75class 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:
79class C23 { C23(int i); }; // NOLINT (some-other-check)
80
81void 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); };
89MACRO(D)
90// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: single-argument constructors must be marked explicit
91MACRO(E) // NOLINT
92
93#define MACRO_NOARG class F { F(int i); };
94MACRO_NOARG // NOLINT
95
96#define MACRO_NOLINT class G { G(int i); }; // NOLINT
97MACRO_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
106MACRO_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
114MACRO_SUPPRESS_DIAG_FOR_ARG_2(G2)
115
116#define DOUBLE_MACRO MACRO(H) // NOLINT
117DOUBLE_MACRO
118
119class D1 { D1(int x); }; // NOLINT(google*)
120class D2 { D2(int x); }; // NOLINT(*explicit-constructor)
121class D3 { D3(int x); }; // NOLINT(*explicit*)
122class D4 { D4(int x); }; // NOLINT(-explicit-constructor)
123// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: single-argument constructors must be marked explicit
124class D5 { D5(int x); }; // NOLINT(google*,-google*)
125class D6 { D6(int x); }; // NOLINT(*,-google*)
126
127int 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
130int 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
133int array3[10]; // NOLINT(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays)
134int array4[10]; // NOLINT(*-avoid-c-arrays)
135
136// CHECK-MESSAGES: Suppressed 36 warnings (36 NOLINT)
137

source code of clang-tools-extra/test/clang-tidy/infrastructure/nolint.cpp