diff options
| author | Matthias Gehre <M.Gehre@gmx.de> | 2016-07-20 12:32:06 +0000 |
|---|---|---|
| committer | Matthias Gehre <M.Gehre@gmx.de> | 2016-07-20 12:32:06 +0000 |
| commit | 056237a457dc3cdc88d0e282663400871f8e96dd (patch) | |
| tree | 423b37dfcb53dfdd63968790007b5c54a88b6b37 | |
| parent | 5d8f071a0773635df1cc2d9b799198a350a732e2 (diff) | |
| download | bcm5719-llvm-056237a457dc3cdc88d0e282663400871f8e96dd.tar.gz bcm5719-llvm-056237a457dc3cdc88d0e282663400871f8e96dd.zip | |
clang-tidy modernize-loop-convert: preserve type of alias declaration (bug 28341)
Summary:
Previoly, the added test failed with the fillowing fixit:
char v[5];
- for(size_t i = 0; i < 5; ++i)
+ for(char value : v)
{
- unsigned char value = v[i];
if (value > 127)
i.e. the variable 'value' changes from unsigned char to signed char. And
thus the following 'if' does not work anymore.
With this commit, the fixit is changed to:
char v[5];
- for(size_t i = 0; i < 5; ++i)
+ for(unsigned char value : v)
{
- unsigned char value = v[i];
if (value > 127)
Reviewers: alexfh, klimek
Subscribers: cfe-commits
Differential Revision: http://reviews.llvm.org/D22069
llvm-svn: 276111
| -rw-r--r-- | clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp | 12 | ||||
| -rw-r--r-- | clang-tools-extra/test/clang-tidy/modernize-loop-convert-extra.cpp | 12 |
2 files changed, 23 insertions, 1 deletions
diff --git a/clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp b/clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp index a1c5009c42d..4736bce32dd 100644 --- a/clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp @@ -517,7 +517,17 @@ void LoopConvertCheck::doConversion( if (VarNameFromAlias) { const auto *AliasVar = cast<VarDecl>(AliasDecl->getSingleDecl()); VarName = AliasVar->getName().str(); - AliasVarIsRef = AliasVar->getType()->isReferenceType(); + + // Use the type of the alias if it's not the same + QualType AliasVarType = AliasVar->getType(); + assert(!AliasVarType.isNull() && "Type in VarDecl is null"); + if (AliasVarType->isReferenceType()) { + AliasVarType = AliasVarType.getNonReferenceType(); + AliasVarIsRef = true; + } + if (Descriptor.ElemType.isNull() || + !Context->hasSameUnqualifiedType(AliasVarType, Descriptor.ElemType)) + Descriptor.ElemType = AliasVarType; // We keep along the entire DeclStmt to keep the correct range here. SourceRange ReplaceRange = AliasDecl->getSourceRange(); diff --git a/clang-tools-extra/test/clang-tidy/modernize-loop-convert-extra.cpp b/clang-tools-extra/test/clang-tidy/modernize-loop-convert-extra.cpp index 7312b48b9b3..b46ff25c2a3 100644 --- a/clang-tools-extra/test/clang-tidy/modernize-loop-convert-extra.cpp +++ b/clang-tools-extra/test/clang-tidy/modernize-loop-convert-extra.cpp @@ -1060,3 +1060,15 @@ void f() { } } // namespace InitLists + +void bug28341() { + char v[5]; + for(int i = 0; i < 5; ++i) { + unsigned char value = v[i]; + if (value > 127) + ; + // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop instead + // CHECK-FIXES: for(unsigned char value : v) + // CHECK-FIXES-NEXT: if (value > 127) + } +} |

