[clang-tools-extra] 42c2f71 - [clang-tidy] Don't insert blank line in IncludeInserter (#221428)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Oct 11 12:24:18 PDT 2026
Author: Baranov Victor
Date: 2026-10-11T22:24:14+03:00
New Revision: 42c2f71288a6a09d9e9b50f5dc014d43bd428856
URL: https://github.com/llvm/llvm-project/commit/42c2f71288a6a09d9e9b50f5dc014d43bd428856
DIFF: https://github.com/llvm/llvm-project/commit/42c2f71288a6a09d9e9b50f5dc014d43bd428856.diff
LOG: [clang-tidy] Don't insert blank line in IncludeInserter (#221428)
Fixes https://github.com/llvm/llvm-project/issues/52859.
Added:
Modified:
clang-tools-extra/clang-tidy/utils/IncludeSorter.cpp
clang-tools-extra/docs/ReleaseNotes.md
clang-tools-extra/unittests/clang-tidy/IncludeInserterTest.cpp
clang-tools-extra/unittests/clang-tidy/TransformerClangTidyCheckTest.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/utils/IncludeSorter.cpp b/clang-tools-extra/clang-tidy/utils/IncludeSorter.cpp
index b7c313693781d..826c3d2676e88 100644
--- a/clang-tools-extra/clang-tidy/utils/IncludeSorter.cpp
+++ b/clang-tools-extra/clang-tidy/utils/IncludeSorter.cpp
@@ -199,19 +199,18 @@ IncludeSorter::createIncludeInsertion(StringRef FileName, bool IsAngled) {
return std::nullopt;
if (NonEmptyKind < IncludeKind) {
- // Create a block after.
+ // Insert directly after the nearest preceding group.
+ // This keeps clang-format able to fix up the ordering within group.
const std::string &LastInclude = IncludeBucket[NonEmptyKind].back();
const SourceRange LastIncludeLocation =
IncludeLocations[LastInclude].back();
- IncludeStmt.insert(0, LineEnding);
return FixItHint::CreateInsertion(LastIncludeLocation.getEnd(),
IncludeStmt);
}
- // Create a block before.
+ // Insert directly before the nearest group for the same reason as above.
const std::string &FirstInclude = IncludeBucket[NonEmptyKind][0];
const SourceRange FirstIncludeLocation =
IncludeLocations[FirstInclude].back();
- IncludeStmt.append(LineEnding);
return FixItHint::CreateInsertion(FirstIncludeLocation.getBegin(),
IncludeStmt);
}
diff --git a/clang-tools-extra/docs/ReleaseNotes.md b/clang-tools-extra/docs/ReleaseNotes.md
index 25bc3ba0ed73f..a6d8c764728b4 100644
--- a/clang-tools-extra/docs/ReleaseNotes.md
+++ b/clang-tools-extra/docs/ReleaseNotes.md
@@ -416,6 +416,10 @@ infrastructure are described first, followed by tool-specific sections.
#### Miscellaneous
+- Fixed {program}`clang-tidy` adding an unwanted blank line when automatically
+ inserting a missing `#include`, which stopped {program}`clang-format` and
+ the `llvm-include-order` check from being able to sort it correctly afterwards.
+
### Improvements to include-fixer
### Improvements to clang-include-fixer
diff --git a/clang-tools-extra/unittests/clang-tidy/IncludeInserterTest.cpp b/clang-tools-extra/unittests/clang-tidy/IncludeInserterTest.cpp
index d01ba9814b8a1..b01bcc8e69d63 100644
--- a/clang-tools-extra/unittests/clang-tidy/IncludeInserterTest.cpp
+++ b/clang-tools-extra/unittests/clang-tidy/IncludeInserterTest.cpp
@@ -392,7 +392,6 @@ void foo() {
#include <list>
#include <map>
-
#include "path/to/header.h"
void foo() {
@@ -413,7 +412,6 @@ void foo() {
})";
const char *PostCode = R"(
#include "clang_tidy/tests/insert_includes_test_header.h"
-
#include "path/to/header.h"
void foo() {
@@ -524,7 +522,6 @@ void foo() {
})";
const char *PostCode = R"(
#include "clang_tidy/tests/insert_includes_test_header.h"
-
#include <set>
#include "path/to/a/header.h"
@@ -553,7 +550,6 @@ void foo() {
#include "clang_tidy/tests/insert_includes_test_header.h"
#include <stdlib.h>
-
#include <set>
#include "path/to/a/header.h"
@@ -576,7 +572,6 @@ void foo() {
})";
const char *PostCode = R"(
#include <set>
-
#include "path/to/a/header.h"
void foo() {
@@ -600,7 +595,6 @@ void foo() {
})";
const char *PostCode = R"(
#include <stdlib.h>
-
#include <set>
#include "path/to/a/header.h"
@@ -718,7 +712,6 @@ void foo() {
})";
const char *PostCode = R"(
#include "clang_tidy/tests/insert_includes_test_header.h"
-
#include <c.h>
#include <d>
@@ -747,7 +740,6 @@ void foo() {
})";
const char *PostCode = R"(
#import "clang_tidy/tests/insert_includes_test_header.h"
-
#import "a/header.h"
void foo() {
@@ -798,7 +790,6 @@ void foo() {
#include <map>
#include "path/to/a/header.h"
-
#import "clang_tidy/tests/generated_file.proto.h"
void foo() {
diff --git a/clang-tools-extra/unittests/clang-tidy/TransformerClangTidyCheckTest.cpp b/clang-tools-extra/unittests/clang-tidy/TransformerClangTidyCheckTest.cpp
index fc43f2579e8e2..612229a4f1eb1 100644
--- a/clang-tools-extra/unittests/clang-tidy/TransformerClangTidyCheckTest.cpp
+++ b/clang-tools-extra/unittests/clang-tidy/TransformerClangTidyCheckTest.cpp
@@ -335,7 +335,6 @@ TEST(TransformerClangTidyCheckTest, AddIncludeObeysSortStyleLocalOption) {
int h(int x) { return 3; })cc";
std::string TreatsAsLibraryHeader = R"cc(#include "input.h"
-
#include "bar.h"
int h(int x) { return 5; })cc";
@@ -367,7 +366,6 @@ TEST(TransformerClangTidyCheckTest, AddIncludeObeysSortStyleGlobalOption) {
int h(int x) { return 3; })cc";
std::string TreatsAsLibraryHeader = R"cc(#include "input.h"
-
#include "bar.h"
int h(int x) { return 5; })cc";
More information about the cfe-commits
mailing list