While testing clang-tidy for D59251: [Documentation] Proposal for plan to change variable names I found that a lambda capture could get incorrectly get transformed by readability-identifier-naming when run with -fix
The following code:
bool foo() { bool Columns=false; auto ptr=[&] { return Columns; }(); return true; }
would get transformed to (replace [&] with [columns]
bool foo() { bool columns=false; auto ptr=[columns] { return columns; }(); return true; }
This is caused by the presence of a declrefexpr in the LambdaExpr, seeming having the location of the [&] (line 3 column 13), but also having the Name "Columns"
-DeclRefExpr <col:13> 'bool' lvalue Var 0x55f0145f1c80 'Columns' 'bool' |
LambdaExpr <line:3:12, line:5:9> '(lambda at line:3:12)' |-CXXRecordDecl <line:3:12> col:12 implicit class definition | |-DefinitionData lambda pass_in_registers trivially_copyable can_const_default_init | | |-DefaultConstructor | | |-CopyConstructor simple trivial has_const_param needs_implicit implicit_has_const_param | | |-MoveConstructor exists simple trivial needs_implicit | | |-CopyAssignment trivial has_const_param needs_implicit implicit_has_const_param | | |-MoveAssignment | | `-Destructor simple irrelevant trivial | |-FieldDecl <line:4:18> col:18 implicit 'bool &' | |-CXXMethodDecl <line:3:14, line:5:9> line:3:12 used operator() 'auto () const -> bool' inline | | `-CompoundStmt <col:16, line:5:9> | | `-ReturnStmt <line:4:11, col:18> | | `-ImplicitCastExpr <col:18> 'bool' <LValueToRValue> | | `-DeclRefExpr <col:18> 'bool' lvalue Var 0x55f0145f1c80 'Columns' 'bool' | `-CXXDestructorDecl <line:3:12> col:12 implicit referenced ~ 'void () noexcept' inline default trivial |-DeclRefExpr <col:13> 'bool' lvalue Var 0x55f0145f1c80 'Columns' 'bool' `-CompoundStmt <col:16, line:5:9> `-ReturnStmt <line:4:11, col:18> `-ImplicitCastExpr <col:18> 'bool' <LValueToRValue> `-DeclRefExpr <col:18> 'bool' lvalue Var 0x55f0145f1c80 'Columns' 'bool'
The following code tries to detect the DeclRefExpr in the LambdaExpr, and not add this to the usage map
This issue is logged as https://bugs.llvm.org/show_bug.cgi?id=41119
I have retested this against lib/Clang/Format and this issue is resolved.
// Don't use this Format, if the difference between the longest and shortest // element in a column exceeds a threshold to avoid excessive spaces. if ([&] { for (unsigned i = 0; i < columns - 1; ++i) if (format.ColumnSizes[i] - minSizeInColumn[i] > 10) return true; return false; }()) continue;
Please use early return: