@@ -405,7 +405,7 @@ static bool containerIsConst(const Expr *ContainerExpr, bool Dereference) {
405
405
406
406
LoopConvertCheck::RangeDescriptor::RangeDescriptor ()
407
407
: ContainerNeedsDereference(false ), DerefByConstRef(false ),
408
- DerefByValue(false ), IsTriviallyCopyable( false ) {}
408
+ DerefByValue(false ) {}
409
409
410
410
LoopConvertCheck::LoopConvertCheck (StringRef Name, ClangTidyContext *Context)
411
411
: ClangTidyCheck(Name, Context), TUInfo(new TUTrackingInfo),
@@ -554,30 +554,33 @@ void LoopConvertCheck::doConversion(
554
554
// Now, we need to construct the new range expression.
555
555
SourceRange ParenRange (Loop->getLParenLoc (), Loop->getRParenLoc ());
556
556
557
- QualType AutoType = Context->getAutoDeductType ();
557
+ QualType Type = Context->getAutoDeductType ();
558
+ if (!Descriptor.ElemType .isNull () && Descriptor.ElemType ->isFundamentalType ())
559
+ Type = Descriptor.ElemType .getUnqualifiedType ();
558
560
559
561
// If the new variable name is from the aliased variable, then the reference
560
562
// type for the new variable should only be used if the aliased variable was
561
563
// declared as a reference.
564
+ bool IsTriviallyCopyable =
565
+ !Descriptor.ElemType .isNull () &&
566
+ Descriptor.ElemType .isTriviallyCopyableType (*Context);
562
567
bool UseCopy =
563
- CanCopy &&
564
- ((VarNameFromAlias && !AliasVarIsRef) ||
565
- (Descriptor.DerefByConstRef && Descriptor.IsTriviallyCopyable ));
568
+ CanCopy && ((VarNameFromAlias && !AliasVarIsRef) ||
569
+ (Descriptor.DerefByConstRef && IsTriviallyCopyable));
566
570
567
571
if (!UseCopy) {
568
572
if (Descriptor.DerefByConstRef ) {
569
- AutoType =
570
- Context->getLValueReferenceType (Context->getConstType (AutoType));
573
+ Type = Context->getLValueReferenceType (Context->getConstType (Type));
571
574
} else if (Descriptor.DerefByValue ) {
572
- if (!Descriptor. IsTriviallyCopyable )
573
- AutoType = Context->getRValueReferenceType (AutoType );
575
+ if (!IsTriviallyCopyable)
576
+ Type = Context->getRValueReferenceType (Type );
574
577
} else {
575
- AutoType = Context->getLValueReferenceType (AutoType );
578
+ Type = Context->getLValueReferenceType (Type );
576
579
}
577
580
}
578
581
579
582
StringRef MaybeDereference = Descriptor.ContainerNeedsDereference ? " *" : " " ;
580
- std::string TypeString = AutoType .getAsString ();
583
+ std::string TypeString = Type .getAsString (getLangOpts () );
581
584
std::string Range = (" (" + TypeString + " " + VarName + " : " +
582
585
MaybeDereference + Descriptor.ContainerString + " )" )
583
586
.str ();
@@ -633,7 +636,7 @@ void LoopConvertCheck::getArrayLoopQualifiers(ASTContext *Context,
633
636
}
634
637
Type = Type->getPointeeType ();
635
638
}
636
- Descriptor.IsTriviallyCopyable = Type. isTriviallyCopyableType (*Context) ;
639
+ Descriptor.ElemType = Type;
637
640
}
638
641
}
639
642
@@ -654,8 +657,7 @@ void LoopConvertCheck::getIteratorLoopQualifiers(ASTContext *Context,
654
657
// If the dereference operator returns by value then test for the
655
658
// canonical const qualification of the init variable type.
656
659
Descriptor.DerefByConstRef = CanonicalInitVarType.isConstQualified ();
657
- Descriptor.IsTriviallyCopyable =
658
- DerefByValueType->isTriviallyCopyableType (*Context);
660
+ Descriptor.ElemType = *DerefByValueType;
659
661
} else {
660
662
if (const auto *DerefType =
661
663
Nodes.getNodeAs <QualType>(DerefByRefResultName)) {
@@ -665,8 +667,7 @@ void LoopConvertCheck::getIteratorLoopQualifiers(ASTContext *Context,
665
667
auto ValueType = DerefType->getNonReferenceType ();
666
668
667
669
Descriptor.DerefByConstRef = ValueType.isConstQualified ();
668
- Descriptor.IsTriviallyCopyable =
669
- ValueType.isTriviallyCopyableType (*Context);
670
+ Descriptor.ElemType = ValueType;
670
671
} else {
671
672
// By nature of the matcher this case is triggered only for built-in
672
673
// iterator types (i.e. pointers).
@@ -676,9 +677,7 @@ void LoopConvertCheck::getIteratorLoopQualifiers(ASTContext *Context,
676
677
// We test for const qualification of the pointed-at type.
677
678
Descriptor.DerefByConstRef =
678
679
CanonicalInitVarType->getPointeeType ().isConstQualified ();
679
- Descriptor.IsTriviallyCopyable =
680
- CanonicalInitVarType->getPointeeType ().isTriviallyCopyableType (
681
- *Context);
680
+ Descriptor.ElemType = CanonicalInitVarType->getPointeeType ();
682
681
}
683
682
}
684
683
}
0 commit comments