[clang-tidy] Don't insert blank line in IncludeInserter (#221428) Fixes https://github.com/llvm/llvm-project/issues/52859. GitOrigin-RevId: 42c2f71288a6a09d9e9b50f5dc014d43bd428856
diff --git a/clang-tidy/utils/IncludeSorter.cpp b/clang-tidy/utils/IncludeSorter.cpp index b7c3136..826c3d2 100644 --- a/clang-tidy/utils/IncludeSorter.cpp +++ b/clang-tidy/utils/IncludeSorter.cpp
@@ -199,19 +199,18 @@ 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/docs/ReleaseNotes.md b/docs/ReleaseNotes.md index 25bc3ba..a6d8c76 100644 --- a/docs/ReleaseNotes.md +++ b/docs/ReleaseNotes.md
@@ -416,6 +416,10 @@ #### 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/unittests/clang-tidy/IncludeInserterTest.cpp b/unittests/clang-tidy/IncludeInserterTest.cpp index d01ba98..b01bcc8 100644 --- a/unittests/clang-tidy/IncludeInserterTest.cpp +++ b/unittests/clang-tidy/IncludeInserterTest.cpp
@@ -392,7 +392,6 @@ #include <list> #include <map> - #include "path/to/header.h" void foo() { @@ -413,7 +412,6 @@ })"; const char *PostCode = R"( #include "clang_tidy/tests/insert_includes_test_header.h" - #include "path/to/header.h" void foo() { @@ -524,7 +522,6 @@ })"; 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 @@ #include "clang_tidy/tests/insert_includes_test_header.h" #include <stdlib.h> - #include <set> #include "path/to/a/header.h" @@ -576,7 +572,6 @@ })"; const char *PostCode = R"( #include <set> - #include "path/to/a/header.h" void foo() { @@ -600,7 +595,6 @@ })"; const char *PostCode = R"( #include <stdlib.h> - #include <set> #include "path/to/a/header.h" @@ -718,7 +712,6 @@ })"; const char *PostCode = R"( #include "clang_tidy/tests/insert_includes_test_header.h" - #include <c.h> #include <d> @@ -747,7 +740,6 @@ })"; const char *PostCode = R"( #import "clang_tidy/tests/insert_includes_test_header.h" - #import "a/header.h" void foo() { @@ -798,7 +790,6 @@ #include <map> #include "path/to/a/header.h" - #import "clang_tidy/tests/generated_file.proto.h" void foo() {
diff --git a/unittests/clang-tidy/TransformerClangTidyCheckTest.cpp b/unittests/clang-tidy/TransformerClangTidyCheckTest.cpp index fc43f25..612229a 100644 --- a/unittests/clang-tidy/TransformerClangTidyCheckTest.cpp +++ b/unittests/clang-tidy/TransformerClangTidyCheckTest.cpp
@@ -335,7 +335,6 @@ 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 @@ 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";