diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index f6e5798057bbd2..364d7e9855e8cf 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -5454,6 +5454,10 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, } if ((Left.is(TT_TemplateOpener)) != (Right.is(TT_TemplateCloser))) return ShouldAddSpacesInAngles(); + if (Left.is(tok::r_paren) && Right.is(TT_PointerOrReference) && + Right.isOneOf(tok::amp, tok::ampamp)) { + return true; + } // Space before TT_StructuredBindingLSquare. if (Right.is(TT_StructuredBindingLSquare)) { return !Left.isOneOf(tok::amp, tok::ampamp) || diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 61287aafe8273d..2c5e5857445c35 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -11519,6 +11519,7 @@ TEST_F(FormatTest, UnderstandsFunctionRefQualification) { AlignLeft); verifyFormat("template void operator=(T) & {}", AlignLeft); verifyFormat("template void operator=(T) && {}", AlignLeft); + verifyFormat("for (foo& cb : X)", AlignLeft); FormatStyle AlignMiddle = getLLVMStyle(); AlignMiddle.PointerAlignment = FormatStyle::PAS_Middle;