diff --git a/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.cpp b/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.cpp --- a/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.cpp +++ b/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.cpp @@ -105,12 +105,9 @@ // Create a preprocessor #include FixIt hint (CreateIncludeInsertion checks // whether this already exists). - auto IncludeHint = IncludeInserter->CreateIncludeInsertion( + Diagnostic << IncludeInserter->CreateIncludeInsertion( Source.getFileID(ComparisonExpr->getBeginLoc()), AbseilStringsMatchHeader, false); - if (IncludeHint) { - Diagnostic << *IncludeHint; - } } void StringFindStartswithCheck::registerPPCallbacks( diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp --- a/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp +++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp @@ -96,10 +96,8 @@ MatchedDecl->getName().size()), InitializationString); if (AddMathInclude) { - auto IncludeHint = IncludeInserter->CreateIncludeInsertion( + Diagnostic << IncludeInserter->CreateIncludeInsertion( Source.getFileID(MatchedDecl->getBeginLoc()), MathHeader, false); - if (IncludeHint) - Diagnostic << *IncludeHint; } } } diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp --- a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp +++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp @@ -86,13 +86,10 @@ SourceRange(BaseRange.getEnd().getLocWithOffset(1), IndexRange.getBegin().getLocWithOffset(-1)), ", ") - << FixItHint::CreateReplacement(Matched->getEndLoc(), ")"); - - Optional Insertion = Inserter->CreateIncludeInsertion( - Result.SourceManager->getMainFileID(), GslHeader, - /*IsAngled=*/false); - if (Insertion) - Diag << Insertion.getValue(); + << FixItHint::CreateReplacement(Matched->getEndLoc(), ")") + << Inserter->CreateIncludeInsertion( + Result.SourceManager->getMainFileID(), GslHeader, + /*IsAngled=*/false); } return; } diff --git a/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp b/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp --- a/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp @@ -429,11 +429,9 @@ if (MakeSmartPtrFunctionHeader.empty()) { return; } - if (auto IncludeFixit = Inserter->CreateIncludeInsertion( - FD, MakeSmartPtrFunctionHeader, - /*IsAngled=*/MakeSmartPtrFunctionHeader == StdMemoryHeader)) { - Diag << *IncludeFixit; - } + Diag << Inserter->CreateIncludeInsertion( + FD, MakeSmartPtrFunctionHeader, + /*IsAngled=*/MakeSmartPtrFunctionHeader == StdMemoryHeader); } } // namespace modernize diff --git a/clang/include/clang/Basic/Diagnostic.h b/clang/include/clang/Basic/Diagnostic.h --- a/clang/include/clang/Basic/Diagnostic.h +++ b/clang/include/clang/Basic/Diagnostic.h @@ -21,6 +21,7 @@ #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/IntrusiveRefCntPtr.h" +#include "llvm/ADT/Optional.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/iterator_range.h" @@ -1288,6 +1289,29 @@ return DB; } +inline const DiagnosticBuilder & +operator<<(const DiagnosticBuilder &DB, + const llvm::Optional &Opt) { + if (Opt) + DB << *Opt; + return DB; +} + +inline const DiagnosticBuilder & +operator<<(const DiagnosticBuilder &DB, + const llvm::Optional &Opt) { + if (Opt) + DB << *Opt; + return DB; +} + +inline const DiagnosticBuilder & +operator<<(const DiagnosticBuilder &DB, const llvm::Optional &Opt) { + if (Opt) + DB << *Opt; + return DB; +} + /// A nullability kind paired with a bit indicating whether it used a /// context-sensitive keyword. using DiagNullabilityKind = std::pair;