Index: clang-tidy/modernize/RawStringLiteralCheck.h =================================================================== --- clang-tidy/modernize/RawStringLiteralCheck.h +++ clang-tidy/modernize/RawStringLiteralCheck.h @@ -32,7 +32,7 @@ private: void replaceWithRawStringLiteral( const ast_matchers::MatchFinder::MatchResult &Result, - const StringLiteral *Literal); + const StringLiteral *Literal, StringRef Replacement); std::string DelimiterStem; }; Index: clang-tidy/modernize/RawStringLiteralCheck.cpp =================================================================== --- clang-tidy/modernize/RawStringLiteralCheck.cpp +++ clang-tidy/modernize/RawStringLiteralCheck.cpp @@ -121,19 +121,24 @@ if (Literal->getLocStart().isMacroID()) return; - if (containsEscapedCharacters(Result, Literal)) - replaceWithRawStringLiteral(Result, Literal); + if (containsEscapedCharacters(Result, Literal)) { + std::string Replacement = asRawStringLiteral(Literal, DelimiterStem); + if (Replacement.length() <= + Lexer::MeasureTokenLength(Literal->getLocStart(), *Result.SourceManager, + getLangOpts())) + replaceWithRawStringLiteral(Result, Literal, Replacement); + } } void RawStringLiteralCheck::replaceWithRawStringLiteral( - const MatchFinder::MatchResult &Result, const StringLiteral *Literal) { + const MatchFinder::MatchResult &Result, const StringLiteral *Literal, + StringRef Replacement) { CharSourceRange CharRange = Lexer::makeFileCharRange( CharSourceRange::getTokenRange(Literal->getSourceRange()), *Result.SourceManager, getLangOpts()); diag(Literal->getLocStart(), "escaped string literal can be written as a raw string literal") - << FixItHint::CreateReplacement( - CharRange, asRawStringLiteral(Literal, DelimiterStem)); + << FixItHint::CreateReplacement(CharRange, Replacement); } } // namespace modernize Index: test/clang-tidy/modernize-raw-string-literal.cpp =================================================================== --- test/clang-tidy/modernize-raw-string-literal.cpp +++ test/clang-tidy/modernize-raw-string-literal.cpp @@ -55,6 +55,10 @@ wchar_t const *const WideLiteral(L"foobie\\bletch"); wchar_t const *const WideRawLiteral(LR"(foobie\\bletch)"); +// Don't replace these, because the raw literal would be longer. +char const *const JustAQuote("quote:\'"); +char const *const NeedDelimiter("\":)\""); + char const *const SingleQuote("goink\'frob"); // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: {{.*}} can be written as a raw string literal // CHECK-XFIXES: {{^}}char const *const SingleQuote(R"(goink'frob)");{{$}}