diff --git a/clang-tools-extra/clang-tidy/bugprone/ReturnConstRefFromParameterCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/ReturnConstRefFromParameterCheck.cpp index b3f7dd6d1c86f8..cacba38b4a5aa8 100644 --- a/clang-tools-extra/clang-tidy/bugprone/ReturnConstRefFromParameterCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/ReturnConstRefFromParameterCheck.cpp @@ -29,10 +29,13 @@ void ReturnConstRefFromParameterCheck::registerMatchers(MatchFinder *Finder) { void ReturnConstRefFromParameterCheck::check( const MatchFinder::MatchResult &Result) { const auto *R = Result.Nodes.getNodeAs("ret"); - diag(R->getRetValue()->getBeginLoc(), - "returning a constant reference parameter may cause a use-after-free " + const SourceRange Range = R->getRetValue()->getSourceRange(); + if (Range.isInvalid()) + return; + diag(Range.getBegin(), + "returning a constant reference parameter may cause use-after-free " "when the parameter is constructed from a temporary") - << R->getRetValue()->getSourceRange(); + << Range; } } // namespace clang::tidy::bugprone