Skip to content

[clang-tidy][NFC] add '.clang-tidy' config for clang-tidy project #147793

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

vbvictor
Copy link
Contributor

@vbvictor vbvictor commented Jul 9, 2025

Added .clang-tidy config as discussed in RFC.
Added bugprone, readability, modernize, performance checks that didn't create many warnings.
Fixed minor warnings to make /clang-tidy directory complaint with clang-tidy-20.

Disabled checks will be enabled in future PRs after fixing their warnings.

@vbvictor vbvictor changed the title [clang-tidy] add .clang-tidy config for clang-tidy project [clang-tidy][NFC] add '.clang-tidy' config for clang-tidy project Jul 9, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 9, 2025

@llvm/pr-subscribers-clang-tidy

Author: Baranov Victor (vbvictor)

Changes

Added .clang-tidy config as discussed in RFC.
Added bugprone checks that didn't create many warnings.
Fixed minor warnings to make /clang-tidy directory complaint with clang-tidy-20.

Disabled checks will be enabled in future PRs after fixing their warnings.


Full diff: https://github.com/llvm/llvm-project/pull/147793.diff

3 Files Affected:

  • (added) clang-tools-extra/clang-tidy/.clang-tidy (+11)
  • (modified) clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp (+2-5)
  • (modified) clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp (+5-5)
diff --git a/clang-tools-extra/clang-tidy/.clang-tidy b/clang-tools-extra/clang-tidy/.clang-tidy
new file mode 100644
index 0000000000000..45d1929b6b742
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/.clang-tidy
@@ -0,0 +1,11 @@
+InheritParentConfig: true
+Checks: [
+  bugprone-*
+  -bugprone-assignment-in-if-condition,
+  -bugprone-branch-clone,
+  -bugprone-easily-swappable-parameters,
+  -bugprone-narrowing-conversions,
+  -bugprone-suspicious-stringview-data-usage,
+  -bugprone-unchecked-optional-access,
+  -bugprone-unused-return-value,
+]
diff --git a/clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp
index 28e8fe002d575..6565fa3f7c85b 100644
--- a/clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp
@@ -129,13 +129,10 @@ void CrtpConstructorAccessibilityCheck::check(
         << HintFriend;
   }
 
-  auto WithFriendHintIfNeeded =
-      [&](const DiagnosticBuilder &Diag,
-          bool NeedsFriend) -> const DiagnosticBuilder & {
+  auto WithFriendHintIfNeeded = [&](const DiagnosticBuilder &Diag,
+                                    bool NeedsFriend) {
     if (NeedsFriend)
       Diag << HintFriend;
-
-    return Diag;
   };
 
   if (!CRTPDeclaration->hasUserDeclaredConstructor()) {
diff --git a/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
index 88d2f2c388d07..88e048e65d4e8 100644
--- a/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
@@ -370,16 +370,16 @@ void SizeofExpressionCheck::check(const MatchFinder::MatchResult &Result) {
         << E->getSourceRange();
   } else if (Result.Nodes.getNodeAs<Stmt>("loop-expr")) {
     auto *SizeofArgTy = Result.Nodes.getNodeAs<Type>("sizeof-arg-type");
-    if (const auto member = dyn_cast<MemberPointerType>(SizeofArgTy))
-      SizeofArgTy = member->getPointeeType().getTypePtr();
+    if (const auto *Member = dyn_cast<MemberPointerType>(SizeofArgTy))
+      SizeofArgTy = Member->getPointeeType().getTypePtr();
 
     const auto *SzOfExpr = Result.Nodes.getNodeAs<Expr>("sizeof-expr");
 
-    if (const auto type = dyn_cast<ArrayType>(SizeofArgTy)) {
+    if (const auto *Type = dyn_cast<ArrayType>(SizeofArgTy)) {
       // check if the array element size is larger than one. If true,
       // the size of the array is higher than the number of elements
-      CharUnits sSize = Ctx.getTypeSizeInChars(type->getElementType());
-      if (!sSize.isOne()) {
+      CharUnits SSize = Ctx.getTypeSizeInChars(Type->getElementType());
+      if (!SSize.isOne()) {
         diag(SzOfExpr->getBeginLoc(),
              "suspicious usage of 'sizeof' in the loop")
             << SzOfExpr->getSourceRange();

@llvmbot
Copy link
Member

llvmbot commented Jul 9, 2025

@llvm/pr-subscribers-clang-tools-extra

Author: Baranov Victor (vbvictor)

Changes

Added .clang-tidy config as discussed in RFC.
Added bugprone checks that didn't create many warnings.
Fixed minor warnings to make /clang-tidy directory complaint with clang-tidy-20.

Disabled checks will be enabled in future PRs after fixing their warnings.


Full diff: https://github.com/llvm/llvm-project/pull/147793.diff

3 Files Affected:

  • (added) clang-tools-extra/clang-tidy/.clang-tidy (+11)
  • (modified) clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp (+2-5)
  • (modified) clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp (+5-5)
diff --git a/clang-tools-extra/clang-tidy/.clang-tidy b/clang-tools-extra/clang-tidy/.clang-tidy
new file mode 100644
index 0000000000000..45d1929b6b742
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/.clang-tidy
@@ -0,0 +1,11 @@
+InheritParentConfig: true
+Checks: [
+  bugprone-*
+  -bugprone-assignment-in-if-condition,
+  -bugprone-branch-clone,
+  -bugprone-easily-swappable-parameters,
+  -bugprone-narrowing-conversions,
+  -bugprone-suspicious-stringview-data-usage,
+  -bugprone-unchecked-optional-access,
+  -bugprone-unused-return-value,
+]
diff --git a/clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp
index 28e8fe002d575..6565fa3f7c85b 100644
--- a/clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp
@@ -129,13 +129,10 @@ void CrtpConstructorAccessibilityCheck::check(
         << HintFriend;
   }
 
-  auto WithFriendHintIfNeeded =
-      [&](const DiagnosticBuilder &Diag,
-          bool NeedsFriend) -> const DiagnosticBuilder & {
+  auto WithFriendHintIfNeeded = [&](const DiagnosticBuilder &Diag,
+                                    bool NeedsFriend) {
     if (NeedsFriend)
       Diag << HintFriend;
-
-    return Diag;
   };
 
   if (!CRTPDeclaration->hasUserDeclaredConstructor()) {
diff --git a/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
index 88d2f2c388d07..88e048e65d4e8 100644
--- a/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
@@ -370,16 +370,16 @@ void SizeofExpressionCheck::check(const MatchFinder::MatchResult &Result) {
         << E->getSourceRange();
   } else if (Result.Nodes.getNodeAs<Stmt>("loop-expr")) {
     auto *SizeofArgTy = Result.Nodes.getNodeAs<Type>("sizeof-arg-type");
-    if (const auto member = dyn_cast<MemberPointerType>(SizeofArgTy))
-      SizeofArgTy = member->getPointeeType().getTypePtr();
+    if (const auto *Member = dyn_cast<MemberPointerType>(SizeofArgTy))
+      SizeofArgTy = Member->getPointeeType().getTypePtr();
 
     const auto *SzOfExpr = Result.Nodes.getNodeAs<Expr>("sizeof-expr");
 
-    if (const auto type = dyn_cast<ArrayType>(SizeofArgTy)) {
+    if (const auto *Type = dyn_cast<ArrayType>(SizeofArgTy)) {
       // check if the array element size is larger than one. If true,
       // the size of the array is higher than the number of elements
-      CharUnits sSize = Ctx.getTypeSizeInChars(type->getElementType());
-      if (!sSize.isOne()) {
+      CharUnits SSize = Ctx.getTypeSizeInChars(Type->getElementType());
+      if (!SSize.isOne()) {
         diag(SzOfExpr->getBeginLoc(),
              "suspicious usage of 'sizeof' in the loop")
             << SzOfExpr->getSourceRange();

@vbvictor
Copy link
Contributor Author

vbvictor commented Jul 9, 2025

Added all people that participated in RFC and regular clang-tidy reviewers to see this change.

@EugeneZelenko
Copy link
Contributor

EugeneZelenko commented Jul 9, 2025

How about adding modernize relevant to currently used C++ standard? Also some of readability checks are useful. Please also consider top-level .clang-tidy file re-formatting.

@vbvictor
Copy link
Contributor Author

vbvictor commented Jul 9, 2025

How about adding modernize relevant to currently used C++ standard? Also some of readability checks are useful.

Yes, I will add more and more checks to config. Just started with bugprone as it is the most straightforward improvement. Or do you think we can make it in one go?

Please also consider top-level .clang-tidy file re-formatting.

Noted, will check that.

@EugeneZelenko
Copy link
Contributor

How about adding modernize relevant to currently used C++ standard? Also some of readability checks are useful.

Yes, I will add more and more checks to config. Just started with bugprone as it is the most straightforward improvement. Or do you think we can make it in one go?

Yes, one go is straightforward. However, if preliminary code fixes will be necessary, they should go into separate pull request.

@vbvictor vbvictor force-pushed the add-clang-tidy-custom-config branch from 86802f2 to a0fd1b0 Compare July 9, 2025 18:01
@vbvictor
Copy link
Contributor Author

vbvictor commented Jul 9, 2025

Results of running with config (disabled checks that doesn't align with our style)

InheritParentConfig: true
Checks: [
  bugprone-*,
  modernize-*,
  -modernize-avoid-c-arrays,
  -modernize-use-trailing-return-type,
  readability-*,
  -readability-braces-around-statements,
  -readability-identifier-length,
  -readability-magic-numbers,
  -readability-implicit-bool-conversion,
  performance-*
]

report.html.txt (delete .txt to view)
report.txt
I will add checks with a couple of warnings and disable others for future PR's.

@vbvictor vbvictor force-pushed the add-clang-tidy-custom-config branch from 7dbf98f to 4b061f9 Compare July 9, 2025 20:41
Copy link
Contributor

@EugeneZelenko EugeneZelenko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But please wait for other reviewers.

@EugeneZelenko
Copy link
Contributor

Will be good idea to similar Clang-Tidy configuration over other parts of LLVM code base. May be some check warnings will be easy to fix and enforce. I did such cleanups in past (by quite remote :-().

Copy link
Member

@PiotrZSL PiotrZSL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants