[clang-tools-extra] [clang-tidy] Don't insert blank line in IncludeInserter (PR #221428)
Baranov Victor via cfe-commits
cfe-commits at lists.llvm.org
Mon Sep 7 05:50:51 PDT 2026
https://github.com/vbvictor updated https://github.com/llvm/llvm-project/pull/221428
>From 4ed652fe77dae0bd5bd644bb400105a44a1f1e1c Mon Sep 17 00:00:00 2001
From: Victor Baranov <bar.victor.2002 at gmail.com>
Date: Sat, 5 Sep 2026 12:53:06 +0300
Subject: [PATCH] [clang-tidy] Don't insert blank line in IncludeInserter
---
clang-tools-extra/clang-tidy/utils/IncludeSorter.cpp | 7 +++----
clang-tools-extra/docs/ReleaseNotes.md | 4 ++++
.../unittests/clang-tidy/IncludeInserterTest.cpp | 9 ---------
.../clang-tidy/TransformerClangTidyCheckTest.cpp | 2 --
4 files changed, 7 insertions(+), 15 deletions(-)
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 b32b8a51e0606..c0ff5b0cc096b 100644
--- a/clang-tools-extra/docs/ReleaseNotes.md
+++ b/clang-tools-extra/docs/ReleaseNotes.md
@@ -261,6 +261,10 @@ infrastructure are described first, followed by tool-specific sections.
#### Miscellaneous
+- Fixed clang-tidy adding an unwanted blank line when automatically inserting
+ a missing `#include`, which stopped `clang-format` and `llvm-include-order`
+ from being able to sort it correctly afterward.
+
### 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