95 Commits

Author SHA1 Message Date
Balázs Kéri
6f2cf6b0ac
[clang-tidy] Add check 'bugprone-invalid-enum-default-initialization' (#136823) 2025-07-31 09:00:34 +02:00
Baranov Victor
94877ce1b4
[clang-tidy][NFC] fix 'misc-use-internal-linkage' check warnings (#143482)
Run misc-use-internal-linkage check over clang-tidy code. 
Also fixed a couple of other clang-tidy warnings.

Apart from issues in header files, all '.cpp' in
`clang-tools-extra/clang-tidy` must be clang-tidy clear now.
2025-06-10 23:23:37 +03:00
Balázs Kéri
d84b97ebb3
[clang-tidy] Add check bugprone-misleading-setter-of-reference (#132242) 2025-05-17 10:26:13 +02: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
56762b7ace
[clang-tidy] Add new check bugprone-unintended-char-ostream-output (#127720)
It wants to find unintended character output from `uint8_t` and `int8_t`
to an ostream.
e.g.
```c++
uint8_t v = 9;
std::cout << v;
```

---------

Co-authored-by: whisperity <whisperity@gmail.com>
2025-02-27 19:36:24 +08:00
MichelleCDjunaidi
8ebc35f8d0
[clang-tidy] Create bugprone-incorrect-enable-shared-from-this check (#102299)
This checks that classes/structs inheriting from
``std::enable_shared_from_this`` does so with public inheritance, so it
prevents crashes due to ``std::make_shared`` and ``shared_from_this()``
getting called when the internal weak pointer was not initialized (e.g.
due to private inheritance).
2025-01-12 11:04:40 +01:00
Congcong Cai
e45e091b90
[clang-tidy] swap cppcoreguidelines-narrowing-conversions and bugprone-narrowing-conversions (#120245)
According to #116591.
> Coding guidelines should "cherry-pick" (and posddsibly
configure/harden/make more strict) base checks.
We should move narrowing conversion to bugprone and keep alias in
cppcoreguidelines
2024-12-29 19:22:25 +08:00
vabridgers
3d6923dbac
RFC: [clang-tidy] [analyzer] Move nondeterministic pointer usage check to tidy (#110471)
This change moves the `alpha.nondeterministic.PointerSorting` and
`alpha.nondeterministic.PointerIteration` static analyzer checkers to a
single `clang-tidy` check. Those checkers were implemented as simple
`clang-tidy` check-like code, wrapped in the static analyzer framework.
The documentation was updated to describe what the checks can and cannot
do, and testing was completed on a broad set of open-source projects.

Co-authored-by: Vince Bridgers <vince.a.bridgers@ericsson.com>
2024-10-28 03:53:36 -05:00
Carlos Galvez
fb0ef6b66e
[clang-tidy] Create bugprone-bitwise-pointer-cast check (#108083)
To detect unsafe usages of casting a pointer to another via copying
the bytes from one into the other, either via std::bit_cast or via
memcpy. This is currently not caught by any other means.

Fixes #106987

---------

Co-authored-by: Carlos Gálvez <carlos.galvez@zenseact.com>
2024-10-06 12:21:09 +02:00
tigbr
7b8f7beadc
[clang-tidy] Add new check bugprone-tagged-union-member-count (#89925)
This patch introduces a new check to find mismatches between the number
of data members in a union and the number enum values present in
variant-like structures.

Variant-like types can look something like this:

```c++
struct variant {
    enum {
        tag1,
        tag2,
    } kind;
    union {
        int i;
        char c;
    } data;
};
```

The kind data member of the variant is supposed to tell which data
member of the union is valid, however if there are fewer enum values
than union members, then it is likely a mistake.

The opposite is not that obvious, because it might be fine to have more
enum values than union data members, but for the time being I am curious
how many real bugs can be caught if we give a warning regardless.

This patch also contains a heuristic where we try to guess whether the
last enum constant is actually supposed to be a tag value for the
variant or whether it is just holding how many enum constants have been
created.

Patch by Gábor Tóthvári!
2024-10-01 13:24:32 +02:00
Discookie
f329e3ed90
[clang-tidy] Add bugprone-pointer-arithmetic-on-polymorphic-object check (#91951)
Finds pointer arithmetic on classes that declare a virtual function.

This check corresponds to the SEI Cert rule [CTR56-CPP: Do not use
pointer arithmetic on polymorphic
objects](https://wiki.sei.cmu.edu/confluence/display/cplusplus/CTR56-CPP.+Do+not+use+pointer+arithmetic+on+polymorphic+objects).

```cpp
struct Base {
  virtual void ~Base();
};

struct Derived : public Base {};

void foo(Base *b) {
  b += 1; // passing `Derived` to `foo()` results in UB
}
```

[Results on open-source
projects](https://codechecker-demo.eastus.cloudapp.azure.com/Default/runs?run=Discookie-ctr56-with-classnames).
Most of the Qtbase reports are from having a `virtual override`
declaration, and the LLVM reports are true positives, as far as I can
tell.
2024-07-04 13:44:31 +00:00
Congcong Cai
d56f08b2ba
[tidy] add new check bugprone-return-const-ref-from-parameter (#89497) 2024-04-24 07:40:22 +08:00
Piotr Zegar
28c1279db3
[clang-tidy] Add bugprone-suspicious-stringview-data-usage check (#83716)
This check identifies suspicious usages of std::string_view::data() that
could lead to reading out-of-bounds data due to inadequate or incorrect
string null termination.

Closes #80854
2024-03-19 20:15:08 +01:00
isuckatcs
8e56fb824a
[clang-tidy] CRTP Constructor Accessibility Check (#82403)
Detects error-prone Curiously Recurring Template Pattern usage, when the CRTP
can be constructed outside itself and the derived class.
2024-03-05 01:09:39 +01:00
Piotr Zegar
06c3c3b67c
[clang-tidy] Add bugprone-chained-comparison check (#76365)
Check that flags chained comparison expressions,
such as a < b < c or a == b == c, which may have
unintended behavior due to implicit operator
associativity.

Moved from Phabricator  (D144429).
2024-01-22 17:11:02 +01:00
Tyler Rockwood
952d344f3e
[clang-tidy] introduce a unused local non trival variable check (#76101)
Introduce a new (off by default) clang tidy check to ensure that
variables of a specific type are always used even if -Wunused-variables
wouldn't generate a warning.

This check has already caught a couple of different bugs on the codebase
I work on, where not handling a future means that lifetimes may not be
kept alive properly as an async chunk of code may run after a class has
been destroyed, etc.

I would like to upstream it because I believe there could be other
applications of this check that would be useful in different contexts.

---------

Signed-off-by: Tyler Rockwood <rockwood@redpanda.com>
2023-12-25 12:19:53 +01:00
Congcong Cai
9a5c6f1760
[clang-tidy]Add new check bugprone-casting-through-void (#69465)
This check detects usage of ``static_cast`` pointer to the other pointer
throght `static_cast` to `void *` in C++ code.
Fixes: #68532
2023-10-20 09:29:06 -05:00
Congcong Cai
72d4d4e3b9
[clang-tidy]add new check bugprone-compare-pointer-to-member-virtual-function (#66055) 2023-09-15 20:59:12 +08:00
Chris Cotter
a7bdaff7ca [clang-tidy] Implement bugprone-incorrect-enable-if
Detects incorrect usages of std::enable_if that don't name the
nested 'type' type.

Reviewed By: PiotrZSL

Differential Revision: https://reviews.llvm.org/D157239
2023-08-21 17:38:30 +00:00
Piotr Zegar
575900d0d9
[clang-tidy] Add bugprone-optional-value-conversion check
Detects potentially unintentional and redundant conversions where a value is
extracted from an optional-like type and then used to create a new instance of
the same optional-like type.

Reviewed By: xgupta

Differential Revision: https://reviews.llvm.org/D147357
2023-07-31 06:22:39 +00:00
Piotr Zegar
f27f22b345
[clang-tidy] Added bugprone-inc-dec-in-conditions check
Detects when a variable is both incremented/decremented and referenced inside a
complex condition and suggests moving them outside to avoid ambiguity in the
variable's value.

Reviewed By: xgupta

Differential Revision: https://reviews.llvm.org/D149015
2023-07-30 13:19:51 +00:00
Piotr Zegar
315946c57d [clang-tidy] Added bugprone-multi-level-implicit-pointer-conversion check
Detects implicit conversions between pointers of different levels of
indirection.

Reviewed By: xgupta

Differential Revision: https://reviews.llvm.org/D149084
2023-07-27 15:49:43 +00:00
Piotr Zegar
047273fc9c [clang-tidy] Add bugprone-empty-catch check
Detects and suggests addressing issues with empty catch statements.

Reviewed By: xgupta

Differential Revision: https://reviews.llvm.org/D144748
2023-07-24 06:34:34 +00:00
Piotr Zegar
490bf27e53 Revert "[clang-tidy] Add bugprone-empty-catch check"
CI failed on "ubuntu-fast" due to disabled exceptions.

This reverts commit f256fee5343033bf8a31aee06a80f3e982b76f82.
2023-07-23 18:13:52 +00:00
Piotr Zegar
f256fee534 [clang-tidy] Add bugprone-empty-catch check
Detects and suggests addressing issues with empty catch statements.

Reviewed By: xgupta

Differential Revision: https://reviews.llvm.org/D144748
2023-07-23 18:02:10 +00:00
Shivam Gupta
42179bbf6b [clang-tidy] Add check for possibly incomplete switch statements
While clang warns about a possibly incomplete switch statement when switching over an enum variable and failing to cover all enum values (either explicitly or with a default case), no such warning is emitted if a plain integer variable is used as switch variable.

Add a clang-tidy check to diagnose these scenarios.

No fixit hint is provided since there are multiple possible solutions.

Differential Revision: https://reviews.llvm.org/D4784
2023-07-17 10:40:11 +05:30
Balázs Kéri
b0bab14b8b [clang-tidy] Add check bugprone-unique-ptr-array-mismatch.
Reviewed By: PiotrZSL

Differential Revision: https://reviews.llvm.org/D151431
2023-05-31 09:55:01 +02:00
Balázs Kéri
852bf52cc9 [clang-tidy] Add check bugprone-multiple-new-in-one-expression.
Reviewed By: donat.nagy
Fixed test failures with previous commit.

Differential Revision: https://reviews.llvm.org/D138777
2023-05-02 12:29:17 +02:00
Balázs Kéri
7b7a6b641a Revert "[clang-tidy] Add check bugprone-multiple-new-in-one-expression."
This reverts commit 1aa36da15369678d94add0f64809b11f95795efd.
2023-05-02 11:23:31 +02:00
Balázs Kéri
1aa36da153 [clang-tidy] Add check bugprone-multiple-new-in-one-expression.
Reviewed By: donat.nagy

Differential Revision: https://reviews.llvm.org/D138777
2023-05-02 10:59:07 +02:00
Piotr Zegar
3bf322e69d [clang-tidy] Add bugprone-non-zero-enum-to-bool-conversion check
Detect implicit and explicit conversions of enum to bool,
when enum doesn't have a enumerator with value equal to 0.
In theory such conversion should always return TRUE.

Reviewed By: carlosgalvezp

Differential Revision: https://reviews.llvm.org/D144036
2023-04-16 08:51:00 +00:00
Gergely Fűtő
f27c8ac83e [clang-tidy] Add the bugprone-unsafe-functions check
Checks for unsafe functions, mostly those listed in the
SEI CERT C Coding Standard Recommendation `MSC24-C` and Rule `MSC33-C`.

For the listed functions, an alternative, more secure replacement is
suggested, if such is available. The checker heavily relies on the
functions from "Annex K" (Bounds-checking interfaces) from C11, but
there are several other recommendations not directly from Annex K.

Differential Revision: http://reviews.llvm.org/D91000

Reviewed-By: aaron.ballman, dkrupp, steakhal, whisperity

Co-Authored-By: Tamás Koller <koller.tamas1996@gmail.com>
Co-Authored-By: Balázs Benics <balazs.benics@sigmatechnology.se>
Co-Authored-By: Whisperity <whisperity@gmail.com>
2023-02-02 14:11:42 +01:00
Carlos Galvez
7d2ea6c422 [clang-tidy][NFC] Use C++17 nested namespaces in the clang-tidy folder
Fix applied by running:

run-clang-tidy.py -checks=-*,modernize-concat-nested-namespaces

Differential Revision: https://reviews.llvm.org/D141770
2023-01-14 18:51:39 +00:00
Abraham Corea Diaz
ec3f8feddf [Clang-Tidy] Empty Check
Adds a clang-tidy check for the incorrect use of `empty()` on a
container when the result of the call is ignored.

Authored-by: Abraham Corea Diaz <abrahamcd@google.com>
Co-authored-by: Denis Nikitin <denik@google.com>

Reviewed By: cjdb

Differential Revision: https://reviews.llvm.org/D128372
2022-12-09 23:19:45 +00:00
Balázs Kéri
6d9eb53329 [clang-tidy] Add checker 'bugprone-suspicious-realloc-usage'.
Add a check to detect usages of `realloc` where the result is assigned
to the same variable (or field) as passed to the first argument.

Reviewed By: steakhal, martong

Differential Revision: https://reviews.llvm.org/D133119
2022-10-04 09:14:46 +02:00
Dmitri Gribenko
05130a6ba7 new clang-tidy checker for assignments within condition clause of if statement
new clang-tidy checker for assignments within the condition clause of an 'if' statement.

Reviewed By: gribozavr2

Differential Revision: https://reviews.llvm.org/D127114
2022-07-05 23:04:12 +02:00
Yitzhak Mandelbaum
7e63a0d479 [clang-tidy] New check for safe usage of std::optional and like types.
This check verifies the safety of access to `std::optional` and related
types (including `absl::optional`). It is based on a corresponding Clang
Dataflow Analysis, which does most of the work. This check merely runs it and
converts its findings into diagnostics.

Differential Revision: https://reviews.llvm.org/D121120
2022-05-06 18:50:36 +00:00
Balázs Kéri
c63522e6ba [clang-tidy] Add new check 'shared-ptr-array-mismatch'.
Reviewed By: LegalizeAdulthood

Differential Revision: https://reviews.llvm.org/D117306
2022-02-07 12:57:58 +01:00
CJ Johnson
6a9487df73 Add new clang-tidy check for string_view(nullptr)
Checks for various ways that the `const CharT*` constructor of `std::basic_string_view` can be passed a null argument and replaces them with the default constructor in most cases. For the comparison operators, braced initializer list does not compile so instead a call to `.empty()` or the empty string literal are used, where appropriate.

This prevents code from invoking behavior which is unconditionally undefined. The single-argument `const CharT*` constructor does not check for the null case before dereferencing its input. The standard is slated to add an explicitly-deleted overload to catch some of these cases: wg21.link/p2166

https://reviews.llvm.org/D114823 is a companion change to prevent duplicate warnings from the `bugprone-string-constructor` check.

Reviewed By: ymandel

Differential Revision: https://reviews.llvm.org/D113148
2021-12-02 13:25:28 +00:00
Gabor Bencze
3373e84539 [clang-tidy] Add bugprone-suspicious-memory-comparison check
The check warns on suspicious calls to `memcmp`.
It currently checks for comparing types that do not have
unique object representations or are non-standard-layout.
Based on
  https://wiki.sei.cmu.edu/confluence/display/c/EXP42-C.+Do+not+compare+padding+data
  https://wiki.sei.cmu.edu/confluence/display/c/FLP37-C.+Do+not+use+object+representations+to+compare+floating-point+values
and part of
  https://wiki.sei.cmu.edu/confluence/display/cplusplus/OOP57-CPP.+Prefer+special+member+functions+and+overloaded+operators+to+C+Standard+Library+functions
Add alias `cert-exp42-c` and `cert-flp37-c`.

Some tests are currently failing at head, the check depends on D89649.
Originally started in D71973

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D89651
2021-08-26 09:23:37 +02:00
Whisperity
499e39c598 [clang-tidy] Add 'bugprone-easily-swappable-parameters' check
Finds function definitions where parameters of convertible types follow
each other directly, making call sites prone to calling the function
with swapped (or badly ordered) arguments.

Such constructs are usually the result of inefficient design and lack of
exploitation of strong type capabilities that are possible in the
language.

This check finds and flags **function definitions** and **not** call
sites!

Reviewed By: aaron.ballman, alexfh

Differential Revision: http://reviews.llvm.org/D69560
2021-06-28 10:49:37 +02:00
Balázs Kéri
530456caf9 [clang-tidy] Add new check 'bugprone-unhandled-exception-at-new'.
Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D97196
2021-04-14 09:33:11 +02:00
Roman Lebedev
46b8ea2fff
[clang-tidy] Add check for implicit widening of multiplication result
Overflows are never fun.
In most cases (in most of the code), they are rare,
because usually you e.g. don't have as many elements.

However, it's exceptionally easy to fall into this pitfail
in code that deals with images, because, assuming 4-channel 32-bit FP data,
you need *just* ~269 megapixel image to case an overflow
when computing at least the total byte count.

In [[ https://github.com/darktable-org/darktable | darktable ]], there is a *long*, painful history of dealing with such bugs:
* https://github.com/darktable-org/darktable/pull/7740
* https://github.com/darktable-org/darktable/pull/7419
* eea1989f2c
* 70626dd95b
* https://github.com/darktable-org/darktable/pull/670
* 38c69fb1b2

and yet they clearly keep resurfacing still.

It would be immensely helpful to have a diagnostic for those patterns,
which is what this change proposes.

Currently, i only diagnose the most obvious case, where multiplication
is directly widened with no other expressions inbetween,
(i.e. `long r = (int)a * (int)b` but not even e.g. `long r = ((int)a * (int)b)`)
however that might be worth relaxing later.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D93822
2021-04-13 21:41:22 +03:00
Balázs Kéri
d1b2a52319 [clang-tidy] Add signal-handler-check for SEI CERT rule SIG30-C
SIG30-C. Call only asynchronous-safe functions within signal handlers

First version of this check, only minimal list of functions is allowed
("strictly conforming" case), for C only.

Differential Revision: https://reviews.llvm.org/D87449
2020-11-04 16:42:30 +01:00
Adam Balogh
14dd073782 [Clang-Tidy] New check bugprone-redundant-branch-condition
Checking the same condition again in a nested `if` usually make no sense,
except if the value of the expression could have been changed between
the two checks. Although compilers may optimize this out, such code is
suspicious: the programmer may have meant to check something else.
Therefore it is worth to find such places in the code and notify the
user about the problem.

This patch implements a basic check for this problem. Currently it
only detects redundant conditions where the condition is a variable of
integral type. It also detects the possible bug if the variable is in an
//or// or //and// logical expression in the inner if and/or the variable
is in an //and// logical expression in the outer if statement. Negated
cases are not handled yet.

Differential Revision: https://reviews.llvm.org/D81272
2020-08-31 16:00:59 +02:00
Ellis Hoag
dfa0db79d0 Warn pointer captured in async block
The block arguments in dispatch_async() and dispatch_after() are
guaranteed to escape. If those blocks capture any pointers with the
noescape attribute then it is an error.
2020-07-07 13:31:14 -04:00
abelkocsis
0f4c70dd3e [clang-tidy] Add spuriously-wake-up-functions check
Summary:
According to
https://wiki.sei.cmu.edu/confluence/display/cplusplus/CON54-CPP.+Wrap+functions+that+can+spuriously+wake+up+in+a+loop
and
https://wiki.sei.cmu.edu/confluence/display/c/CON36-C.+Wrap+functions+that+can+spuriously+wake+up+in+a+loop
misc-spuriously-wake-up-functions check is created. The check finds
`cnd_wait` or `wait` function calls in an `IfStmt` and  warns the user to
replace it with a `WhileStmt` or use it with a lambda parameter.

Reviewers: aaron.ballman, alexfh, hokein, jfb, Charusso

Reviewed By: aaron.ballman

Subscribers: sylvestre.ledru, whisperity, Eugene.Zelenko, mgorny, dexonsmith, cfe-commits, gerazo, xazax.hun, steakhal, Charusso

Tags: #clang-tools-extra, #clang

Differential Revision: https://reviews.llvm.org/D70876
2020-03-21 12:04:03 +01:00
Jonathan Roelofs
2c9cf9f4dd [clang-tidy] New check: bugprone-suspicious-include
Detects and fixes suspicious code like: `#include "foo.cpp"`.

Inspired by: https://twitter.com/lefticus/status/1228458240364687360?s=20

https://reviews.llvm.org/D74669
2020-03-12 09:59:28 -06:00
Nico Weber
714466bf36 Revert "[clang-tidy] New check: bugprone-suspicious-include"
This reverts commit 1e0669bfe05f0f48ee88152c4a1d581f484f8d67
(and follow-ups 698a12712920c214e39bb215fe26fad2e099425b and
52bbdad7d63fd060d102b3591b433d116a982255).
The tests fail fail on Windows, see https://reviews.llvm.org/D74669
2020-03-10 10:28:20 -04:00
Jonathan Roelofs
1e0669bfe0 [clang-tidy] New check: bugprone-suspicious-include
Detects and fixes suspicious code like: `#include "foo.cpp"`.

Inspired by: https://twitter.com/lefticus/status/1228458240364687360?s=20

https://reviews.llvm.org/D74669
2020-03-09 15:54:32 -06:00