diff --git a/clang-tools-extra/clang-tidy/bugprone/BranchCloneCheck.h b/clang-tools-extra/clang-tidy/bugprone/BranchCloneCheck.h --- a/clang-tools-extra/clang-tidy/bugprone/BranchCloneCheck.h +++ b/clang-tools-extra/clang-tidy/bugprone/BranchCloneCheck.h @@ -27,6 +27,9 @@ : ClangTidyCheck(Name, Context) {} void registerMatchers(ast_matchers::MatchFinder *Finder) override; void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + std::optional getCheckTraversalKind() const override { + return TK_IgnoreUnlessSpelledInSource; + } }; } // namespace clang::tidy::bugprone 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 @@ -201,6 +201,11 @@ Changes in existing checks ^^^^^^^^^^^^^^^^^^^^^^^^^^ + +- Fixed false-positive in :doc:`bugprone-branch-clone + ` check by ignoring auto-generated + code, template instances, and implicit code patterns. + - Improved :doc:`readability-redundant-string-cstr ` check to recognise unnecessary ``std::string::c_str()`` and ``std::string::data()`` calls in diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/branch-clone.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/branch-clone.cpp --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/branch-clone.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/branch-clone.cpp @@ -1024,3 +1024,48 @@ SEMICOLON_CASE_COLON(3); } } + +namespace PR62693 { + class Object { + public: + template + bool ConvertableTo() const; + + template + void Handle(); + }; + + template + void update(Object &a) { + if (a.ConvertableTo()) { + a.Handle(); + } else { + a.Handle(); + } + } + + template + void update2(Object &a) { + if (a.ConvertableTo()) { + a.Handle(); + } else { + a.Handle(); + } + } + + void foo(Object &a) { + update(a); + update2(a); + } + + template + int branch_clone_in_template(T t) { + // CHECK-MESSAGES: :[[@LINE+2]]:5: warning: if with identical then and else branches [bugprone-branch-clone] + // CHECK-MESSAGES: :[[@LINE+3]]:7: note: else branch starts here + if (t) { + return 42; + } else { + return 42; + } + } +}