summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp6
-rw-r--r--clang-tools-extra/test/clang-tidy/modernize-loop-convert-basic.cpp12
2 files changed, 18 insertions, 0 deletions
diff --git a/clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp b/clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp
index 2c623727119..054e5ce94d9 100644
--- a/clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp
@@ -391,6 +391,12 @@ static bool containerIsConst(const Expr *ContainerExpr, bool Dereference) {
return false;
CType = CType->getPointeeType();
}
+ // If VDec is a reference to a container, Dereference is false,
+ // but we still need to check the const-ness of the underlying container
+ // type.
+ if (const auto &RT = CType->getAs<ReferenceType>()) {
+ CType = RT->getPointeeType();
+ }
return CType.isConstQualified();
}
return false;
diff --git a/clang-tools-extra/test/clang-tidy/modernize-loop-convert-basic.cpp b/clang-tools-extra/test/clang-tidy/modernize-loop-convert-basic.cpp
index dd16cb26f62..d3908b9126b 100644
--- a/clang-tools-extra/test/clang-tidy/modernize-loop-convert-basic.cpp
+++ b/clang-tools-extra/test/clang-tidy/modernize-loop-convert-basic.cpp
@@ -544,6 +544,18 @@ void constness() {
// CHECK-FIXES-NEXT: sum += elem + 2;
}
+void ConstRef(const dependent<int>& ConstVRef) {
+ int sum = 0;
+ // FIXME: This does not work with size_t (probably due to the implementation
+ // of dependent); make dependent work exactly like a std container type.
+ for (int i = 0; i < ConstVRef.size(); ++i) {
+ sum += ConstVRef[i];
+ }
+ // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use range-based for loop instead
+ // CHECK-FIXES: for (const auto & elem : ConstVRef)
+ // CHECK-FIXES-NEXT: sum += elem;
+}
+
// Check for loops that don't mention containers.
void noContainer() {
for (auto i = 0; i < v.size(); ++i) {
OpenPOWER on IntegriCloud