diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -18539,7 +18539,7 @@ MemberExpr *ME = nullptr; BindingDecl *BD = nullptr; if (auto *V = dyn_cast(Var)) { - if (V->getInit()) + if (V->getInit() && V->getType()->isReferenceType()) ME = dyn_cast(V->getInit()->IgnoreImplicit()); } else if ((BD = dyn_cast(Var))) { ME = dyn_cast_or_null(BD->getBinding()); diff --git a/clang/test/SemaCXX/lambda-expressions.cpp b/clang/test/SemaCXX/lambda-expressions.cpp --- a/clang/test/SemaCXX/lambda-expressions.cpp +++ b/clang/test/SemaCXX/lambda-expressions.cpp @@ -665,3 +665,21 @@ // expected-note@-2 2 {{default capture by}} } }; + + +namespace bitfields { +struct S { + int s : 4; // expected-note {{bit-field is declared here}} +}; + +void f() { + S s; + const int & a = s.s; // expected-note{{'a' declared here}} + int b = s.s; + auto l = [&]() { + a; // expected-error{{cannot capture a bit-field by reference}} + b; + s.s; + }; +} +}