diff --git a/clang-tools-extra/clangd/Diagnostics.cpp b/clang-tools-extra/clangd/Diagnostics.cpp --- a/clang-tools-extra/clangd/Diagnostics.cpp +++ b/clang-tools-extra/clangd/Diagnostics.cpp @@ -631,7 +631,10 @@ /// the result is not too large and does not contain newlines. static void writeCodeToFixMessage(llvm::raw_ostream &OS, llvm::StringRef Code) { constexpr unsigned MaxLen = 50; - + if (Code == "\n") { + OS << "\\n"; + return; + } // Only show the first line if there are many. llvm::StringRef R = Code.split('\n').first; // Shorten the message if it's too long. diff --git a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp --- a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp +++ b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp @@ -623,6 +623,15 @@ Fix(Source.range(), "ident", "change 'ide\\…' to 'ident'")))); } +TEST(DiagnosticTest, NewLineFixMessage) { + Annotations Source("int a;[[]]"); + TestTU TU = TestTU::withCode(Source.code()); + TU.ExtraArgs = {"-Wnewline-eof"}; + EXPECT_THAT( + *TU.build().getDiagnostics(), + ElementsAre(WithFix((Fix(Source.range(), "\n", "insert '\\n'"))))); +} + TEST(DiagnosticTest, ClangTidySuppressionCommentTrumpsWarningAsError) { Annotations Main(R"cpp( int main() {