[clang-tools-extra] eedbe81 - [clang-tidy] Apply cppcoreguidelines-avoid-capture-default-when-capturin-this only to by-value capture default
Carlos Galvez via cfe-commits
cfe-commits at lists.llvm.org
Sat Apr 15 03:10:14 PDT 2023
Author: Carlos Galvez
Date: 2023-04-15T10:10:04Z
New Revision: eedbe81b1c6dfbd85c2a46093e9e862335ad6516
URL: https://github.com/llvm/llvm-project/commit/eedbe81b1c6dfbd85c2a46093e9e862335ad6516
DIFF: https://github.com/llvm/llvm-project/commit/eedbe81b1c6dfbd85c2a46093e9e862335ad6516.diff
LOG: [clang-tidy] Apply cppcoreguidelines-avoid-capture-default-when-capturin-this only to by-value capture default
Since Cpp Core Guidelines have accepted the change in the rules:
https://github.com/isocpp/CppCoreGuidelines/commit/3c90d590e138c3a1e4eb59234e410e00545326de
Also rename the check accordingly.
Differential Revision: https://reviews.llvm.org/D148340
Added:
clang-tools-extra/clang-tidy/cppcoreguidelines/MisleadingCaptureDefaultByValueCheck.cpp
clang-tools-extra/clang-tidy/cppcoreguidelines/MisleadingCaptureDefaultByValueCheck.h
clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/misleading-capture-default-by-value.rst
clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/misleading-capture-default-by-value.cpp
Modified:
clang-tools-extra/clang-tidy/cppcoreguidelines/CMakeLists.txt
clang-tools-extra/clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
clang-tools-extra/docs/ReleaseNotes.rst
clang-tools-extra/docs/clang-tidy/checks/list.rst
Removed:
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
################################################################################
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidCaptureDefaultWhenCapturingThisCheck.h b/clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidCaptureDefaultWhenCapturingThisCheck.h
deleted file mode 100644
index 08bd3ebd3c3ec..0000000000000
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidCaptureDefaultWhenCapturingThisCheck.h
+++ /dev/null
@@ -1,44 +0,0 @@
-//===--- AvoidCaptureDefaultWhenCapturingThisCheck.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_CPPCOREGUIDELINES_AVOIDCAPTUREDEFAULTWHENCAPTURINGTHISCHECK_H
-#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_AVOIDCAPTUREDEFAULTWHENCAPTURINGTHISCHECK_H
-
-#include "../ClangTidyCheck.h"
-
-namespace clang::tidy::cppcoreguidelines {
-
-/// Warns when lambda specify a capture default and capture ``this``. The check
-/// also offers fix-its.
-///
-/// Capture defaults in lambas defined within member functions can be
-/// misleading about whether capturing data member is by value or reference.
-/// For example, [=] will capture local variables by value but member variables
-/// by reference. CppCoreGuideline F.54 suggests to always be explicit
-/// and never specify a capture default when also capturing this.
-///
-/// For the user-facing documentation see:
-/// http://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines/avoid-capture-default-when-capturing-this.html
-class AvoidCaptureDefaultWhenCapturingThisCheck : public ClangTidyCheck {
-public:
- AvoidCaptureDefaultWhenCapturingThisCheck(StringRef Name,
- 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
-
-#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_AVOIDCAPTUREDEFAULTWHENCAPTURINGTHISCHECK_H
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/CMakeLists.txt b/clang-tools-extra/clang-tidy/cppcoreguidelines/CMakeLists.txt
index 0804675c63949..700070d6783aa 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/CMakeLists.txt
@@ -4,7 +4,6 @@ set(LLVM_LINK_COMPONENTS
)
add_clang_library(clangTidyCppCoreGuidelinesModule
- AvoidCaptureDefaultWhenCapturingThisCheck.cpp
AvoidCapturingLambdaCoroutinesCheck.cpp
AvoidConstOrRefDataMembersCheck.cpp
AvoidDoWhileCheck.cpp
@@ -15,6 +14,7 @@ add_clang_library(clangTidyCppCoreGuidelinesModule
InitVariablesCheck.cpp
InterfacesGlobalInitCheck.cpp
MacroUsageCheck.cpp
+ MisleadingCaptureDefaultByValueCheck.cpp
NarrowingConversionsCheck.cpp
NoMallocCheck.cpp
OwningMemoryCheck.cpp
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
index 5bd9648fe8371..ebffc97127cb6 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
@@ -14,7 +14,6 @@
#include "../modernize/AvoidCArraysCheck.h"
#include "../modernize/UseOverrideCheck.h"
#include "../readability/MagicNumbersCheck.h"
-#include "AvoidCaptureDefaultWhenCapturingThisCheck.h"
#include "AvoidCapturingLambdaCoroutinesCheck.h"
#include "AvoidConstOrRefDataMembersCheck.h"
#include "AvoidDoWhileCheck.h"
@@ -24,6 +23,7 @@
#include "InitVariablesCheck.h"
#include "InterfacesGlobalInitCheck.h"
#include "MacroUsageCheck.h"
+#include "MisleadingCaptureDefaultByValueCheck.h"
#include "NarrowingConversionsCheck.h"
#include "NoMallocCheck.h"
#include "OwningMemoryCheck.h"
@@ -50,8 +50,6 @@ namespace cppcoreguidelines {
class CppCoreGuidelinesModule : public ClangTidyModule {
public:
void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
- CheckFactories.registerCheck<AvoidCaptureDefaultWhenCapturingThisCheck>(
- "cppcoreguidelines-avoid-capture-default-when-capturing-this");
CheckFactories.registerCheck<AvoidCapturingLambdaCoroutinesCheck>(
"cppcoreguidelines-avoid-capturing-lambda-coroutines");
CheckFactories.registerCheck<modernize::AvoidCArraysCheck>(
@@ -76,6 +74,8 @@ class CppCoreGuidelinesModule : public ClangTidyModule {
"cppcoreguidelines-interfaces-global-init");
CheckFactories.registerCheck<MacroUsageCheck>(
"cppcoreguidelines-macro-usage");
+ CheckFactories.registerCheck<MisleadingCaptureDefaultByValueCheck>(
+ "cppcoreguidelines-misleading-capture-default-by-value");
CheckFactories.registerCheck<NarrowingConversionsCheck>(
"cppcoreguidelines-narrowing-conversions");
CheckFactories.registerCheck<NoMallocCheck>("cppcoreguidelines-no-malloc");
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidCaptureDefaultWhenCapturingThisCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/MisleadingCaptureDefaultByValueCheck.cpp
similarity index 76%
rename from clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidCaptureDefaultWhenCapturingThisCheck.cpp
rename to clang-tools-extra/clang-tidy/cppcoreguidelines/MisleadingCaptureDefaultByValueCheck.cpp
index 1489ca6c442b1..138f4bf6b19c2 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidCaptureDefaultWhenCapturingThisCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/MisleadingCaptureDefaultByValueCheck.cpp
@@ -1,4 +1,4 @@
-//===--- AvoidCaptureDefaultWhenCapturingThisCheck.cpp - clang-tidy--------===//
+//===--- MisleadingCaptureDefaultByValueCheck.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.
@@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//
-#include "AvoidCaptureDefaultWhenCapturingThisCheck.h"
+#include "MisleadingCaptureDefaultByValueCheck.h"
#include "../utils/LexerUtils.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
@@ -18,20 +18,11 @@ using namespace clang::ast_matchers;
namespace clang::tidy::cppcoreguidelines {
-AvoidCaptureDefaultWhenCapturingThisCheck::
- AvoidCaptureDefaultWhenCapturingThisCheck(StringRef Name,
- ClangTidyContext *Context)
- : ClangTidyCheck(Name, Context),
- IgnoreCaptureDefaultByReference(
- Options.get("IgnoreCaptureDefaultByReference", false)) {}
+MisleadingCaptureDefaultByValueCheck::MisleadingCaptureDefaultByValueCheck(
+ StringRef Name, ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context) {}
-void AvoidCaptureDefaultWhenCapturingThisCheck::storeOptions(
- ClangTidyOptions::OptionMap &Opts) {
- Options.store(Opts, "IgnoreCaptureDefaultByReference",
- IgnoreCaptureDefaultByReference);
-}
-
-void AvoidCaptureDefaultWhenCapturingThisCheck::registerMatchers(
+void MisleadingCaptureDefaultByValueCheck::registerMatchers(
MatchFinder *Finder) {
Finder->addMatcher(lambdaExpr(hasAnyCapture(capturesThis())).bind("lambda"),
this);
@@ -85,23 +76,19 @@ static std::string createReplacementText(const LambdaExpr *Lambda) {
return Replacement;
}
-void AvoidCaptureDefaultWhenCapturingThisCheck::check(
+void MisleadingCaptureDefaultByValueCheck::check(
const MatchFinder::MatchResult &Result) {
const auto *Lambda = Result.Nodes.getNodeAs<LambdaExpr>("lambda");
if (!Lambda)
return;
- if (IgnoreCaptureDefaultByReference &&
- Lambda->getCaptureDefault() == LCD_ByRef)
- return;
-
- if (Lambda->getCaptureDefault() != LCD_None) {
+ if (Lambda->getCaptureDefault() == LCD_ByCopy) {
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")
+ "should not specify a by-value capture default")
<< IsThisImplicitlyCaptured;
std::string ReplacementText = createReplacementText(Lambda);
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/MisleadingCaptureDefaultByValueCheck.h b/clang-tools-extra/clang-tidy/cppcoreguidelines/MisleadingCaptureDefaultByValueCheck.h
new file mode 100644
index 0000000000000..06ffa9a1b148a
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/MisleadingCaptureDefaultByValueCheck.h
@@ -0,0 +1,40 @@
+//===--- MisleadingCaptureDefaultByValueCheck.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_CPPCOREGUIDELINES_MISLEADINGCAPTUREDEFAULTBYVALUECHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_MISLEADINGCAPTUREDEFAULTBYVALUECHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang::tidy::cppcoreguidelines {
+
+/// Warns when lambda specify a by-value capture default and capture ``this``.
+///
+/// By-value capture defaults in lambas defined within member functions can be
+/// misleading about whether capturing data member is by value or reference.
+/// For example, [=] will capture local variables by value but member variables
+/// by reference. CppCoreGuideline F.54 suggests to never use by-value capture
+/// default when capturing this.
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines/misleading-capture-default-by-value.html
+class MisleadingCaptureDefaultByValueCheck : public ClangTidyCheck {
+public:
+ MisleadingCaptureDefaultByValueCheck(StringRef Name,
+ ClangTidyContext *Context);
+ void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+ void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+ bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
+ return LangOpts.CPlusPlus11;
+ }
+};
+
+} // namespace clang::tidy::cppcoreguidelines
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_MISLEADINGCAPTUREDEFAULTBYVALUECHECK_H
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index d7359ec07fece..37024576ee99c 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -112,10 +112,10 @@ New checks
This check relies heavily on, but is not exclusive to, the functions from
the *Annex K. "Bounds-checking interfaces"* of C11.
-- New :doc:`cppcoreguidelines-avoid-capture-default-when-capturing-this
- <clang-tidy/checks/cppcoreguidelines/avoid-capture-default-when-capturing-this>` check.
+- New :doc:`cppcoreguidelines-misleading-capture-default-by-value
+ <clang-tidy/checks/cppcoreguidelines/misleading-capture-default-by-value>` check.
- Warns when lambda specify a capture default and capture ``this``.
+ Warns when lambda specify a by-value capture default and capture ``this``.
- New :doc:`cppcoreguidelines-avoid-capturing-lambda-coroutines
<clang-tidy/checks/cppcoreguidelines/avoid-capturing-lambda-coroutines>` check.
diff --git a/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/avoid-capture-default-when-capturing-this.rst b/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/misleading-capture-default-by-value.rst
similarity index 51%
rename from clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/avoid-capture-default-when-capturing-this.rst
rename to clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/misleading-capture-default-by-value.rst
index d22f5b6f9343b..456b20ece0bba 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/avoid-capture-default-when-capturing-this.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/misleading-capture-default-by-value.rst
@@ -1,11 +1,11 @@
-.. title:: clang-tidy - cppcoreguidelines-avoid-capture-default-when-capturing-this
+.. title:: clang-tidy - cppcoreguidelines-misleading-capture-default-by-value
-cppcoreguidelines-avoid-capture-default-when-capturing-this
-===========================================================
+cppcoreguidelines-misleading-capture-default-by-value
+=====================================================
-Warns when lambda specify a capture default and capture ``this``.
+Warns when lambda specify a by-value capture default and capture ``this``.
-Capture-defaults in member functions can be misleading about
+By-value capture-defaults in member functions can be misleading about
whether data members are captured by value or reference. For example,
specifying the capture default ``[=]`` will still capture data members
by reference.
@@ -40,14 +40,4 @@ Examples:
};
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`.
+`CppCoreGuideline F.54 <https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#f54-when-writing-a-lambda-that-captures-this-or-any-class-data-member-dont-use--default-capture>`_.
diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst
index f44523f77bd6d..c4eaedc253f8e 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/list.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst
@@ -179,7 +179,6 @@ Clang-Tidy Checks
`clang-analyzer-valist.Unterminated <clang-analyzer/valist.Unterminated.html>`_,
`concurrency-mt-unsafe <concurrency/mt-unsafe.html>`_,
`concurrency-thread-canceltype-asynchronous <concurrency/thread-canceltype-asynchronous.html>`_,
- `cppcoreguidelines-avoid-capture-default-when-capturing-this <cppcoreguidelines/avoid-capture-default-when-capturing-this.html>`_, "Yes"
`cppcoreguidelines-avoid-capturing-lambda-coroutines <cppcoreguidelines/avoid-capturing-lambda-coroutines.html>`_,
`cppcoreguidelines-avoid-const-or-ref-data-members <cppcoreguidelines/avoid-const-or-ref-data-members.html>`_,
`cppcoreguidelines-avoid-do-while <cppcoreguidelines/avoid-do-while.html>`_,
@@ -189,6 +188,7 @@ Clang-Tidy Checks
`cppcoreguidelines-init-variables <cppcoreguidelines/init-variables.html>`_, "Yes"
`cppcoreguidelines-interfaces-global-init <cppcoreguidelines/interfaces-global-init.html>`_,
`cppcoreguidelines-macro-usage <cppcoreguidelines/macro-usage.html>`_,
+ `cppcoreguidelines-misleading-capture-default-by-value <cppcoreguidelines/misleading-capture-default-by-value.html>`_, "Yes"
`cppcoreguidelines-narrowing-conversions <cppcoreguidelines/narrowing-conversions.html>`_,
`cppcoreguidelines-no-malloc <cppcoreguidelines/no-malloc.html>`_,
`cppcoreguidelines-owning-memory <cppcoreguidelines/owning-memory.html>`_,
diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-capture-default-when-capturing-this.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/misleading-capture-default-by-value.cpp
similarity index 55%
rename from clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-capture-default-when-capturing-this.cpp
rename to clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/misleading-capture-default-by-value.cpp
index 3ae8cc47e0147..c9e8bbc22d4c0 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-capture-default-when-capturing-this.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/misleading-capture-default-by-value.cpp
@@ -1,7 +1,4 @@
-// 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}]}"
+// RUN: %check_clang_tidy -std=c++11-or-later %s cppcoreguidelines-misleading-capture-default-by-value %t
struct Obj {
void lambdas_that_warn_default_capture_copy() {
@@ -9,73 +6,60 @@ struct Obj {
int local2{};
auto explicit_this_capture = [=, this]() { };
- // CHECK-MESSAGES: :[[@LINE-1]]:35: warning: lambdas that capture 'this' should not specify a capture default [cppcoreguidelines-avoid-capture-default-when-capturing-this]
+ // CHECK-MESSAGES: :[[@LINE-1]]:35: warning: lambdas that capture 'this' should not specify a by-value capture default [cppcoreguidelines-misleading-capture-default-by-value]
// CHECK-FIXES: auto explicit_this_capture = [this]() { };
auto explicit_this_capture_locals1 = [=, this]() { return (local+x) > 10; };
- // CHECK-MESSAGES: :[[@LINE-1]]:43: warning: lambdas that capture 'this' should not specify a capture default [cppcoreguidelines-avoid-capture-default-when-capturing-this]
+ // CHECK-MESSAGES: :[[@LINE-1]]:43: warning: lambdas that capture 'this' should not specify a by-value capture default [cppcoreguidelines-misleading-capture-default-by-value]
// CHECK-FIXES: auto explicit_this_capture_locals1 = [local, this]() { return (local+x) > 10; };
auto explicit_this_capture_locals2 = [=, this]() { return (local+local2) > 10; };
- // CHECK-MESSAGES: :[[@LINE-1]]:43: warning: lambdas that capture 'this' should not specify a capture default [cppcoreguidelines-avoid-capture-default-when-capturing-this]
+ // CHECK-MESSAGES: :[[@LINE-1]]:43: warning: lambdas that capture 'this' should not specify a by-value capture default [cppcoreguidelines-misleading-capture-default-by-value]
// CHECK-FIXES: auto explicit_this_capture_locals2 = [local, local2, this]() { return (local+local2) > 10; };
auto explicit_this_capture_local_ref = [=, this, &local]() { 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-MESSAGES: :[[@LINE-1]]:45: warning: lambdas that capture 'this' should not specify a by-value capture default [cppcoreguidelines-misleading-capture-default-by-value]
// CHECK-FIXES: auto explicit_this_capture_local_ref = [this, &local]() { return (local+x) > 10; };
auto explicit_this_capture_local_ref2 = [=, &local, this]() { return (local+x) > 10; };
- // CHECK-MESSAGES: :[[@LINE-1]]:46: warning: lambdas that capture 'this' should not specify a capture default [cppcoreguidelines-avoid-capture-default-when-capturing-this]
+ // CHECK-MESSAGES: :[[@LINE-1]]:46: warning: lambdas that capture 'this' should not specify a by-value capture default [cppcoreguidelines-misleading-capture-default-by-value]
// CHECK-FIXES: auto explicit_this_capture_local_ref2 = [&local, this]() { return (local+x) > 10; };
auto explicit_this_capture_local_ref3 = [=, &local, this, &local2]() { return (local+x) > 10; };
- // CHECK-MESSAGES: :[[@LINE-1]]:46: warning: lambdas that capture 'this' should not specify a capture default [cppcoreguidelines-avoid-capture-default-when-capturing-this]
+ // CHECK-MESSAGES: :[[@LINE-1]]:46: warning: lambdas that capture 'this' should not specify a by-value capture default [cppcoreguidelines-misleading-capture-default-by-value]
// CHECK-FIXES: auto explicit_this_capture_local_ref3 = [&local, this, &local2]() { return (local+x) > 10; };
auto explicit_this_capture_local_ref4 = [=, &local, &local2, this]() { return (local+x) > 10; };
- // CHECK-MESSAGES: :[[@LINE-1]]:46: warning: lambdas that capture 'this' should not specify a capture default [cppcoreguidelines-avoid-capture-default-when-capturing-this]
+ // CHECK-MESSAGES: :[[@LINE-1]]:46: warning: lambdas that capture 'this' should not specify a by-value capture default [cppcoreguidelines-misleading-capture-default-by-value]
// CHECK-FIXES: auto explicit_this_capture_local_ref4 = [&local, &local2, this]() { return (local+x) > 10; };
auto explicit_this_capture_local_ref_extra_whitespace = [=, & local, &local2, this]() { return (local+x) > 10; };
- // CHECK-MESSAGES: :[[@LINE-1]]:62: warning: lambdas that capture 'this' should not specify a capture default [cppcoreguidelines-avoid-capture-default-when-capturing-this]
+ // CHECK-MESSAGES: :[[@LINE-1]]:62: warning: lambdas that capture 'this' should not specify a by-value capture default [cppcoreguidelines-misleading-capture-default-by-value]
// CHECK-FIXES: auto explicit_this_capture_local_ref_extra_whitespace = [& local, &local2, this]() { return (local+x) > 10; };
auto explicit_this_capture_local_ref_with_comment = [=, & /* byref */ local, &local2, this]() { return (local+x) > 10; };
- // CHECK-MESSAGES: :[[@LINE-1]]:58: warning: lambdas that capture 'this' should not specify a capture default [cppcoreguidelines-avoid-capture-default-when-capturing-this]
+ // CHECK-MESSAGES: :[[@LINE-1]]:58: warning: lambdas that capture 'this' should not specify a by-value capture default [cppcoreguidelines-misleading-capture-default-by-value]
// CHECK-FIXES: auto explicit_this_capture_local_ref_with_comment = [& /* byref */ local, &local2, this]() { return (local+x) > 10; };
auto implicit_this_capture = [=]() { return x > 10; };
- // CHECK-MESSAGES: :[[@LINE-1]]:35: warning: lambdas that implicitly capture 'this' should not specify a capture default [cppcoreguidelines-avoid-capture-default-when-capturing-this]
+ // CHECK-MESSAGES: :[[@LINE-1]]:35: warning: lambdas that implicitly capture 'this' should not specify a by-value capture default [cppcoreguidelines-misleading-capture-default-by-value]
// CHECK-FIXES: auto implicit_this_capture = [this]() { return x > 10; };
auto implicit_this_capture_local = [=]() { return (local+x) > 10; };
- // CHECK-MESSAGES: :[[@LINE-1]]:41: warning: lambdas that implicitly capture 'this' should not specify a capture default [cppcoreguidelines-avoid-capture-default-when-capturing-this]
+ // CHECK-MESSAGES: :[[@LINE-1]]:41: warning: lambdas that implicitly capture 'this' should not specify a by-value capture default [cppcoreguidelines-misleading-capture-default-by-value]
// CHECK-FIXES: auto implicit_this_capture_local = [local, this]() { return (local+x) > 10; };
}
- void lambdas_that_warn_default_capture_ref() {
+ void lambdas_that_dont_warn_default_capture_ref() {
int local{};
int local2{};
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-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-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-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-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() {
More information about the cfe-commits
mailing list