diff --git a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp --- a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp @@ -48,6 +48,7 @@ #include "SizeofContainerCheck.h" #include "SizeofExpressionCheck.h" #include "SpuriouslyWakeUpFunctionsCheck.h" +#include "StandaloneEmptyCheck.h" #include "StringConstructorCheck.h" #include "StringIntegerAssignmentCheck.h" #include "StringLiteralWithEmbeddedNulCheck.h" @@ -156,6 +157,8 @@ "bugprone-sizeof-expression"); CheckFactories.registerCheck( "bugprone-spuriously-wake-up-functions"); + CheckFactories.registerCheck( + "bugprone-standalone-empty"); CheckFactories.registerCheck( "bugprone-string-constructor"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt --- a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt @@ -44,6 +44,7 @@ SizeofContainerCheck.cpp SizeofExpressionCheck.cpp SpuriouslyWakeUpFunctionsCheck.cpp + StandaloneEmptyCheck.cpp StringConstructorCheck.cpp StringIntegerAssignmentCheck.cpp StringLiteralWithEmbeddedNulCheck.cpp diff --git a/clang-tools-extra/clang-tidy/bugprone/StandaloneEmptyCheck.h b/clang-tools-extra/clang-tidy/bugprone/StandaloneEmptyCheck.h new file mode 100644 --- /dev/null +++ b/clang-tools-extra/clang-tidy/bugprone/StandaloneEmptyCheck.h @@ -0,0 +1,34 @@ +//===--- StandaloneEmptyCheck.h - clang-tidy --------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_STANDALONEEMPTYCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_STANDALONEEMPTYCHECK_H + +#include "../ClangTidyCheck.h" + +namespace clang { +namespace tidy { +namespace bugprone { + +/// FIXME: Write a short description. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/bugprone-standalone-empty.html +class StandaloneEmptyCheck : public ClangTidyCheck { +public: + StandaloneEmptyCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace bugprone +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_STANDALONEEMPTYCHECK_H diff --git a/clang-tools-extra/clang-tidy/bugprone/StandaloneEmptyCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/StandaloneEmptyCheck.cpp new file mode 100644 --- /dev/null +++ b/clang-tools-extra/clang-tidy/bugprone/StandaloneEmptyCheck.cpp @@ -0,0 +1,84 @@ +//===--- StandaloneEmptyCheck.cpp - clang-tidy ----------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "StandaloneEmptyCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclBase.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/Expr.h" +#include "clang/AST/ExprCXX.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Lex/Lexer.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SmallVector.h" + +namespace clang { +namespace tidy { +namespace bugprone { + +void StandaloneEmptyCheck::registerMatchers(ast_matchers::MatchFinder *Finder) { + const auto CallMatcher = + ast_matchers::callExpr(ast_matchers::callee(ast_matchers::functionDecl( + ast_matchers::hasName("empty")))) + .bind("empty"); + const auto MemberMatcher = + ast_matchers::cxxMemberCallExpr( + ast_matchers::callee( + ast_matchers::cxxMethodDecl(ast_matchers::hasName("empty")))) + .bind("empty"); + + Finder->addMatcher(MemberMatcher, this); + Finder->addMatcher(CallMatcher, this); +} + +void StandaloneEmptyCheck::check( + const ast_matchers::MatchFinder::MatchResult &Result) { + if (const auto *MemberCall = + Result.Nodes.getNodeAs("empty")) { + SourceLocation MemberLoc = MemberCall->getBeginLoc(); + SourceLocation ReplacementLoc = MemberCall->getExprLoc(); + SourceRange ReplacementRange = SourceRange(ReplacementLoc, ReplacementLoc); + + auto Methods = MemberCall->getRecordDecl()->methods(); + auto Clear = llvm::find_if(Methods, [](const CXXMethodDecl *F) { + return F->getDeclName().getAsString() == "clear" && + F->getMinRequiredArguments() == 0; + }); + + if (Clear != Methods.end()) { + DiagnosticBuilder Builder = + diag(MemberLoc, + "ignoring the result of 'empty()', did you mean 'clear()'? "); + Builder << FixItHint::CreateReplacement(ReplacementRange, "clear"); + } + + } else if (const auto *NonMemberCall = + Result.Nodes.getNodeAs("empty")) { + SourceLocation NonMemberLoc = NonMemberCall->getExprLoc(); + SourceLocation NonMemberEndLoc = NonMemberCall->getEndLoc(); + const Expr *Arg = NonMemberCall->getArg(0); + std::string ReplacementText = + std::string(Lexer::getSourceText( + CharSourceRange::getTokenRange(Arg->getSourceRange()), + *Result.SourceManager, getLangOpts())) + + ".clear()"; + SourceRange ReplacementRange = SourceRange(NonMemberLoc, NonMemberEndLoc); + diag(NonMemberLoc, "ignoring the result of '%0'") + << llvm::dyn_cast(NonMemberCall->getCalleeDecl()) + ->getQualifiedNameAsString() + << FixItHint::CreateReplacement(ReplacementRange, ReplacementText); + } +} + +} // namespace bugprone +} // namespace tidy +} // namespace clang diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -117,6 +117,11 @@ Finds initializations of C++ shared pointers to non-array type that are initialized with an array. +- New :doc:`bugprone-standalone-empty + ` check. + + Warns when `empty()` is used on a container and the result is ignored. + - New :doc:`bugprone-unchecked-optional-access ` check. diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst --- a/clang-tools-extra/docs/clang-tidy/checks/list.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -36,328 +36,329 @@ .. csv-table:: :header: "Name", "Offers fixes" - `abseil-cleanup-ctad `_, "Yes" - `abseil-duration-addition `_, "Yes" - `abseil-duration-comparison `_, "Yes" - `abseil-duration-conversion-cast `_, "Yes" - `abseil-duration-division `_, "Yes" - `abseil-duration-factory-float `_, "Yes" - `abseil-duration-factory-scale `_, "Yes" - `abseil-duration-subtraction `_, "Yes" - `abseil-duration-unnecessary-conversion `_, "Yes" - `abseil-faster-strsplit-delimiter `_, "Yes" - `abseil-no-internal-dependencies `_, - `abseil-no-namespace `_, - `abseil-redundant-strcat-calls `_, "Yes" - `abseil-str-cat-append `_, "Yes" - `abseil-string-find-startswith `_, "Yes" - `abseil-string-find-str-contains `_, "Yes" - `abseil-time-comparison `_, "Yes" - `abseil-time-subtraction `_, "Yes" - `abseil-upgrade-duration-conversions `_, "Yes" - `altera-id-dependent-backward-branch `_, - `altera-kernel-name-restriction `_, - `altera-single-work-item-barrier `_, - `altera-struct-pack-align `_, "Yes" - `altera-unroll-loops `_, - `android-cloexec-accept `_, "Yes" - `android-cloexec-accept4 `_, "Yes" - `android-cloexec-creat `_, "Yes" - `android-cloexec-dup `_, "Yes" - `android-cloexec-epoll-create `_, "Yes" - `android-cloexec-epoll-create1 `_, "Yes" - `android-cloexec-fopen `_, "Yes" - `android-cloexec-inotify-init `_, "Yes" - `android-cloexec-inotify-init1 `_, "Yes" - `android-cloexec-memfd-create `_, "Yes" - `android-cloexec-open `_, "Yes" - `android-cloexec-pipe `_, "Yes" - `android-cloexec-pipe2 `_, "Yes" - `android-cloexec-socket `_, "Yes" - `android-comparison-in-temp-failure-retry `_, - `boost-use-to-string `_, "Yes" - `bugprone-argument-comment `_, "Yes" - `bugprone-assert-side-effect `_, - `bugprone-bad-signal-to-kill-thread `_, - `bugprone-bool-pointer-implicit-conversion `_, "Yes" - `bugprone-branch-clone `_, - `bugprone-copy-constructor-init `_, "Yes" - `bugprone-dangling-handle `_, - `bugprone-dynamic-static-initializers `_, - `bugprone-easily-swappable-parameters `_, - `bugprone-exception-escape `_, - `bugprone-fold-init-type `_, - `bugprone-forward-declaration-namespace `_, - `bugprone-forwarding-reference-overload `_, - `bugprone-implicit-widening-of-multiplication-result `_, "Yes" - `bugprone-inaccurate-erase `_, "Yes" - `bugprone-incorrect-roundings `_, - `bugprone-infinite-loop `_, - `bugprone-integer-division `_, - `bugprone-lambda-function-name `_, - `bugprone-macro-parentheses `_, "Yes" - `bugprone-macro-repeated-side-effects `_, - `bugprone-misplaced-operator-in-strlen-in-alloc `_, "Yes" - `bugprone-misplaced-pointer-arithmetic-in-alloc `_, "Yes" - `bugprone-misplaced-widening-cast `_, - `bugprone-move-forwarding-reference `_, "Yes" - `bugprone-multiple-statement-macro `_, - `bugprone-no-escape `_, - `bugprone-not-null-terminated-result `_, "Yes" - `bugprone-parent-virtual-call `_, "Yes" - `bugprone-posix-return `_, "Yes" - `bugprone-redundant-branch-condition `_, "Yes" - `bugprone-reserved-identifier `_, "Yes" - `bugprone-shared-ptr-array-mismatch `_, "Yes" - `bugprone-signal-handler `_, - `bugprone-signed-char-misuse `_, - `bugprone-sizeof-container `_, - `bugprone-sizeof-expression `_, - `bugprone-spuriously-wake-up-functions `_, - `bugprone-string-constructor `_, "Yes" - `bugprone-string-integer-assignment `_, "Yes" - `bugprone-string-literal-with-embedded-nul `_, - `bugprone-stringview-nullptr `_, "Yes" - `bugprone-suspicious-enum-usage `_, - `bugprone-suspicious-include `_, - `bugprone-suspicious-memory-comparison `_, - `bugprone-suspicious-memset-usage `_, "Yes" - `bugprone-suspicious-missing-comma `_, - `bugprone-suspicious-semicolon `_, "Yes" - `bugprone-suspicious-string-compare `_, "Yes" - `bugprone-swapped-arguments `_, "Yes" - `bugprone-terminating-continue `_, "Yes" - `bugprone-throw-keyword-missing `_, - `bugprone-too-small-loop-variable `_, - `bugprone-unchecked-optional-access `_, - `bugprone-undefined-memory-manipulation `_, - `bugprone-undelegated-constructor `_, - `bugprone-unhandled-exception-at-new `_, - `bugprone-unhandled-self-assignment `_, - `bugprone-unused-raii `_, "Yes" - `bugprone-unused-return-value `_, - `bugprone-use-after-move `_, - `bugprone-virtual-near-miss `_, "Yes" - `cert-dcl21-cpp `_, "Yes" - `cert-dcl50-cpp `_, - `cert-dcl58-cpp `_, - `cert-env33-c `_, - `cert-err33-c `_, - `cert-err34-c `_, - `cert-err52-cpp `_, - `cert-err58-cpp `_, - `cert-err60-cpp `_, - `cert-flp30-c `_, - `cert-mem57-cpp `_, - `cert-msc50-cpp `_, - `cert-msc51-cpp `_, - `cert-oop57-cpp `_, - `cert-oop58-cpp `_, - `clang-analyzer-core.DynamicTypePropagation `_, - `clang-analyzer-core.uninitialized.CapturedBlockVariable `_, - `clang-analyzer-cplusplus.InnerPointer `_, - `clang-analyzer-nullability.NullableReturnedFromNonnull `_, - `clang-analyzer-optin.osx.OSObjectCStyleCast `_, - `clang-analyzer-optin.performance.GCDAntipattern `_, - `clang-analyzer-optin.performance.Padding `_, - `clang-analyzer-optin.portability.UnixAPI `_, - `clang-analyzer-osx.MIG `_, - `clang-analyzer-osx.NumberObjectConversion `_, - `clang-analyzer-osx.OSObjectRetainCount `_, - `clang-analyzer-osx.ObjCProperty `_, - `clang-analyzer-osx.cocoa.AutoreleaseWrite `_, - `clang-analyzer-osx.cocoa.Loops `_, - `clang-analyzer-osx.cocoa.MissingSuperCall `_, - `clang-analyzer-osx.cocoa.NonNilReturnValue `_, - `clang-analyzer-osx.cocoa.RunLoopAutoreleaseLeak `_, - `clang-analyzer-valist.CopyToSelf `_, - `clang-analyzer-valist.Uninitialized `_, - `clang-analyzer-valist.Unterminated `_, - `concurrency-mt-unsafe `_, - `concurrency-thread-canceltype-asynchronous `_, - `cppcoreguidelines-avoid-goto `_, - `cppcoreguidelines-avoid-non-const-global-variables `_, - `cppcoreguidelines-init-variables `_, "Yes" - `cppcoreguidelines-interfaces-global-init `_, - `cppcoreguidelines-macro-usage `_, - `cppcoreguidelines-narrowing-conversions `_, - `cppcoreguidelines-no-malloc `_, - `cppcoreguidelines-owning-memory `_, - `cppcoreguidelines-prefer-member-initializer `_, "Yes" - `cppcoreguidelines-pro-bounds-array-to-pointer-decay `_, - `cppcoreguidelines-pro-bounds-constant-array-index `_, "Yes" - `cppcoreguidelines-pro-bounds-pointer-arithmetic `_, - `cppcoreguidelines-pro-type-const-cast `_, - `cppcoreguidelines-pro-type-cstyle-cast `_, "Yes" - `cppcoreguidelines-pro-type-member-init `_, "Yes" - `cppcoreguidelines-pro-type-reinterpret-cast `_, - `cppcoreguidelines-pro-type-static-cast-downcast `_, "Yes" - `cppcoreguidelines-pro-type-union-access `_, - `cppcoreguidelines-pro-type-vararg `_, - `cppcoreguidelines-slicing `_, - `cppcoreguidelines-special-member-functions `_, - `cppcoreguidelines-virtual-class-destructor `_, "Yes" - `darwin-avoid-spinlock `_, - `darwin-dispatch-once-nonstatic `_, "Yes" - `fuchsia-default-arguments-calls `_, - `fuchsia-default-arguments-declarations `_, "Yes" - `fuchsia-multiple-inheritance `_, - `fuchsia-overloaded-operator `_, - `fuchsia-statically-constructed-objects `_, - `fuchsia-trailing-return `_, - `fuchsia-virtual-inheritance `_, - `google-build-explicit-make-pair `_, - `google-build-namespaces `_, - `google-build-using-namespace `_, - `google-default-arguments `_, - `google-explicit-constructor `_, "Yes" - `google-global-names-in-headers `_, - `google-objc-avoid-nsobject-new `_, - `google-objc-avoid-throwing-exception `_, - `google-objc-function-naming `_, - `google-objc-global-variable-declaration `_, - `google-readability-avoid-underscore-in-googletest-name `_, - `google-readability-casting `_, - `google-readability-todo `_, - `google-runtime-int `_, - `google-runtime-operator `_, - `google-upgrade-googletest-case `_, "Yes" - `hicpp-avoid-goto `_, - `hicpp-exception-baseclass `_, - `hicpp-multiway-paths-covered `_, - `hicpp-no-assembler `_, - `hicpp-signed-bitwise `_, - `linuxkernel-must-use-errs `_, - `llvm-header-guard `_, - `llvm-include-order `_, "Yes" - `llvm-namespace-comment `_, - `llvm-prefer-isa-or-dyn-cast-in-conditionals `_, "Yes" - `llvm-prefer-register-over-unsigned `_, "Yes" - `llvm-twine-local `_, "Yes" - `llvmlibc-callee-namespace `_, - `llvmlibc-implementation-in-namespace `_, - `llvmlibc-restrict-system-libc-headers `_, "Yes" - `misc-definitions-in-headers `_, "Yes" - `misc-misleading-bidirectional `_, - `misc-misleading-identifier `_, - `misc-misplaced-const `_, - `misc-new-delete-overloads `_, - `misc-no-recursion `_, - `misc-non-copyable-objects `_, - `misc-non-private-member-variables-in-classes `_, - `misc-redundant-expression `_, "Yes" - `misc-static-assert `_, "Yes" - `misc-throw-by-value-catch-by-reference `_, - `misc-unconventional-assign-operator `_, - `misc-uniqueptr-reset-release `_, "Yes" - `misc-unused-alias-decls `_, "Yes" - `misc-unused-parameters `_, "Yes" - `misc-unused-using-decls `_, "Yes" - `modernize-avoid-bind `_, "Yes" - `modernize-avoid-c-arrays `_, - `modernize-concat-nested-namespaces `_, "Yes" - `modernize-deprecated-headers `_, "Yes" - `modernize-deprecated-ios-base-aliases `_, "Yes" - `modernize-loop-convert `_, "Yes" - `modernize-macro-to-enum `_, "Yes" - `modernize-make-shared `_, "Yes" - `modernize-make-unique `_, "Yes" - `modernize-pass-by-value `_, "Yes" - `modernize-raw-string-literal `_, "Yes" - `modernize-redundant-void-arg `_, "Yes" - `modernize-replace-auto-ptr `_, "Yes" - `modernize-replace-disallow-copy-and-assign-macro `_, "Yes" - `modernize-replace-random-shuffle `_, "Yes" - `modernize-return-braced-init-list `_, "Yes" - `modernize-shrink-to-fit `_, "Yes" - `modernize-unary-static-assert `_, "Yes" - `modernize-use-auto `_, "Yes" - `modernize-use-bool-literals `_, "Yes" - `modernize-use-default-member-init `_, "Yes" - `modernize-use-emplace `_, "Yes" - `modernize-use-equals-default `_, "Yes" - `modernize-use-equals-delete `_, "Yes" - `modernize-use-nodiscard `_, "Yes" - `modernize-use-noexcept `_, "Yes" - `modernize-use-nullptr `_, "Yes" - `modernize-use-override `_, "Yes" - `modernize-use-trailing-return-type `_, "Yes" - `modernize-use-transparent-functors `_, "Yes" - `modernize-use-uncaught-exceptions `_, "Yes" - `modernize-use-using `_, "Yes" - `mpi-buffer-deref `_, "Yes" - `mpi-type-mismatch `_, "Yes" - `objc-assert-equals `_, "Yes" - `objc-avoid-nserror-init `_, - `objc-dealloc-in-category `_, - `objc-forbidden-subclassing `_, - `objc-missing-hash `_, - `objc-nsinvocation-argument-lifetime `_, "Yes" - `objc-property-declaration `_, "Yes" - `objc-super-self `_, "Yes" - `openmp-exception-escape `_, - `openmp-use-default-none `_, - `performance-faster-string-find `_, "Yes" - `performance-for-range-copy `_, "Yes" - `performance-implicit-conversion-in-loop `_, - `performance-inefficient-algorithm `_, "Yes" - `performance-inefficient-string-concatenation `_, - `performance-inefficient-vector-operation `_, "Yes" - `performance-move-const-arg `_, "Yes" - `performance-move-constructor-init `_, - `performance-no-automatic-move `_, - `performance-no-int-to-ptr `_, - `performance-noexcept-move-constructor `_, "Yes" - `performance-trivially-destructible `_, "Yes" - `performance-type-promotion-in-math-fn `_, "Yes" - `performance-unnecessary-copy-initialization `_, "Yes" - `performance-unnecessary-value-param `_, "Yes" - `portability-restrict-system-includes `_, "Yes" - `portability-simd-intrinsics `_, - `portability-std-allocator-const `_, - `readability-avoid-const-params-in-decls `_, "Yes" - `readability-braces-around-statements `_, "Yes" - `readability-const-return-type `_, "Yes" - `readability-container-contains `_, "Yes" - `readability-container-data-pointer `_, "Yes" - `readability-container-size-empty `_, "Yes" - `readability-convert-member-functions-to-static `_, "Yes" - `readability-delete-null-pointer `_, "Yes" - `readability-duplicate-include `_, "Yes" - `readability-else-after-return `_, "Yes" - `readability-function-cognitive-complexity `_, - `readability-function-size `_, - `readability-identifier-length `_, - `readability-identifier-naming `_, "Yes" - `readability-implicit-bool-conversion `_, "Yes" - `readability-inconsistent-declaration-parameter-name `_, "Yes" - `readability-isolate-declaration `_, "Yes" - `readability-magic-numbers `_, - `readability-make-member-function-const `_, "Yes" - `readability-misleading-indentation `_, - `readability-misplaced-array-index `_, "Yes" - `readability-named-parameter `_, "Yes" - `readability-non-const-parameter `_, "Yes" - `readability-qualified-auto `_, "Yes" - `readability-redundant-access-specifiers `_, "Yes" - `readability-redundant-control-flow `_, "Yes" - `readability-redundant-declaration `_, "Yes" - `readability-redundant-function-ptr-dereference `_, "Yes" - `readability-redundant-member-init `_, "Yes" - `readability-redundant-preprocessor `_, - `readability-redundant-smartptr-get `_, "Yes" - `readability-redundant-string-cstr `_, "Yes" - `readability-redundant-string-init `_, "Yes" - `readability-simplify-boolean-expr `_, "Yes" - `readability-simplify-subscript-expr `_, "Yes" - `readability-static-accessed-through-instance `_, "Yes" - `readability-static-definition-in-anonymous-namespace `_, "Yes" - `readability-string-compare `_, "Yes" - `readability-suspicious-call-argument `_, - `readability-uniqueptr-delete-release `_, "Yes" - `readability-uppercase-literal-suffix `_, "Yes" - `readability-use-anyofallof `_, - `zircon-temporary-objects `_, + `abseil-cleanup-ctad `_, "Yes" + `abseil-duration-addition `_, "Yes" + `abseil-duration-comparison `_, "Yes" + `abseil-duration-conversion-cast `_, "Yes" + `abseil-duration-division `_, "Yes" + `abseil-duration-factory-float `_, "Yes" + `abseil-duration-factory-scale `_, "Yes" + `abseil-duration-subtraction `_, "Yes" + `abseil-duration-unnecessary-conversion `_, "Yes" + `abseil-faster-strsplit-delimiter `_, "Yes" + `abseil-no-internal-dependencies `_, + `abseil-no-namespace `_, + `abseil-redundant-strcat-calls `_, "Yes" + `abseil-str-cat-append `_, "Yes" + `abseil-string-find-startswith `_, "Yes" + `abseil-string-find-str-contains `_, "Yes" + `abseil-time-comparison `_, "Yes" + `abseil-time-subtraction `_, "Yes" + `abseil-upgrade-duration-conversions `_, "Yes" + `altera-id-dependent-backward-branch `_, + `altera-kernel-name-restriction `_, + `altera-single-work-item-barrier `_, + `altera-struct-pack-align `_, "Yes" + `altera-unroll-loops `_, + `android-cloexec-accept `_, "Yes" + `android-cloexec-accept4 `_, "Yes" + `android-cloexec-creat `_, "Yes" + `android-cloexec-dup `_, "Yes" + `android-cloexec-epoll-create `_, "Yes" + `android-cloexec-epoll-create1 `_, "Yes" + `android-cloexec-fopen `_, "Yes" + `android-cloexec-inotify-init `_, "Yes" + `android-cloexec-inotify-init1 `_, "Yes" + `android-cloexec-memfd-create `_, "Yes" + `android-cloexec-open `_, "Yes" + `android-cloexec-pipe `_, "Yes" + `android-cloexec-pipe2 `_, "Yes" + `android-cloexec-socket `_, "Yes" + `android-comparison-in-temp-failure-retry `_, + `boost-use-to-string `_, "Yes" + `bugprone-argument-comment `_, "Yes" + `bugprone-assert-side-effect `_, + `bugprone-bad-signal-to-kill-thread `_, + `bugprone-bool-pointer-implicit-conversion `_, "Yes" + `bugprone-branch-clone `_, + `bugprone-copy-constructor-init `_, "Yes" + `bugprone-dangling-handle `_, + `bugprone-dynamic-static-initializers `_, + `bugprone-easily-swappable-parameters `_, + `bugprone-exception-escape `_, + `bugprone-fold-init-type `_, + `bugprone-forward-declaration-namespace `_, + `bugprone-forwarding-reference-overload `_, + `bugprone-implicit-widening-of-multiplication-result `_, "Yes" + `bugprone-inaccurate-erase `_, "Yes" + `bugprone-incorrect-roundings `_, + `bugprone-infinite-loop `_, + `bugprone-integer-division `_, + `bugprone-lambda-function-name `_, + `bugprone-macro-parentheses `_, "Yes" + `bugprone-macro-repeated-side-effects `_, + `bugprone-misplaced-operator-in-strlen-in-alloc `_, "Yes" + `bugprone-misplaced-pointer-arithmetic-in-alloc `_, "Yes" + `bugprone-misplaced-widening-cast `_, + `bugprone-move-forwarding-reference `_, "Yes" + `bugprone-multiple-statement-macro `_, + `bugprone-no-escape `_, + `bugprone-not-null-terminated-result `_, "Yes" + `bugprone-parent-virtual-call `_, "Yes" + `bugprone-posix-return `_, "Yes" + `bugprone-redundant-branch-condition `_, "Yes" + `bugprone-reserved-identifier `_, "Yes" + `bugprone-shared-ptr-array-mismatch `_, "Yes" + `bugprone-signal-handler `_, + `bugprone-signed-char-misuse `_, + `bugprone-sizeof-container `_, + `bugprone-sizeof-expression `_, + `bugprone-spuriously-wake-up-functions `_, + `bugprone-standalone-empty `_, "Yes" + `bugprone-string-constructor `_, "Yes" + `bugprone-string-integer-assignment `_, "Yes" + `bugprone-string-literal-with-embedded-nul `_, + `bugprone-stringview-nullptr `_, "Yes" + `bugprone-suspicious-enum-usage `_, + `bugprone-suspicious-include `_, + `bugprone-suspicious-memory-comparison `_, + `bugprone-suspicious-memset-usage `_, "Yes" + `bugprone-suspicious-missing-comma `_, + `bugprone-suspicious-semicolon `_, "Yes" + `bugprone-suspicious-string-compare `_, "Yes" + `bugprone-swapped-arguments `_, "Yes" + `bugprone-terminating-continue `_, "Yes" + `bugprone-throw-keyword-missing `_, + `bugprone-too-small-loop-variable `_, + `bugprone-unchecked-optional-access `_, + `bugprone-undefined-memory-manipulation `_, + `bugprone-undelegated-constructor `_, + `bugprone-unhandled-exception-at-new `_, + `bugprone-unhandled-self-assignment `_, + `bugprone-unused-raii `_, "Yes" + `bugprone-unused-return-value `_, + `bugprone-use-after-move `_, + `bugprone-virtual-near-miss `_, "Yes" + `cert-dcl21-cpp `_, "Yes" + `cert-dcl50-cpp `_, + `cert-dcl58-cpp `_, + `cert-env33-c `_, + `cert-err33-c `_, + `cert-err34-c `_, + `cert-err52-cpp `_, + `cert-err58-cpp `_, + `cert-err60-cpp `_, + `cert-flp30-c `_, + `cert-mem57-cpp `_, + `cert-msc50-cpp `_, + `cert-msc51-cpp `_, + `cert-oop57-cpp `_, + `cert-oop58-cpp `_, + `clang-analyzer-core.DynamicTypePropagation `_, + `clang-analyzer-core.uninitialized.CapturedBlockVariable `_, + `clang-analyzer-cplusplus.InnerPointer `_, + `clang-analyzer-nullability.NullableReturnedFromNonnull `_, + `clang-analyzer-optin.osx.OSObjectCStyleCast `_, + `clang-analyzer-optin.performance.GCDAntipattern `_, + `clang-analyzer-optin.performance.Padding `_, + `clang-analyzer-optin.portability.UnixAPI `_, + `clang-analyzer-osx.MIG `_, + `clang-analyzer-osx.NumberObjectConversion `_, + `clang-analyzer-osx.OSObjectRetainCount `_, + `clang-analyzer-osx.ObjCProperty `_, + `clang-analyzer-osx.cocoa.AutoreleaseWrite `_, + `clang-analyzer-osx.cocoa.Loops `_, + `clang-analyzer-osx.cocoa.MissingSuperCall `_, + `clang-analyzer-osx.cocoa.NonNilReturnValue `_, + `clang-analyzer-osx.cocoa.RunLoopAutoreleaseLeak `_, + `clang-analyzer-valist.CopyToSelf `_, + `clang-analyzer-valist.Uninitialized `_, + `clang-analyzer-valist.Unterminated `_, + `concurrency-mt-unsafe `_, + `concurrency-thread-canceltype-asynchronous `_, + `cppcoreguidelines-avoid-goto `_, + `cppcoreguidelines-avoid-non-const-global-variables `_, + `cppcoreguidelines-init-variables `_, "Yes" + `cppcoreguidelines-interfaces-global-init `_, + `cppcoreguidelines-macro-usage `_, + `cppcoreguidelines-narrowing-conversions `_, + `cppcoreguidelines-no-malloc `_, + `cppcoreguidelines-owning-memory `_, + `cppcoreguidelines-prefer-member-initializer `_, "Yes" + `cppcoreguidelines-pro-bounds-array-to-pointer-decay `_, + `cppcoreguidelines-pro-bounds-constant-array-index `_, "Yes" + `cppcoreguidelines-pro-bounds-pointer-arithmetic `_, + `cppcoreguidelines-pro-type-const-cast `_, + `cppcoreguidelines-pro-type-cstyle-cast `_, "Yes" + `cppcoreguidelines-pro-type-member-init `_, "Yes" + `cppcoreguidelines-pro-type-reinterpret-cast `_, + `cppcoreguidelines-pro-type-static-cast-downcast `_, "Yes" + `cppcoreguidelines-pro-type-union-access `_, + `cppcoreguidelines-pro-type-vararg `_, + `cppcoreguidelines-slicing `_, + `cppcoreguidelines-special-member-functions `_, + `cppcoreguidelines-virtual-class-destructor `_, "Yes" + `darwin-avoid-spinlock `_, + `darwin-dispatch-once-nonstatic `_, "Yes" + `fuchsia-default-arguments-calls `_, + `fuchsia-default-arguments-declarations `_, "Yes" + `fuchsia-multiple-inheritance `_, + `fuchsia-overloaded-operator `_, + `fuchsia-statically-constructed-objects `_, + `fuchsia-trailing-return `_, + `fuchsia-virtual-inheritance `_, + `google-build-explicit-make-pair `_, + `google-build-namespaces `_, + `google-build-using-namespace `_, + `google-default-arguments `_, + `google-explicit-constructor `_, "Yes" + `google-global-names-in-headers `_, + `google-objc-avoid-nsobject-new `_, + `google-objc-avoid-throwing-exception `_, + `google-objc-function-naming `_, + `google-objc-global-variable-declaration `_, + `google-readability-avoid-underscore-in-googletest-name `_, + `google-readability-casting `_, + `google-readability-todo `_, + `google-runtime-int `_, + `google-runtime-operator `_, + `google-upgrade-googletest-case `_, "Yes" + `hicpp-avoid-goto `_, + `hicpp-exception-baseclass `_, + `hicpp-multiway-paths-covered `_, + `hicpp-no-assembler `_, + `hicpp-signed-bitwise `_, + `linuxkernel-must-use-errs `_, + `llvm-header-guard `_, + `llvm-include-order `_, "Yes" + `llvm-namespace-comment `_, + `llvm-prefer-isa-or-dyn-cast-in-conditionals `_, "Yes" + `llvm-prefer-register-over-unsigned `_, "Yes" + `llvm-twine-local `_, "Yes" + `llvmlibc-callee-namespace `_, + `llvmlibc-implementation-in-namespace `_, + `llvmlibc-restrict-system-libc-headers `_, "Yes" + `misc-definitions-in-headers `_, "Yes" + `misc-misleading-bidirectional `_, + `misc-misleading-identifier `_, + `misc-misplaced-const `_, + `misc-new-delete-overloads `_, + `misc-no-recursion `_, + `misc-non-copyable-objects `_, + `misc-non-private-member-variables-in-classes `_, + `misc-redundant-expression `_, "Yes" + `misc-static-assert `_, "Yes" + `misc-throw-by-value-catch-by-reference `_, + `misc-unconventional-assign-operator `_, + `misc-uniqueptr-reset-release `_, "Yes" + `misc-unused-alias-decls `_, "Yes" + `misc-unused-parameters `_, "Yes" + `misc-unused-using-decls `_, "Yes" + `modernize-avoid-bind `_, "Yes" + `modernize-avoid-c-arrays `_, + `modernize-concat-nested-namespaces `_, "Yes" + `modernize-deprecated-headers `_, "Yes" + `modernize-deprecated-ios-base-aliases `_, "Yes" + `modernize-loop-convert `_, "Yes" + `modernize-macro-to-enum `_, "Yes" + `modernize-make-shared `_, "Yes" + `modernize-make-unique `_, "Yes" + `modernize-pass-by-value `_, "Yes" + `modernize-raw-string-literal `_, "Yes" + `modernize-redundant-void-arg `_, "Yes" + `modernize-replace-auto-ptr `_, "Yes" + `modernize-replace-disallow-copy-and-assign-macro `_, "Yes" + `modernize-replace-random-shuffle `_, "Yes" + `modernize-return-braced-init-list `_, "Yes" + `modernize-shrink-to-fit `_, "Yes" + `modernize-unary-static-assert `_, "Yes" + `modernize-use-auto `_, "Yes" + `modernize-use-bool-literals `_, "Yes" + `modernize-use-default-member-init `_, "Yes" + `modernize-use-emplace `_, "Yes" + `modernize-use-equals-default `_, "Yes" + `modernize-use-equals-delete `_, "Yes" + `modernize-use-nodiscard `_, "Yes" + `modernize-use-noexcept `_, "Yes" + `modernize-use-nullptr `_, "Yes" + `modernize-use-override `_, "Yes" + `modernize-use-trailing-return-type `_, "Yes" + `modernize-use-transparent-functors `_, "Yes" + `modernize-use-uncaught-exceptions `_, "Yes" + `modernize-use-using `_, "Yes" + `mpi-buffer-deref `_, "Yes" + `mpi-type-mismatch `_, "Yes" + `objc-assert-equals `_, "Yes" + `objc-avoid-nserror-init `_, + `objc-dealloc-in-category `_, + `objc-forbidden-subclassing `_, + `objc-missing-hash `_, + `objc-nsinvocation-argument-lifetime `_, "Yes" + `objc-property-declaration `_, "Yes" + `objc-super-self `_, "Yes" + `openmp-exception-escape `_, + `openmp-use-default-none `_, + `performance-faster-string-find `_, "Yes" + `performance-for-range-copy `_, "Yes" + `performance-implicit-conversion-in-loop `_, + `performance-inefficient-algorithm `_, "Yes" + `performance-inefficient-string-concatenation `_, + `performance-inefficient-vector-operation `_, "Yes" + `performance-move-const-arg `_, "Yes" + `performance-move-constructor-init `_, + `performance-no-automatic-move `_, + `performance-no-int-to-ptr `_, + `performance-noexcept-move-constructor `_, "Yes" + `performance-trivially-destructible `_, "Yes" + `performance-type-promotion-in-math-fn `_, "Yes" + `performance-unnecessary-copy-initialization `_, "Yes" + `performance-unnecessary-value-param `_, "Yes" + `portability-restrict-system-includes `_, "Yes" + `portability-simd-intrinsics `_, + `portability-std-allocator-const `_, + `readability-avoid-const-params-in-decls `_, "Yes" + `readability-braces-around-statements `_, "Yes" + `readability-const-return-type `_, "Yes" + `readability-container-contains `_, "Yes" + `readability-container-data-pointer `_, "Yes" + `readability-container-size-empty `_, "Yes" + `readability-convert-member-functions-to-static `_, "Yes" + `readability-delete-null-pointer `_, "Yes" + `readability-duplicate-include `_, "Yes" + `readability-else-after-return `_, "Yes" + `readability-function-cognitive-complexity `_, + `readability-function-size `_, + `readability-identifier-length `_, + `readability-identifier-naming `_, "Yes" + `readability-implicit-bool-conversion `_, "Yes" + `readability-inconsistent-declaration-parameter-name `_, "Yes" + `readability-isolate-declaration `_, "Yes" + `readability-magic-numbers `_, + `readability-make-member-function-const `_, "Yes" + `readability-misleading-indentation `_, + `readability-misplaced-array-index `_, "Yes" + `readability-named-parameter `_, "Yes" + `readability-non-const-parameter `_, "Yes" + `readability-qualified-auto `_, "Yes" + `readability-redundant-access-specifiers `_, "Yes" + `readability-redundant-control-flow `_, "Yes" + `readability-redundant-declaration `_, "Yes" + `readability-redundant-function-ptr-dereference `_, "Yes" + `readability-redundant-member-init `_, "Yes" + `readability-redundant-preprocessor `_, + `readability-redundant-smartptr-get `_, "Yes" + `readability-redundant-string-cstr `_, "Yes" + `readability-redundant-string-init `_, "Yes" + `readability-simplify-boolean-expr `_, "Yes" + `readability-simplify-subscript-expr `_, "Yes" + `readability-static-accessed-through-instance `_, "Yes" + `readability-static-definition-in-anonymous-namespace `_, "Yes" + `readability-string-compare `_, "Yes" + `readability-suspicious-call-argument `_, + `readability-uniqueptr-delete-release `_, "Yes" + `readability-uppercase-literal-suffix `_, "Yes" + `readability-use-anyofallof `_, + `zircon-temporary-objects `_, .. csv-table:: Aliases.. 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 new file mode 100644 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone-standalone-empty.cpp @@ -0,0 +1,79 @@ +// RUN: %check_clang_tidy %s bugprone-standalone-empty %t + +namespace std { + +template +struct vector { + bool empty(); + void clear(); +}; + +template +bool empty(T &&); + +} // namespace std + +namespace absl { +template +struct vector { + bool empty(); + void clear(); +}; + +template +bool empty(T &&); +} // namespace absl + +int test_member_empty() { + std::vector v; + + v.empty(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ignoring the result of 'empty()', did you mean 'clear()'? [bugprone-standalone-empty] + // CHECK-FIXES: {{^ }}v.clear();{{$}} + + absl::vector w; + + w.empty(); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ignoring the result of 'empty()', did you mean 'clear()'? [bugprone-standalone-empty] + // CHECK-FIXES: {{^ }}w.clear();{{$}} +} + +int test_qualified_empty() { + absl::vector v; + + std::empty(v); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ignoring the result of 'std::empty' [bugprone-standalone-empty] + // CHECK-FIXES: {{^ }}v.clear();{{$}} + + absl::empty(v); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ignoring the result of 'absl::empty' [bugprone-standalone-empty] + // CHECK-FIXES: {{^ }}v.clear();{{$}} +} + +int test_unqualified_empty() { + std::vector v; + + empty(v); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ignoring the result of 'std::empty' [bugprone-standalone-empty] + // CHECK-FIXES: {{^ }}v.clear();{{$}} + + absl::vector w; + + empty(w); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ignoring the result of 'absl::empty' [bugprone-standalone-empty] + // CHECK-FIXES: {{^ }}w.clear();{{$}} + + { + using std::empty; + empty(v); + // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: ignoring the result of 'std::empty' [bugprone-standalone-empty] + // CHECK-FIXES: {{^ }} v.clear();{{$}} + } + + { + using absl::empty; + empty(w); + // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: ignoring the result of 'absl::empty' [bugprone-standalone-empty] + // CHECK-FIXES: {{^ }} w.clear();{{$}} + } +}