1194 Commits

Author SHA1 Message Date
Harshil Solanki
6127e46ff8
[clang-tidy][docs] Add documentation for Clang-Tidy Automation (#153166)
Resolves #90772

---------

Co-authored-by: Baranov Victor <bar.victor.2002@gmail.com>
2025-08-19 23:28:26 +03:00
Balázs Kéri
a0f325bd41
[clang-tidy] Added check 'misc-override-with-different-visibility' (#140086) 2025-08-18 11:00:42 +02:00
Baranov Victor
dff8dac9dc
[clang-tidy][docs] Add description of "clang-diagnostic-error" (#153870)
This helps better distinguish warnings that could be disabled via
`.clang-tidy` config (like `clang-diagnostic-literal-conversion`) from
errors that could not be suppressed at all (like
`clang-diagnostic-error`) because it's a hard compiler error.
2025-08-18 00:18:32 +03:00
Carlos Galvez
3b6d8798ba
[clang-tidy][doc] Improve documentation of the -line-filter flag (#153372)
Fixes #25589

Co-authored-by: Carlos Gálvez <carlos.galvez@zenseact.com>
2025-08-14 07:55:20 +02:00
Baranov Victor
c5e6938c67
[clang-tidy][docs][NFC] Add limitation of variable lifetimes in performance-unnecessary-copy-initialization (#151862)
Addresses https://github.com/llvm/llvm-project/issues/150189.
2025-08-05 10:41:15 +03:00
Baranov Victor
0cf7377a1e
[clang-tidy][docs][NFC] Make uniform "Limitations" sections across all checks (#151863) 2025-08-05 10:40:55 +03:00
tigbr
b16fe132c6
[clang-tidy] Fix bugprone-tagged-union-member-count false-positive (#135831)
This patch implements a fix for the false-positive case described in
[issue #134840](https://github.com/llvm/llvm-project/issues/134840) for
the
[bugprone-tagged-union-member-count](https://clang.llvm.org/extra/clang-tidy/checks/bugprone/tagged-union-member-count.html)
clang-tidy check.

The example given in the linked issue was the following:

```C
#include <pthread.h>

typedef enum {
  MYENUM_ONE,
  MYENUM_TWO,
} myEnumT;

typedef struct {
  pthread_mutex_t mtx;       
  myEnumT         my_enum;
} myTypeT;
```

The
[bugprone-tagged-union-member-count](https://clang.llvm.org/extra/clang-tidy/checks/bugprone/tagged-union-member-count.html)
check emits the following a warning for this struct:

```
<source>:8:9: warning: tagged union has more data members (3) than tags (2)! [bugprone-tagged-union-member-count]
    8 | typedef struct {
      |         ^
1 warning generated.
```

The issue is that `pthread_mutex_t` can be a union behind a typedef like
the following:

```C
typedef union
{
  struct __pthread_mutex_s __data;
  char __size[__SIZEOF_PTHREAD_MUTEX_T];
  long int __align;
} pthread_mutex_t;
```

From the checker's point of view therefore `myTypeT` contains a data
member with an enum type and another data member with a union type and
starts analyzing it like a user-defined tagged union.

The proposed solution is that the types from system headers and the std
namespace are no longer candidates to be the enum part or the union part
of a user-defined tagged union. This filtering for enums and the std
namespace may not be strictly necessary in this example, however I added
it preemptively out of (perhaps unnecessary) caution.

Fixes https://github.com/llvm/llvm-project/issues/134840.
2025-08-03 14:33:29 +03:00
Balázs Kéri
6f2cf6b0ac
[clang-tidy] Add check 'bugprone-invalid-enum-default-initialization' (#136823) 2025-07-31 09:00:34 +02:00
Juan Besa
4d259de2ae
[clang-tidy] Add IgnoreAliasing option to readability-qualified-auto check (#147060)
`readability-qualified-auto` check currently looks at the unsugared
type, skipping any typedefs, to determine if the variable is a
pointer-type. This may not be the desired behaviour, in particular when
the type depends on compilation flags.
For example

```
 #if CONDITION
      using Handler = int *;
  #else
      using Handler = uint64_t;
  #endif
```

A more common example is some implementations of `std::array` use
pointers as iterators.

This introduces the IgnoreAliasing option so that
`readability-qualified-auto` does not look beyond typedefs.

---------

Co-authored-by: juanbesa <juanbesa@devvm33299.lla0.facebook.com>
Co-authored-by: Kazu Hirata <kazu@google.com>
Co-authored-by: EugeneZelenko <eugene.zelenko@gmail.com>
Co-authored-by: Baranov Victor <bar.victor.2002@gmail.com>
2025-07-28 18:20:02 +03:00
Jacques Pienaar
3d5b18af17
[clang-tidy] Fix typo in doc (#150424) 2025-07-24 16:01:05 +02:00
Jacques Pienaar
3feb6f9715
[clang-tidy] Add MLIR check for old op builder usage. (#149148)
Upstream is moving towards new create method invocation, add check to flag old
usage that will be deprecated.

---------

Co-authored-by: Baranov Victor <bar.victor.2002@gmail.com>
Co-authored-by: EugeneZelenko <eugene.zelenko@gmail.com>
2025-07-24 15:48:05 +02:00
Kazu Hirata
f0bbe73cf1
[clang-tidy] Proofread Contributing.rst (#149831) 2025-07-21 14:59:35 -07:00
Baranov Victor
4e6157f784
[clang-tidy][NFC] Add mention of running 'clang-tidy' on changes in Contributing.rst (#148547)
Follow up to https://github.com/llvm/llvm-project/pull/147793.

_Originally suggested by carlosgalvezp in
https://github.com/llvm/llvm-project/pull/147793#issuecomment-3059021433_
2025-07-18 07:41:39 +03:00
Victor Chernyakin
59b39c0031
[clang-tidy] Add new check: readability-use-concise-preprocessor-directives (#146830)
Closes #132561.

This is a check that rewrites `#if`s and `#elif`s like so:

```cpp
#if  defined(MEOW) // -> #ifdef  MEOW
#if !defined(MEOW) // -> #ifndef MEOW
```

And, since C23 and C++23:

```cpp
#elif  defined(MEOW) // -> #elifdef  MEOW
#elif !defined(MEOW) // -> #elifndef MEOW
```
2025-07-13 19:23:27 +03:00
Baranov Victor
2f4a804e6e
[clang-tidy] Add new check llvm-prefer-static-over-anonymous-namespace (#142839)
Finds function and variable declarations inside anonymous namespace and
suggests replacing them with ``static`` declarations.

The check will enforce that
[restrict-visibility](https://llvm.org/docs/CodingStandards.html#restrict-visibility)
rule in LLVM Coding Standards is followed correctly (by adding `static`
to functions instead of putting them in anonimous namespace).

The check has additional levels of "strictness" represented by Options.
By default, the check works in the most relaxed way by giving warning
only for methods and functions defined in anonymous namespaces. Also, It
finds `static` functions that are placed inside anonymous namespace -
there is no point in keeping them inside.
2025-07-12 09:56:30 +03:00
Dmitry Polukhin
a61ea9fd9b
[clang-tidy] Add an option in 'readability-named-parameter' to print names without comment (#147953)
Add InsertPlainNamesInForwardDecls option to readability-named-parameter
check to insert parameter names without comments for forward
declarations only.

When enabled, forward declarations get plain parameter names (e.g., `int
param`) while function definitions continue to use commented names
(e.g., `int /*param*/`). Named parameters in forward decls don't cause
compiler warnings and some developers prefer to have names without
comments but in sync between declarations and the definition.

Default behavior remains unchanged
(InsertPlainNamesInForwardDecls=false).

Example with InsertPlainNamesInForwardDecls=true:
```cpp
// Forward declaration - gets plain name because the definition has name.
void func(int param);
void func(int param) { ... = param; }
```
2025-07-11 11:51:20 +01:00
Victor Chernyakin
fbc4255ffc
[clang-tidy] Teach modernize-type-traits about more type traits (#147074)
These new traits come from various standard versions:

C++14:

 - `tuple_element_t`

C++17:

- `is_placeholder_v`
- `is_bind_expression_v`
- `ratio_equal_v`
- `ratio_not_equal_v`
- `ratio_less_v`
- `ratio_less_equal_v`
- `ratio_greater_v`
- `ratio_greater_equal_v`
- `is_error_code_enum_v`
- `is_error_condition_enum_v`
- `is_execution_policy_v`
- `tuple_size_v`
- `variant_size_v`
- `uses_allocator_v`
- `variant_alternative_t`

C++20:

  - `compare_three_way_result_t`
  - `common_comparison_category_t`
  - `unwrap_ref_decay_t`
  - `unwrap_reference_t`

C++23:

- `is_implicit_lifetime_v`

C++26:

- `is_nothrow_relocatable_v`
- `is_replaceable_v`
- `is_trivially_relocatable_v`
- `is_virtual_base_of_v`

This doesn't add `treat_as_floating_point_v` or `is_clock_v` because
they require more invasive changes; instead I've opened #147072 to track
them.
2025-07-09 08:21:40 +03:00
Dimitrije Dobrota
0f291e5787
[clang-tidy] Add flag to specify an alternative to std::move in cppcoreguidelines-rvalue-reference-param-not-moved (#138757)
Since std::move is nothing more than a cast, part of STL and not the
language itself, it's easy to provide a custom implementation if one
wishes not to include the entirety of <utility>.

Added flag (MoveFunction) provides a way to continue using this
essential check even with the custom implementation of moving.

---------

Co-authored-by: EugeneZelenko <eugene.zelenko@gmail.com>
2025-06-30 22:36:23 +03:00
Dimitrije Dobrota
6a57af8d03
[clang-tidy] Add flag to specify an alternative to std::forward (#138755)
Since std::forward is nothing more than a cast, part of STL and not the
language itself, it's easy to provide a custom implementation if one
wishes not to include the entirety of <utility>.

Added flag (ForwardFunction) provides a way to continue using this
essential check even with the custom implementation of forwarding.

---------

Co-authored-by: EugeneZelenko <eugene.zelenko@gmail.com>
2025-06-30 22:13:33 +03:00
Baranov Victor
a3a60e03e2
[clang-tidy] add new check: modernize-use-scoped-lock (#126434)
Add new clang-tidy check that finds uses of `std::lock_guard` and suggests
replacing them with C++17's more flexible and safer alternative
`std::scoped_lock`.

Here is a small description of how it works for better understanding of
the code:
Two separate AST matchers are registered:

- The first one matches declarations of `std::lock_guard` that are
single in their scope (only one `std::lock_guard` in `CompoundStmt`).
It's an easy case, we can emit warning right away.

- The second one matches `CompoundStmt`'s that have multiple
`std::lock_guard` declarations, which means that we may have consecutive
declarations of `std::lock_guard` that can be replaced by a single
`std::scoped_lock`. In order to ensure that declarations are
consecutive, we need to loop over `Stmt`'s in `CompoundStmt`. Here is a
small example:
```cpp
{
  std::mutex m1, m2;
  std::lock(m1, m2);
  std::lock_guard<std::mutex> l1(m, std::adopt_lock); // first declaration of 'std::lock_guard'
  std::lock_guard<std::mutex> l2(m, std::adopt_lock); // second declaration of 'std::lock_guard' that can be merged with first using 'scoped_lock'
}
```

This PR closes https://github.com/llvm/llvm-project/issues/107839.
2025-06-29 22:34:32 +03:00
Malavika Samak
86026ee623
[clang-tidy] Warn about misuse of sizeof operator in loops. (#143205)
The sizeof operator misuses in loop conditionals can be a source of
bugs. The common misuse is attempting to retrieve the number of elements
in the array by using the sizeof expression and forgetting to divide the
value by the sizeof the array elements. This results in an incorrect
computation of the array length and requires a warning from the sizeof
checker.

Example:
```
 int array[20];

void test_for_loop() {
  // Needs warning.
  for(int i = 0; i < sizeof(array); i++) {
    array[i] = i;
  }
}

void test_while_loop() {

  int count = 0;
  // Needs warning. 
  while(count < sizeof(array)) {
    array[count] = 0;
    count = count + 2;
  }
}
```
rdar://151403083

---------

Co-authored-by: MalavikaSamak <malavika2@apple.com>
2025-06-25 10:04:10 -07:00
Baranov Victor
e435558ff9
[clang-tidy] add 'IgnoreMarcos' option to 'special-member-functions' check (#143550) 2025-06-24 08:39:05 +03:00
Baranov Victor
c594f6e697
Revert "[clang-tidy] Add new check readability-use-numeric-limits" (#145355)
Reverts llvm/llvm-project#127430 due to stable asan buildbot failures:
https://lab.llvm.org/buildbot/#/builders/169
2025-06-23 19:39:01 +03:00
Baranov Victor
05491e0359
[clang-tidy] add 'IgnoreMarcos' option to 'avoid-goto' check (#143554) 2025-06-23 16:27:18 +03:00
Katherine Whitlock
e7dd223ec4
[clang-tidy] Add new check readability-use-numeric-limits (#127430)
The adds a check that replaces specific numeric literals like `32767`
with the equivalent call to `std::numeric_limits` (such as
`std::numeric_limits<int16_t>::max())`.

Partially addresses #34434, but notably does not handle cases listed in
the title post such as `~0` and `-1`.
2025-06-21 21:10:20 +03:00
Baranov Victor
1b5d6ec685
[clang-tidy] count class member initializers as statements in 'readability-function-size' (#131669)
Improve `readability-function-size` by counting class member
initializers as statements.
Relates to https://github.com/llvm/llvm-project/issues/131126
2025-06-21 13:14:19 +03:00
Philipp Jung
669627d0c7
Add check 'cppcoreguidelines-use-enum-class' (#138282)
Warn on non-class enum definitions as suggested by the Core Guidelines:
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Renum-class
2025-06-18 11:02:53 +02:00
Piotr Idzik
3c7df98c7b
[clang-tidy] Add missing colon in the docs of performance-enum-size (#144525)
There is a syntax error in the provided code example - this PR fixes it.

I did a quick search - I could not find similar _typos_.
2025-06-17 21:59:53 +01:00
Dmitry Polukhin
26d082d330
[clang-tidy][performance-unnecessary-value-param] Avoid in coroutines (#140912)
Summary:
Replacing by-value parameters with passing by-reference is not safe for
coroutines because the caller may be executed in parallel with the
callee, which increases the chances of resulting in dangling references
and hard-to-find crashes. See for the reference
[cppcoreguidelines-avoid-reference-coroutine-parameters](https://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines/avoid-reference-coroutine-parameters.html).

Test Plan: check-clang-tools
2025-06-17 09:47:15 +01:00
Thorsten Klein
00eb22fff9
added option google-readability-namespace-comments.AllowNoNamespaceComments (#124265)
New option AllowNoNamespaceComments for
`google-readability-namespace-comments.AllowNoNamespaceComments` is
added.

When true, the check will allow that no namespace comment is present. If
a namespace comment is added but it is not matching, the check will
fail. Default is `false`

Fixes #124264
2025-06-08 13:19:40 +03:00
Carlos Galvez
30d8aebbe2
[clang-tidy] Add option to disable bugprone-multi-level-pointer-conversion in C code (#141209)
Sometimes a project may want to enable this check only in C++, and
disable it in C, since the patterns the check warns about are quite
common and idiomatic in C, and there are no better alternatives.
    
Fixes #140659

Co-authored-by: Carlos Gálvez <carlos.galvez@zenseact.com>
2025-06-07 21:52:23 +02:00
Baranov Victor
0f03f8ab2e
[clang-tidy] Add support for lambda-expression in use-trailing-return-type check (#135383)
Add support for lambda-expression in `use-trailing-return-type` check.

Added two new options:
1. `TransformFunctions` will trigger function declarations to use
trailing return type.
2. `TransformLambdas` will trigger lambda expression to use trailing
return type if it was not stated explicitly.

Fixed false positives when lambda was matched as a function in C++11
mode.

Closes https://github.com/llvm/llvm-project/issues/95711
2025-06-06 10:46:41 +03:00
Aaron Puchert
8641a2cf99
Fix cross-references in Clang attribute reference (#142967)
Anchors are automatically generated, but adding another anchor with the
same name hides the anchor that we actually want. Simply removing the
unnecessary self-referential anchor `lifetimebound` fixes the links.

The documentation for coro_disable_lifetimebound and coro_lifetimebound
also had an unnecessary redirection. It's possible to change the link
text to differ from the heading, but I think that's not necessary here.

Also fix a reference in clang-tidy that was using the (unstable)
numbered anchor.
2025-06-05 18:17:49 +02:00
MarcoFalke
fa36822bfb
NFC: fix typo in tidy modernize-deprecated-headers docs 2025-06-03 12:25:01 +02:00
Tommy Chen
4cb25e2d37
[clang-tidy] Add avoid-pragma-once. (#140388)
#139618
2025-05-27 05:57:51 +08:00
Baranov Victor
ac0a880f4e
[clang-tidy] added AllowedTypes option to readability-qualified-auto check (#136571)
Added `AllowedTypes` option to `readability-qualified-auto` check

Fixes https://github.com/llvm/llvm-project/issues/63461.
2025-05-26 13:25:35 +03:00
Baranov Victor
eda3e96b40
[clang-tidy] Fix false positives in bugprone-crtp-constructor-accessibility check (#132543)
Fix false positives in `bugprone-crtp-constructor-accessibility` check
on deleted constructors that cannot be used to construct objects, even
if they have public or protected access.

Closes https://github.com/llvm/llvm-project/issues/131737.
2025-05-26 06:25:19 +03:00
Daan De Meyer
c46a394df9
[clang-tidy] Add UnusedIncludes/MissingIncludes options to misc-include-cleaner (#140600)
These mimick the same options from clangd and allow using the check to
only check for unused includes or missing includes.
2025-05-21 16:41:04 +02:00
Balázs Kéri
d84b97ebb3
[clang-tidy] Add check bugprone-misleading-setter-of-reference (#132242) 2025-05-17 10:26:13 +02:00
Bart Louwers
89273e9d17
[NFC][clang-tidy][docs] Fix spelling error for bugprone-crtp-constructor-accessibility (#139307) 2025-05-10 17:07:37 -04:00
Baranov Victor
2f16cbc700
[clang-tidy][NFC][doc] fix typos in docs. (#138305)
Fixed typos in docs of `clang-tidy` checks.
2025-05-02 20:41:43 +02:00
David Rivera
45411ac895
[clang-tidy] Avoid diagnosing std::array initializations for modernize-use-designated-initializers (#134774)
**Edit:**
I suggest we avoid diagnosing initializers for `std::array` type. The
fixit provided is incorrect as observed in **#133715.** The only
workaround would require C99-style array designators which don’t really
align with the purpose of this check. This would also generate extra
compiler warnings.

Fixes #133715
2025-04-28 20:12:38 +02:00
Congcong Cai
06814834a6
[clang-tidy] treat unsigned char and signed char as char type by default in bugprone-unintended-char-ostream-output (#134870)
Add `AllowedTypes` options to support custom defined char like type.
treat `unsigned char` and `signed char` as char like type by default.
The allowed types only effect when the var decl or explicit cast to this
non-canonical type names.

Fixed: #133425
2025-04-13 12:09:50 +08:00
Baranov Victor
da6e2454ff
[clang-tidy] Improve bugprone-capturing-this-in-member-variable check: add support of bind functions. (#132635)
Improve `bugprone-capturing-this-in-member-variable` check:
Added support of `bind`-like functions that capture and store `this`
pointer in class member.

Closes https://github.com/llvm/llvm-project/issues/131220.
2025-04-07 09:57:55 +08:00
Baranov Victor
547d054ef1
[clang-tidy][NFC][doc] improve "options" sections of misc-, cppcore- and other checks (#133694)
Improved "options" sections of various checks:

1. Added Options keyword to be a delimiter between "body" and "options"
parts of docs
2. Added default values where were absent.
3. Changed double-tick to single-tick in default values.

---------

Co-authored-by: EugeneZelenko <eugene.zelenko@gmail.com>
2025-04-04 14:21:48 +02:00
Baranov Victor
52639d69ac
[clang-tidy][NFC][doc] improve "options" sections of bugprone- and modernize- checks (#133525)
Improved "options" sections of `bugprone-` and `modernize-` checks:

1. Added `Options` keyword to be a delimiter between "body" and
"options" parts of docs
2. Added default values where was absent.
3. Improved readability of some default values by converting `1` to
`true`.
2025-03-30 20:26:23 +02:00
Baranov Victor
ecdbd26ba4
[clang-tidy][NFC] improve documentation for bugprone-argument-comment check (#133436)
Improve docs for `bugprone-argument-comment` check by writing explicitly
default values for options.
Before this change, it was unclear what values are default.
2025-03-28 14:33:53 +01:00
Baranov Victor
2909c420f6
[clang-tidy] modernize-use-starts-ends-with: fix false positives on find and rfind (#129564)
Also document cases with two or three arguments (matching default arguments)
this check matches.

Closes https://github.com/llvm/llvm-project/issues/129498.
2025-03-22 08:07:58 -04:00
Congcong Cai
3b1e18c2db
[clang-tidy] Add new check bugprone-capture-this-by-field (#130297)
Finds lambda captures that capture the ``this`` pointer and store it as
class
members without handle the copy and move constructors and the
assignments.

Capture this in a lambda and store it as a class member is dangerous
because the
lambda can outlive the object it captures. Especially when the object is
copied
or moved, the captured ``this`` pointer will be implicitly propagated to
the
new object. Most of the time, people will believe that the captured
``this``
pointer points to the new object, which will lead to bugs.

Fixes: #120863

---------

Co-authored-by: Baranov Victor <70346889+vbvictor@users.noreply.github.com>
Co-authored-by: Baranov Victor <bar.victor.2002@gmail.com>
2025-03-17 15:11:43 +08:00
Congcong Cai
1a68269e28
[clang-tidy] support pointee mutation check in misc-const-correctness (#130494)
Co-authored-by: Baranov Victor <bar.victor.2002@gmail.com>
2025-03-14 21:18:43 +08:00