diff --git a/clang-tools-extra/clang-tidy/bugprone/StandaloneEmptyCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/StandaloneEmptyCheck.cpp --- a/clang-tools-extra/clang-tidy/bugprone/StandaloneEmptyCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/StandaloneEmptyCheck.cpp @@ -98,6 +98,7 @@ const auto PParentStmtExpr = Result.Nodes.getNodeAs("stexpr"); const auto ParentCompStmt = Result.Nodes.getNodeAs("parent"); const auto *ParentCond = getCondition(Result.Nodes, "parent"); + const auto *ParentReturnStmt = Result.Nodes.getNodeAs("parent"); if (const auto *MemberCall = Result.Nodes.getNodeAs("empty")) { @@ -109,6 +110,9 @@ if (PParentStmtExpr && ParentCompStmt && ParentCompStmt->body_back() == MemberCall->getExprStmt()) return; + // Skip if it's a return statement + if (ParentReturnStmt) + return; SourceLocation MemberLoc = MemberCall->getBeginLoc(); SourceLocation ReplacementLoc = MemberCall->getExprLoc(); @@ -150,6 +154,8 @@ if (PParentStmtExpr && ParentCompStmt && ParentCompStmt->body_back() == NonMemberCall->getExprStmt()) return; + if (ParentReturnStmt) + return; SourceLocation NonMemberLoc = NonMemberCall->getExprLoc(); SourceLocation NonMemberEndLoc = NonMemberCall->getEndLoc(); diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/standalone-empty.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/standalone-empty.cpp --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/standalone-empty.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/standalone-empty.cpp @@ -155,7 +155,7 @@ } // namespace qualifiers -void test_member_empty() { +bool test_member_empty() { { std::vector v; v.empty(); @@ -231,9 +231,69 @@ s.empty(); // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'empty()' [bugprone-standalone-empty] } + + { + std::vector v; + return v.empty(); + // no-warning + } + + { + std::vector_with_clear v; + return v.empty(); + // no-warning + } + + { + std::vector_with_int_empty v; + return v.empty(); + // no-warning + } + + { + std::vector_with_clear_args v; + return v.empty(); + // no-warning + } + + { + std::vector_with_clear_variable v; + return v.empty(); + // no-warning + } + + { + absl::string s; + return s.empty(); + // no-warning + } + + { + absl::string_with_clear s; + return s.empty(); + // no-warning + } + + { + absl::string_with_int_empty s; + return s.empty(); + // no-warning + } + + { + absl::string_with_clear_args s; + return s.empty(); + // no-warning + } + + { + absl::string_with_clear_variable s; + return s.empty(); + // no-warning + } } -void test_qualified_empty() { +bool test_qualified_empty() { { absl::string_with_clear v; std::empty(v); @@ -260,9 +320,30 @@ absl::empty(nullptr); // no-warning } + + { + absl::string_with_clear s; + return std::empty(s); + // no-warning + return absl::empty(s); + // no-warning + } + + { + absl::string s; + return std::empty(s); + // no-warning + } + + { + return std::empty(0); + // no-warning + return absl::empty(nullptr); + // no-warning + } } -void test_unqualified_empty() { +bool test_unqualified_empty() { { std::vector v; empty(v); @@ -370,6 +451,106 @@ // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'absl::empty'; did you mean 'clear()'? [bugprone-standalone-empty] // CHECK-FIXES: {{^ }} s.clear();{{$}} } + + { + std::vector v; + return empty(v); + // no-warning + } + + { + std::vector_with_void_empty v; + return empty(v); + // no-warning + } + + { + std::vector_with_clear v; + return empty(v); + // no-warning + } + + { + std::vector_with_int_empty v; + return empty(v); + // no-warning + } + + { + std::vector_with_clear_args v; + return empty(v); + // no-warning + } + + { + std::vector_with_clear_variable v; + return empty(v); + // no-warning + } + + { + absl::string s; + return empty(s); + // no-warning + } + + { + absl::string_with_void_empty s; + return empty(s); + // no-warning + } + + { + absl::string_with_clear s; + return empty(s); + // no-warning + } + + { + absl::string_with_int_empty s; + return empty(s); + // no-warning + } + + { + absl::string_with_clear_args s; + return empty(s); + // no-warning + } + + { + absl::string_with_clear_variable s; + return empty(s); + // no-warning + } + + { + std::vector v; + using std::empty; + return empty(v); + // no-warning + } + + { + std::vector_with_clear v; + using std::empty; + return empty(v); + // no-warning + } + + { + absl::string s; + using absl::empty; + return empty(s); + // no-warning + } + + { + absl::string_with_clear s; + using absl::empty; + return empty(s); + // no-warning + } } void test_empty_method_expressions() { @@ -444,8 +625,7 @@ // CHECK-MESSAGES: :[[#@LINE-1]]:27: warning: ignoring the result of 'std::empty' [bugprone-standalone-empty] } -void test_clear_in_base_class() { - +bool test_clear_in_base_class() { { base::vector v; v.empty(); @@ -497,9 +677,57 @@ empty(v); // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'base::empty' [bugprone-standalone-empty] } + + { + base::vector v; + return v.empty(); + // no-warning + } + + { + base::vector_non_dependent v; + return v.empty(); + // no-warning + } + + { + base::vector_clear_with_args v; + return v.empty(); + // no-warning + } + + { + base::vector_clear_variable v; + return v.empty(); + // no-warning + } + + { + base::vector v; + return empty(v); + // no-warning + } + + { + base::vector_non_dependent v; + return empty(v); + // no-warning + } + + { + base::vector_clear_with_args v; + return empty(v); + // no-warning + } + + { + base::vector_clear_variable v; + return empty(v); + // no-warning + } } -void test_clear_with_qualifiers() { +bool test_clear_with_qualifiers() { { qualifiers::vector_with_const_clear v; v.empty(); @@ -575,4 +803,76 @@ empty(v); // CHECK-MESSAGES: :[[#@LINE-1]]:5: warning: ignoring the result of 'std::empty' [bugprone-standalone-empty] } + + { + qualifiers::vector_with_const_clear v; + return v.empty(); + // no-warning + } + + { + const qualifiers::vector_with_const_clear v; + return v.empty(); + // no-warning + } + + { + const qualifiers::vector_with_const_empty v; + return v.empty(); + // no-warning + } + + { + qualifiers::vector_with_const_clear v; + return empty(v); + // no-warning + } + + { + const qualifiers::vector_with_const_clear v; + return empty(v); + // no-warning + } + + { + const std::vector_with_clear v; + return empty(v); + // no-warning + } + + { + qualifiers::vector_with_volatile_clear v; + return v.empty(); + // no-warning + } + + { + volatile qualifiers::vector_with_volatile_clear v; + return v.empty(); + // no-warning + } + + { + volatile qualifiers::vector_with_volatile_empty v; + return v.empty(); + // no-warning + } + + { + qualifiers::vector_with_volatile_clear v; + return empty(v); + // no-warning + } + + { + volatile qualifiers::vector_with_volatile_clear v; + return empty(v); + // no-warning + } + + { + volatile std::vector_with_clear v; + return empty(v); + // no-warning + } }