llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.25k stars 11.67k forks source link

[clang-tidy] readability-identifier-naming incorrectly fixes lambda capture #40464

Open mydeveloperday opened 5 years ago

mydeveloperday commented 5 years ago
Bugzilla Link 41119
Version unspecified
OS Windows NT

Extended Description

Whilst looking at how clang-tidy might be able to handle such large scale variable renaming for something like https://reviews.llvm.org/D59251 I decided to run clang-tidy over the clang code base (starting in lib/Format)

clang-tidy mutated the code so it wouldn't compile in FormatToken.cpp line 275 which is a Lambda, breaking this down into the following code..


bool foo() { for (unsigned Columns = 1; Columns <= 10; ++Columns) { if ([&] { for (unsigned i = 0; i < Columns - 1; ++i) return false; }()) continue; } return true; }

Using the following .clang-tidy file with

$ clang-tidy -version LLVM (http://llvm.org/): LLVM version 9.0.0-r355397 Optimized build with assertions. Default target: x86_64-pc-windows-msvc Host CPU: skylake


Checks: '-*,readability-identifier-naming' CheckOptions:

And running with -fix, gives


c:\Repos\tidy_tests\test.cxx:3:17: warning: invalid case style for variable 'Columns' [readability-identifier-naming] for (unsigned Columns = 1; Columns <= 10; ++Columns) { ^~~ ~~~ ~~~ columns columns columns c:\Repos\tidy_tests\test.cxx:3:17: note: FIX-IT applied suggested code changes c:\Repos\tidy_tests\test.cxx:3:30: note: FIX-IT applied suggested code changes for (unsigned Columns = 1; Columns <= 10; ++Columns) { ^ c:\Repos\tidy_tests\test.cxx:3:47: note: FIX-IT applied suggested code changes for (unsigned Columns = 1; Columns <= 10; ++Columns) { ^ c:\Repos\tidy_tests\test.cxx:4:10: note: FIX-IT applied suggested code changes if ([&] { ^ c:\Repos\tidy_tests\test.cxx:5:36: note: FIX-IT applied suggested code changes for (unsigned i = 0; i < Columns - 1; ++i) ^ clang-tidy applied 5 of 5 suggested fixes. Suppressed 1 warnings (1 with check filters).


We get an incorrect fixed code (notice the capture)


bool foo() { for (unsigned columns = 1; columns <= 10; ++columns) { if ([columns] { for (unsigned i = 0; i < columns - 1; ++i) return false; }()) continue; } return true; }

llvmbot commented 2 years ago

mentioned in issue llvm/llvm-bugzilla-archive#47802

llvmbot commented 3 years ago

Bug llvm/llvm-bugzilla-archive#47802 has been marked as a duplicate of this bug.

llvmbot commented 3 years ago

Decided to have another crack at addressing this, WIP but results seem good https://reviews.llvm.org/D97653

mydeveloperday commented 5 years ago

WIP https://reviews.llvm.org/D59540