[clang-tools-extra] [clang-tidy] add default error message for performance-avoid-endl (PR #107867)

Julian Schmidt via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 12 15:03:00 PDT 2024


================
@@ -46,14 +46,15 @@ void AvoidEndlCheck::check(const MatchFinder::MatchResult &Result) {
     // Handle the more common streaming '... << std::endl' case
     const CharSourceRange TokenRange =
         CharSourceRange::getTokenRange(Expression->getSourceRange());
-    const StringRef SourceText = Lexer::getSourceText(
+    StringRef SourceText = Lexer::getSourceText(
         TokenRange, *Result.SourceManager, Result.Context->getLangOpts());
-
+    if (SourceText.empty())
+      SourceText = "std::endl";
     auto Diag = diag(Expression->getBeginLoc(),
                      "do not use '%0' with streams; use '\\n' instead")
                 << SourceText;
-
-    Diag << FixItHint::CreateReplacement(TokenRange, "'\\n'");
+    if (TokenRange.isValid())
+      Diag << FixItHint::CreateReplacement(TokenRange, "'\\n'");
----------------
5chmidti wrote:

The same issue should also apply to the other diagnostic as well, no?
E.g., both `SourceText` and `ArgSourceText` would probably need checking as well, and the `FixItHint` should also be conditional.

https://github.com/llvm/llvm-project/pull/107867


More information about the cfe-commits mailing list