This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG4d4c0f973460: [clang-tidy] Add option to ignore capture
default by reference in… (authored by carlosgalvezp).
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D147062/new/
https://reviews.llvm.org/D147062
Files:
clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidCaptureDefaultWhenCapturingThisCheck.cpp
clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidCaptureDefaultWhenCapturingThisCheck.h
clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/avoid-capture-default-when-capturing-this.rst
clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-capture-default-when-capturing-this.cpp
Index: clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-capture-default-when-capturing-this.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-capture-default-when-capturing-this.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-capture-default-when-capturing-this.cpp
@@ -1,4 +1,7 @@
-// RUN: %check_clang_tidy -std=c++11-or-later %s cppcoreguidelines-avoid-capture-default-when-capturing-this %t
+// RUN: %check_clang_tidy -std=c++11-or-later %s cppcoreguidelines-avoid-capture-default-when-capturing-this %t \
+// RUN: -check-suffixes=,DEFAULT
+// RUN: %check_clang_tidy -std=c++11-or-later %s cppcoreguidelines-avoid-capture-default-when-capturing-this %t \
+// RUN: -config="{CheckOptions: [{key: cppcoreguidelines-avoid-capture-default-when-capturing-this.IgnoreCaptureDefaultByReference, value: true}]}"
struct Obj {
void lambdas_that_warn_default_capture_copy() {
@@ -55,24 +58,24 @@
int local2{};
auto ref_explicit_this_capture = [&, this]() { };
- // CHECK-MESSAGES: :[[@LINE-1]]:39: warning: lambdas that capture 'this' should not specify a capture default [cppcoreguidelines-avoid-capture-default-when-capturing-this]
- // CHECK-FIXES: auto ref_explicit_this_capture = [this]() { };
+ // CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:39: warning: lambdas that capture 'this' should not specify a capture default [cppcoreguidelines-avoid-capture-default-when-capturing-this]
+ // CHECK-FIXES-DEFAULT: auto ref_explicit_this_capture = [this]() { };
auto ref_explicit_this_capture_local = [&, this]() { return (local+x) > 10; };
- // CHECK-MESSAGES: :[[@LINE-1]]:45: warning: lambdas that capture 'this' should not specify a capture default [cppcoreguidelines-avoid-capture-default-when-capturing-this]
- // CHECK-FIXES: auto ref_explicit_this_capture_local = [&local, this]() { return (local+x) > 10; };
+ // CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:45: warning: lambdas that capture 'this' should not specify a capture default [cppcoreguidelines-avoid-capture-default-when-capturing-this]
+ // CHECK-FIXES-DEFAULT: auto ref_explicit_this_capture_local = [&local, this]() { return (local+x) > 10; };
auto ref_implicit_this_capture = [&]() { return x > 10; };
- // CHECK-MESSAGES: :[[@LINE-1]]:39: warning: lambdas that implicitly capture 'this' should not specify a capture default [cppcoreguidelines-avoid-capture-default-when-capturing-this]
- // CHECK-FIXES: auto ref_implicit_this_capture = [this]() { return x > 10; };
+ // CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:39: warning: lambdas that implicitly capture 'this' should not specify a capture default [cppcoreguidelines-avoid-capture-default-when-capturing-this]
+ // CHECK-FIXES-DEFAULT: auto ref_implicit_this_capture = [this]() { return x > 10; };
auto ref_implicit_this_capture_local = [&]() { return (local+x) > 10; };
- // CHECK-MESSAGES: :[[@LINE-1]]:45: warning: lambdas that implicitly capture 'this' should not specify a capture default [cppcoreguidelines-avoid-capture-default-when-capturing-this]
- // CHECK-FIXES: auto ref_implicit_this_capture_local = [&local, this]() { return (local+x) > 10; };
+ // CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:45: warning: lambdas that implicitly capture 'this' should not specify a capture default [cppcoreguidelines-avoid-capture-default-when-capturing-this]
+ // CHECK-FIXES-DEFAULT: auto ref_implicit_this_capture_local = [&local, this]() { return (local+x) > 10; };
auto ref_implicit_this_capture_locals = [&]() { return (local+local2+x) > 10; };
- // CHECK-MESSAGES: :[[@LINE-1]]:46: warning: lambdas that implicitly capture 'this' should not specify a capture default [cppcoreguidelines-avoid-capture-default-when-capturing-this]
- // CHECK-FIXES: auto ref_implicit_this_capture_locals = [&local, &local2, this]() { return (local+local2+x) > 10; };
+ // CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:46: warning: lambdas that implicitly capture 'this' should not specify a capture default [cppcoreguidelines-avoid-capture-default-when-capturing-this]
+ // CHECK-FIXES-DEFAULT: auto ref_implicit_this_capture_locals = [&local, &local2, this]() { return (local+local2+x) > 10; };
}
void lambdas_that_dont_warn() {
Index: clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/avoid-capture-default-when-capturing-this.rst
===================================================================
--- clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/avoid-capture-default-when-capturing-this.rst
+++ clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/avoid-capture-default-when-capturing-this.rst
@@ -41,3 +41,13 @@
This check implements
`CppCoreGuideline F.54 <http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#f54-if-you-capture-this-capture-all-variables-explicitly-no-default-capture>`_.
+
+
+Options
+-------
+
+.. option:: IgnoreCaptureDefaultByReference
+
+ Do not warn when using capture default by reference. In this case, there is no
+ confusion as to whether variables are captured by value or reference.
+ Defaults to `false`.
Index: clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidCaptureDefaultWhenCapturingThisCheck.h
===================================================================
--- clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidCaptureDefaultWhenCapturingThisCheck.h
+++ clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidCaptureDefaultWhenCapturingThisCheck.h
@@ -27,13 +27,16 @@
class AvoidCaptureDefaultWhenCapturingThisCheck : public ClangTidyCheck {
public:
AvoidCaptureDefaultWhenCapturingThisCheck(StringRef Name,
- ClangTidyContext *Context)
- : ClangTidyCheck(Name, Context) {}
+ ClangTidyContext *Context);
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+ void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
return LangOpts.CPlusPlus11;
}
+
+private:
+ bool IgnoreCaptureDefaultByReference;
};
} // namespace clang::tidy::cppcoreguidelines
Index: clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidCaptureDefaultWhenCapturingThisCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidCaptureDefaultWhenCapturingThisCheck.cpp
+++ clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidCaptureDefaultWhenCapturingThisCheck.cpp
@@ -18,6 +18,19 @@
namespace clang::tidy::cppcoreguidelines {
+AvoidCaptureDefaultWhenCapturingThisCheck::
+ AvoidCaptureDefaultWhenCapturingThisCheck(StringRef Name,
+ ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context),
+ IgnoreCaptureDefaultByReference(
+ Options.get("IgnoreCaptureDefaultByReference", false)) {}
+
+void AvoidCaptureDefaultWhenCapturingThisCheck::storeOptions(
+ ClangTidyOptions::OptionMap &Opts) {
+ Options.store(Opts, "IgnoreCaptureDefaultByReference",
+ IgnoreCaptureDefaultByReference);
+}
+
void AvoidCaptureDefaultWhenCapturingThisCheck::registerMatchers(
MatchFinder *Finder) {
Finder->addMatcher(lambdaExpr(hasAnyCapture(capturesThis())).bind("lambda"),
@@ -74,24 +87,30 @@
void AvoidCaptureDefaultWhenCapturingThisCheck::check(
const MatchFinder::MatchResult &Result) {
- if (const auto *Lambda = Result.Nodes.getNodeAs<LambdaExpr>("lambda")) {
- if (Lambda->getCaptureDefault() != LCD_None) {
- bool IsThisImplicitlyCaptured = std::any_of(
- Lambda->implicit_capture_begin(), Lambda->implicit_capture_end(),
- [](const LambdaCapture &Capture) { return Capture.capturesThis(); });
- auto Diag = diag(Lambda->getCaptureDefaultLoc(),
- "lambdas that %select{|implicitly }0capture 'this' "
- "should not specify a capture default")
- << IsThisImplicitlyCaptured;
-
- std::string ReplacementText = createReplacementText(Lambda);
- SourceLocation DefaultCaptureEnd =
- findDefaultCaptureEnd(Lambda, *Result.Context);
- Diag << FixItHint::CreateReplacement(
- CharSourceRange::getCharRange(Lambda->getCaptureDefaultLoc(),
- DefaultCaptureEnd),
- ReplacementText);
- }
+ const auto *Lambda = Result.Nodes.getNodeAs<LambdaExpr>("lambda");
+ if (!Lambda)
+ return;
+
+ if (IgnoreCaptureDefaultByReference &&
+ Lambda->getCaptureDefault() == LCD_ByRef)
+ return;
+
+ if (Lambda->getCaptureDefault() != LCD_None) {
+ bool IsThisImplicitlyCaptured = std::any_of(
+ Lambda->implicit_capture_begin(), Lambda->implicit_capture_end(),
+ [](const LambdaCapture &Capture) { return Capture.capturesThis(); });
+ auto Diag = diag(Lambda->getCaptureDefaultLoc(),
+ "lambdas that %select{|implicitly }0capture 'this' "
+ "should not specify a capture default")
+ << IsThisImplicitlyCaptured;
+
+ std::string ReplacementText = createReplacementText(Lambda);
+ SourceLocation DefaultCaptureEnd =
+ findDefaultCaptureEnd(Lambda, *Result.Context);
+ Diag << FixItHint::CreateReplacement(
+ CharSourceRange::getCharRange(Lambda->getCaptureDefaultLoc(),
+ DefaultCaptureEnd),
+ ReplacementText);
}
}
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits