From ef731faa4784af8323b8c747730bcf9674c70026 Mon Sep 17 00:00:00 2001 From: Jarek Kobus Date: Tue, 21 Feb 2023 08:23:18 +0100 Subject: [PATCH] Coding style: Avoid empty round brackets in lambdas Remove outdated rules. Use QList instead of QVector. Change-Id: I11ea6be09080ddccfdcfc3d09f79cbeff448659e Reviewed-by: Leena Miettinen Reviewed-by: hjk --- doc/qtcreatordev/src/coding-style.qdoc | 59 ++++++++++---------------- 1 file changed, 22 insertions(+), 37 deletions(-) diff --git a/doc/qtcreatordev/src/coding-style.qdoc b/doc/qtcreatordev/src/coding-style.qdoc index 1aed25bc3ef..02636e3ab59 100644 --- a/doc/qtcreatordev/src/coding-style.qdoc +++ b/doc/qtcreatordev/src/coding-style.qdoc @@ -741,43 +741,28 @@ \section3 Lambdas - When using lambdas, note the following: - - \list - \li You do not have to explicitly specify the return type. If you are not using one - of the previously mentioned compilers, do note that this is a C++14 feature and you - might need to enable C++14 support in your compiler. - \code - []() { - Foo *foo = activeFoo(); - return foo ? foo->displayName() : QString(); - }); - \endcode - - \li If you use static functions from the class that the lambda is located in, you have to - explicitly capture \c this. Otherwise it does not compile with g++ 4.7 and earlier. - \code - void Foo::something() - { - ... - [this]() { Foo::someStaticFunction(); } - ... - } - - -NOT- - - void Foo::something() - { - ... - []() { Foo::someStaticFunction(); } - ... - } - \endcode - \endlist - Format the lambda according to the following rules: \list + \li When the lambda neither takes arguments nor specifies a return type, + drop round brackets. + \code + [] { ... lambda body ... } + + -NOT- + + []() { ... lambda body ... } + \endcode + + \li Glue square brackets with round brackets when defining a lambda. + \code + [](int a) { ... lambda body ... } + + -NOT- + + [] (int a) { ... lambda body ... } + \endcode + \li Place the capture-list, parameter list, return type, and opening brace on the first line, the body indented on the following lines, and the closing brace on a new line. \code @@ -795,7 +780,7 @@ \li Place a closing parenthesis and semicolon of an enclosing function call on the same line as the closing brace of the lambda. \code - foo([]() { + foo([] { something(); }); \endcode @@ -866,7 +851,7 @@ Use initializer lists to initialize containers, for example: \code - const QVector values = {1, 2, 3, 4, 5}; + const QList values = {1, 2, 3, 4, 5}; \endcode \section3 Initialization with Curly Brackets @@ -878,7 +863,7 @@ class Values // the following code is quite useful for test fixtures { float floatValue = 4; // prefer that for simple types - QVector values = {1, 2, 3, 4, integerValue}; // prefer that syntax for initializer lists + QList values = {1, 2, 3, 4, integerValue}; // prefer that syntax for initializer lists SomeValues someValues{"One", 2, 3.4}; // not an initializer_list SomeValues &someValuesReference = someValues; ComplexType complexType{values, otherValues} // constructor call