diff --git a/clang-tools-extra/clang-tidy/bugprone/StringviewNullptrCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/StringviewNullptrCheck.cpp --- a/clang-tools-extra/clang-tidy/bugprone/StringviewNullptrCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/StringviewNullptrCheck.cpp @@ -74,6 +74,11 @@ hasSourceExpression(StringViewConstructingFromNullExpr)), changeTo(node("null_argument_expr"), cat("\"\"")), construction_warning); + auto HandleTemporaryReturnValue = makeRule( + returnStmt( + hasReturnValue(ignoringImpCasts(StringViewConstructingFromNullExpr))), + changeTo(node("construct_expr"), cat("{}")), construction_warning); + auto HandleStackCopyInitialization = makeRule( varDecl(hasInitializer(implicitCastExpr( ignoringImpCasts(StringViewConstructingFromNullExpr)))), @@ -169,15 +174,23 @@ return applyFirst( {HandleTemporaryCXXFunctionalCastExpr, HandleTemporaryCXXTemporaryObjectExprAndCompoundLiteralExpr, - HandleTemporaryCStyleCastExpr, HandleTemporaryCXXStaticCastExpr, - HandleStackCopyInitialization, HandleStackDirectInitialization, + HandleTemporaryCStyleCastExpr, + HandleTemporaryCXXStaticCastExpr, + HandleTemporaryReturnValue, + HandleStackCopyInitialization, + HandleStackDirectInitialization, HandleStackDirectListAndCopyListInitialization, - HandleFieldCopyInitialization, HandleFieldOtherInitialization, - HandleConstructorInitialization, HandleDefaultArgumentInitialization, - HandleDefaultArgumentListInitialization, HandleHeapInitialization, + HandleFieldCopyInitialization, + HandleFieldOtherInitialization, + HandleConstructorInitialization, + HandleDefaultArgumentInitialization, + HandleDefaultArgumentListInitialization, + HandleHeapInitialization, HandleFunctionArgumentInitialization, - HandleFunctionArgumentListInitialization, HandleAssignment, - HandleRelativeComparison, HandleEmptyEqualityComparison, + HandleFunctionArgumentListInitialization, + HandleAssignment, + HandleRelativeComparison, + HandleEmptyEqualityComparison, HandleNonEmptyEqualityComparison}); } diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone-stringview-nullptr.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone-stringview-nullptr.cpp --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone-stringview-nullptr.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone-stringview-nullptr.cpp @@ -215,6 +215,37 @@ // CHECK-MESSAGES: :[[@LINE-1]]:48: warning: constructing{{.*}}default // CHECK-FIXES: {{^}} (void)(static_cast("")) /* a28 */; } + + // Return Value + { + (void)([] -> std::string_view { return nullptr; }) /* a29 */; + // CHECK-MESSAGES: :[[@LINE-1]]:44: warning: constructing{{.*}}default + // CHECK-FIXES: {{^}} (void)([] -> std::string_view { return {}; }) /* a29 */; + + (void)([] -> std::string_view { return (nullptr); }) /* a30 */; + // CHECK-MESSAGES: :[[@LINE-1]]:44: warning: constructing{{.*}}default + // CHECK-FIXES: {{^}} (void)([] -> std::string_view { return {}; }) /* a30 */; + + (void)([] -> std::string_view { return {nullptr}; }) /* a31 */; + // CHECK-MESSAGES: :[[@LINE-1]]:44: warning: constructing{{.*}}default + // CHECK-FIXES: {{^}} (void)([] -> std::string_view { return {}; }) /* a31 */; + + (void)([] -> std::string_view { return {(nullptr)}; }) /* a32 */; + // CHECK-MESSAGES: :[[@LINE-1]]:44: warning: constructing{{.*}}default + // CHECK-FIXES: {{^}} (void)([] -> std::string_view { return {}; }) /* a32 */; + + (void)([] -> std::string_view { return {{nullptr}}; }) /* a33 */; + // CHECK-MESSAGES: :[[@LINE-1]]:44: warning: constructing{{.*}}default + // CHECK-FIXES: {{^}} (void)([] -> std::string_view { return {}; }) /* a33 */; + + (void)([] -> std::string_view { return {{(nullptr)}}; }) /* a34 */; + // CHECK-MESSAGES: :[[@LINE-1]]:44: warning: constructing{{.*}}default + // CHECK-FIXES: {{^}} (void)([] -> std::string_view { return {}; }) /* a34 */; + + (void)([] -> std::string_view { return {{}}; }) /* a35 */; + // CHECK-MESSAGES: :[[@LINE-1]]:44: warning: constructing{{.*}}default + // CHECK-FIXES: {{^}} (void)([] -> std::string_view { return {}; }) /* a35 */; + } } void stack_construction() /* b */ {