12 The list of clang-tidy checks <checks/list>
13 Clang-tidy IDE/Editor Integrations <Integrations>
14 Getting Involved <Contributing>
16 :program:`clang-tidy` is a clang-based C++ "linter" tool. Its purpose is to
17 provide an extensible framework for diagnosing and fixing typical programming
18 errors, like style violations, interface misuse, or bugs that can be deduced via
19 static analysis. :program:`clang-tidy` is modular and provides a convenient
20 interface for writing new checks.
26 :program:`clang-tidy` is a `LibTooling`_-based tool, and it's easier to work
27 with if you set up a compile command database for your project (for an example
28 of how to do this, see `How To Setup Tooling For LLVM`_). You can also specify
29 compilation options on the command line after ``--``:
31 .. code-block:: console
33 $ clang-tidy test.cpp -- -Imy_project/include -DMY_DEFINES ...
35 :program:`clang-tidy` has its own checks and can also run Clang Static Analyzer
36 checks. Each check has a name and the checks to run can be chosen using the
37 ``-checks=`` option, which specifies a comma-separated list of positive and
38 negative (prefixed with ``-``) globs. Positive globs add subsets of checks, and
39 negative globs remove them. For example,
41 .. code-block:: console
43 $ clang-tidy test.cpp -checks=-*,clang-analyzer-*,-clang-analyzer-cplusplus*
45 will disable all default checks (``-*``) and enable all ``clang-analyzer-*``
46 checks except for ``clang-analyzer-cplusplus*`` ones.
48 The ``-list-checks`` option lists all the enabled checks. When used without
49 ``-checks=``, it shows checks enabled by default. Use ``-checks=*`` to see all
50 available checks or with any other value of ``-checks=`` to see which checks are
51 enabled by this value.
53 .. _checks-groups-table:
55 There are currently the following groups of checks:
57 ====================== =========================================================
58 Name prefix Description
59 ====================== =========================================================
60 ``abseil-`` Checks related to Abseil library.
61 ``altera-`` Checks related to OpenCL programming for FPGAs.
62 ``android-`` Checks related to Android.
63 ``boost-`` Checks related to Boost library.
64 ``bugprone-`` Checks that target bug-prone code constructs.
65 ``cert-`` Checks related to CERT Secure Coding Guidelines.
66 ``clang-analyzer-`` Clang Static Analyzer checks.
67 ``concurrency-`` Checks related to concurrent programming (including
68 threads, fibers, coroutines, etc.).
69 ``cppcoreguidelines-`` Checks related to C++ Core Guidelines.
70 ``darwin-`` Checks related to Darwin coding conventions.
71 ``fuchsia-`` Checks related to Fuchsia coding conventions.
72 ``google-`` Checks related to Google coding conventions.
73 ``hicpp-`` Checks related to High Integrity C++ Coding Standard.
74 ``linuxkernel-`` Checks related to the Linux Kernel coding conventions.
75 ``llvm-`` Checks related to the LLVM coding conventions.
76 ``llvmlibc-`` Checks related to the LLVM-libc coding standards.
77 ``misc-`` Checks that we didn't have a better category for.
78 ``modernize-`` Checks that advocate usage of modern (currently "modern"
79 means "C++11") language constructs.
80 ``mpi-`` Checks related to MPI (Message Passing Interface).
81 ``objc-`` Checks related to Objective-C coding conventions.
82 ``openmp-`` Checks related to OpenMP API.
83 ``performance-`` Checks that target performance-related issues.
84 ``portability-`` Checks that target portability-related issues that don't
85 relate to any particular coding style.
86 ``readability-`` Checks that target readability-related issues that don't
87 relate to any particular coding style.
88 ``zircon-`` Checks related to Zircon kernel coding conventions.
89 ====================== =========================================================
91 Clang diagnostics are treated in a similar way as check diagnostics. Clang
92 diagnostics are displayed by :program:`clang-tidy` and can be filtered out using
93 the ``-checks=`` option. However, the ``-checks=`` option does not affect
94 compilation arguments, so it cannot turn on Clang warnings which are not
95 already turned on in the build configuration. The ``-warnings-as-errors=``
96 option upgrades any warnings emitted under the ``-checks=`` flag to errors (but
97 it does not enable any checks itself).
99 Clang diagnostics have check names starting with ``clang-diagnostic-``.
100 Diagnostics which have a corresponding warning option, are named
101 ``clang-diagnostic-<warning-option>``, e.g. Clang warning controlled by
102 ``-Wliteral-conversion`` will be reported with check name
103 ``clang-diagnostic-literal-conversion``.
105 The ``-fix`` flag instructs :program:`clang-tidy` to fix found errors if
106 supported by corresponding checks.
108 An overview of all the command-line options:
110 .. code-block:: console
113 USAGE: clang-tidy [options] <source0> [... <sourceN>]
119 --help - Display available options (--help-hidden for more)
120 --help-list - Display list of available options (--help-list-hidden for more)
121 --version - Display the version of this program
125 --checks=<string> - Comma-separated list of globs with optional '-'
126 prefix. Globs are processed in order of
127 appearance in the list. Globs without '-'
128 prefix add checks with matching names to the
129 set, globs with the '-' prefix remove checks
130 with matching names from the set of enabled
131 checks. This option's value is appended to the
132 value of the 'Checks' option in .clang-tidy
134 --config=<string> - Specifies a configuration in YAML/JSON format:
135 -config="{Checks: '*',
136 CheckOptions: {x: y}}"
137 When the value is empty, clang-tidy will
138 attempt to find a file named .clang-tidy for
139 each source file in its parent directories.
140 --config-file=<string> - Specify the path of .clang-tidy or custom config file:
141 e.g. --config-file=/some/path/myTidyConfigFile
142 This option internally works exactly the same way as
143 --config option after reading specified config file.
144 Use either --config-file or --config, not both.
145 --dump-config - Dumps configuration in the YAML format to
146 stdout. This option can be used along with a
147 file name (and '--' if the file is outside of a
148 project with configured compilation database).
149 The configuration used for this file will be
151 Use along with -checks=* to include
152 configuration of all checks.
153 --enable-check-profile - Enable per-check timing profiles, and print a
155 --enable-module-headers-parsing - Enables preprocessor-level module header parsing
156 for C++20 and above, empowering specific checks
157 to detect macro definitions within modules. This
158 feature may cause performance and parsing issues
159 and is therefore considered experimental.
160 --explain-config - For each enabled check explains, where it is
161 enabled, i.e. in clang-tidy binary, command
162 line or a specific configuration file.
163 --export-fixes=<filename> - YAML file to store suggested fixes in. The
164 stored fixes can be applied to the input source
165 code with clang-apply-replacements.
166 --extra-arg=<string> - Additional argument to append to the compiler command line
167 --extra-arg-before=<string> - Additional argument to prepend to the compiler command line
168 --fix - Apply suggested fixes. Without -fix-errors
169 clang-tidy will bail out if any compilation
171 --fix-errors - Apply suggested fixes even if compilation
172 errors were found. If compiler errors have
173 attached fix-its, clang-tidy will apply them as
175 --fix-notes - If a warning has no fix, but a single fix can
176 be found through an associated diagnostic note,
178 Specifying this flag will implicitly enable the
180 --format-style=<string> - Style for formatting code around applied fixes:
181 - 'none' (default) turns off formatting
182 - 'file' (literally 'file', not a placeholder)
183 uses .clang-format file in the closest parent
185 - '{ <json> }' specifies options inline, e.g.
186 -format-style='{BasedOnStyle: llvm, IndentWidth: 8}'
187 - 'llvm', 'google', 'webkit', 'mozilla'
188 See clang-format documentation for the up-to-date
189 information about formatting styles and options.
190 This option overrides the 'FormatStyle` option in
191 .clang-tidy file, if any.
192 --header-filter=<string> - Regular expression matching the names of the
193 headers to output diagnostics from. Diagnostics
194 from the main file of each translation unit are
196 Can be used together with -line-filter.
197 This option overrides the 'HeaderFilterRegex'
198 option in .clang-tidy file, if any.
199 --line-filter=<string> - List of files with line ranges to filter the
200 warnings. Can be used together with
201 -header-filter. The format of the list is a
202 JSON array of objects:
204 {"name":"file1.cpp","lines":[[1,3],[5,7]]},
207 --list-checks - List all enabled checks and exit. Use with
208 -checks=* to list all available checks.
209 --load=<pluginfilename> - Load the specified plugin
210 -p <string> - Build path
211 --quiet - Run clang-tidy in quiet mode. This suppresses
212 printing statistics about ignored warnings and
213 warnings treated as errors if the respective
214 options are specified.
215 --store-check-profile=<prefix> - By default reports are printed in tabulated
216 format to stderr. When this option is passed,
217 these per-TU profiles are instead stored as JSON.
218 --system-headers - Display the errors from system headers.
219 This option overrides the 'SystemHeaders' option
220 in .clang-tidy file, if any.
221 --use-color - Use colors in diagnostics. If not set, colors
222 will be used if the terminal connected to
223 standard output supports colors.
224 This option overrides the 'UseColor' option in
225 .clang-tidy file, if any.
226 --verify-config - Check the config files to ensure each check and
227 option is recognized.
228 --vfsoverlay=<filename> - Overlay the virtual filesystem described by file
229 over the real file system.
230 --warnings-as-errors=<string> - Upgrades warnings to errors. Same format as
232 This option's value is appended to the value of
233 the 'WarningsAsErrors' option in .clang-tidy
236 -p <build-path> is used to read a compile command database.
238 For example, it can be a CMake build directory in which a file named
239 compile_commands.json exists (use -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
240 CMake option to get this output). When no build path is specified,
241 a search for compile_commands.json will be attempted through all
242 parent paths of the first input file . See:
243 https://clang.llvm.org/docs/HowToSetupToolingForLLVM.html for an
244 example of setting up Clang Tooling on a source tree.
246 <source0> ... specify the paths of source files. These paths are
247 looked up in the compile command database. If the path of a file is
248 absolute, it needs to point into CMake's source tree. If the path is
249 relative, the current working directory needs to be in the CMake
250 source tree and the file must be in a subdirectory of the current
251 working directory. "./" prefixes in the relative files will be
252 automatically removed, but the rest of a relative path must be a
253 suffix of a path in the compile command database.
257 clang-tidy attempts to read configuration for each source file from a
258 .clang-tidy file located in the closest parent directory of the source
259 file. The .clang-tidy file is specified in YAML format. If any configuration
260 options have a corresponding command-line option, command-line option takes
263 The following configuration options may be used in a .clang-tidy file:
265 CheckOptions - List of key-value pairs defining check-specific
268 some-check.SomeOption: 'some value'
269 Checks - Same as '--checks'. Additionally, the list of
270 globs can be specified as a list instead of a
272 ExtraArgs - Same as '--extra-args'.
273 ExtraArgsBefore - Same as '--extra-args-before'.
274 FormatStyle - Same as '--format-style'.
275 HeaderFileExtensions - File extensions to consider to determine if a
276 given diagnostic is located in a header file.
277 HeaderFilterRegex - Same as '--header-filter-regex'.
278 ImplementationFileExtensions - File extensions to consider to determine if a
279 given diagnostic is located in an
281 InheritParentConfig - If this option is true in a config file, the
282 configuration file in the parent directory
283 (if any exists) will be taken and the current
284 config file will be applied on top of the
286 SystemHeaders - Same as '--system-headers'.
287 UseColor - Same as '--use-color'.
288 User - Specifies the name or e-mail of the user
289 running clang-tidy. This option is used, for
290 example, to place the correct user name in
291 TODO() comments in the relevant check.
292 WarningsAsErrors - Same as '--warnings-as-errors'.
294 The effective configuration can be inspected using --dump-config:
296 $ clang-tidy --dump-config
298 Checks: '-*,some-check'
300 HeaderFileExtensions: ['', 'h','hh','hpp','hxx']
301 ImplementationFileExtensions: ['c','cc','cpp','cxx']
302 HeaderFilterRegex: ''
304 InheritParentConfig: true
307 some-check.SomeOption: 'some value'
310 .. _clang-tidy-nolint:
312 Suppressing Undesired Diagnostics
313 =================================
315 :program:`clang-tidy` diagnostics are intended to call out code that does not
316 adhere to a coding standard, or is otherwise problematic in some way. However,
317 if the code is known to be correct, it may be useful to silence the warning.
318 Some clang-tidy checks provide a check-specific way to silence the diagnostics,
319 e.g. `bugprone-use-after-move <checks/bugprone/use-after-move.html>`_ can be
320 silenced by re-initializing the variable after it has been moved out,
321 `bugprone-string-integer-assignment
322 <checks/bugprone/string-integer-assignment.html>`_ can be suppressed by
323 explicitly casting the integer to ``char``,
324 `readability-implicit-bool-conversion
325 <checks/readability/implicit-bool-conversion.html>`_ can also be suppressed by
326 using explicit casts, etc.
328 If a specific suppression mechanism is not available for a certain warning, or
329 its use is not desired for some reason, :program:`clang-tidy` has a generic
330 mechanism to suppress diagnostics using ``NOLINT``, ``NOLINTNEXTLINE``, and
331 ``NOLINTBEGIN`` ... ``NOLINTEND`` comments.
333 The ``NOLINT`` comment instructs :program:`clang-tidy` to ignore warnings on the
334 *same line* (it doesn't apply to a function, a block of code or any other
335 language construct; it applies to the line of code it is on). If introducing the
336 comment on the same line would change the formatting in an undesired way, the
337 ``NOLINTNEXTLINE`` comment allows suppressing clang-tidy warnings on the *next
338 line*. The ``NOLINTBEGIN`` and ``NOLINTEND`` comments allow suppressing
339 clang-tidy warnings on *multiple lines* (affecting all lines between the two
342 All comments can be followed by an optional list of check names in parentheses
343 (see below for the formal syntax). The list of check names supports globbing,
344 with the same format and semantics as for enabling checks. Note: negative globs
345 are ignored here, as they would effectively re-activate the warning.
352 // Suppress all the diagnostics for the line
353 Foo(int param); // NOLINT
355 // Consider explaining the motivation to suppress the warning
356 Foo(char param); // NOLINT: Allow implicit conversion from `char`, because <some valid reason>
358 // Silence only the specified checks for the line
359 Foo(double param); // NOLINT(google-explicit-constructor, google-runtime-int)
361 // Silence all checks from the `google` module
362 Foo(bool param); // NOLINT(google*)
364 // Silence all checks ending with `-avoid-c-arrays`
365 int array[10]; // NOLINT(*-avoid-c-arrays)
367 // Silence only the specified diagnostics for the next line
368 // NOLINTNEXTLINE(google-explicit-constructor, google-runtime-int)
371 // Silence all checks from the `google` module for the next line
372 // NOLINTNEXTLINE(google*)
375 // Silence all checks ending with `-avoid-c-arrays` for the next line
376 // NOLINTNEXTLINE(*-avoid-c-arrays)
379 // Silence only the specified checks for all lines between the BEGIN and END
380 // NOLINTBEGIN(google-explicit-constructor, google-runtime-int)
383 // NOLINTEND(google-explicit-constructor, google-runtime-int)
385 // Silence all checks from the `google` module for all lines between the BEGIN and END
386 // NOLINTBEGIN(google*)
388 // NOLINTEND(google*)
390 // Silence all checks ending with `-avoid-c-arrays` for all lines between the BEGIN and END
391 // NOLINTBEGIN(*-avoid-c-arrays)
393 // NOLINTEND(*-avoid-c-arrays)
396 The formal syntax of ``NOLINT``, ``NOLINTNEXTLINE``, and ``NOLINTBEGIN`` ...
397 ``NOLINTEND`` is the following:
403 lint-command lint-args
406 **(** check-name-list **)**
410 check-name-list **,** *check-name*
418 Note that whitespaces between
419 ``NOLINT``/``NOLINTNEXTLINE``/``NOLINTBEGIN``/``NOLINTEND`` and the opening
420 parenthesis are not allowed (in this case the comment will be treated just as
421 ``NOLINT``/``NOLINTNEXTLINE``/``NOLINTBEGIN``/``NOLINTEND``), whereas in the
422 check names list (inside the parentheses), whitespaces can be used and will be
425 All ``NOLINTBEGIN`` comments must be paired by an equal number of ``NOLINTEND``
426 comments. Moreover, a pair of comments must have matching arguments -- for
427 example, ``NOLINTBEGIN(check-name)`` can be paired with
428 ``NOLINTEND(check-name)`` but not with ``NOLINTEND`` `(zero arguments)`.
429 :program:`clang-tidy` will generate a ``clang-tidy-nolint`` error diagnostic if
430 any ``NOLINTBEGIN``/``NOLINTEND`` comment violates these requirements.
432 .. _LibTooling: https://clang.llvm.org/docs/LibTooling.html
433 .. _How To Setup Tooling For LLVM: https://clang.llvm.org/docs/HowToSetupToolingForLLVM.html