[PATCH] D115121: Add support for return values in bugprone-stringview-nullptr
CJ Johnson via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Sun Dec 5 11:13:32 PST 2021
CJ-Johnson created this revision.
CJ-Johnson added a reviewer: ymandel.
Herald added a subscriber: carlosgalvezp.
CJ-Johnson requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.
bugprone-stringview-nullptr was not initially written with tests for return statements. After landing the check, the thought crossed my mind to add such tests. After writing them, I realized they needed additional handling in the matchers.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D115121
Files:
clang-tools-extra/clang-tidy/bugprone/StringviewNullptrCheck.cpp
clang-tools-extra/test/clang-tidy/checkers/bugprone-stringview-nullptr.cpp
Index: clang-tools-extra/test/clang-tidy/checkers/bugprone-stringview-nullptr.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/bugprone-stringview-nullptr.cpp
+++ 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<const std::string_view>("")) /* 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 */ {
Index: clang-tools-extra/clang-tidy/bugprone/StringviewNullptrCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/bugprone/StringviewNullptrCheck.cpp
+++ 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});
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D115121.391926.patch
Type: text/x-patch
Size: 4455 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20211205/dc77cfc3/attachment-0001.bin>
More information about the cfe-commits
mailing list