diff --git a/clang-tools-extra/clangd/InlayHints.cpp b/clang-tools-extra/clangd/InlayHints.cpp --- a/clang-tools-extra/clangd/InlayHints.cpp +++ b/clang-tools-extra/clangd/InlayHints.cpp @@ -198,8 +198,7 @@ Cfg(Cfg), RestrictRange(std::move(RestrictRange)), MainFileID(AST.getSourceManager().getMainFileID()), Resolver(AST.getHeuristicResolver()), - TypeHintPolicy(this->AST.getPrintingPolicy()), - StructuredBindingPolicy(this->AST.getPrintingPolicy()) { + TypeHintPolicy(this->AST.getPrintingPolicy()) { bool Invalid = false; llvm::StringRef Buf = AST.getSourceManager().getBufferData(MainFileID, &Invalid); @@ -209,14 +208,8 @@ TypeHintPolicy.AnonymousTagLocations = false; // do not print lambda locations - // For structured bindings, print canonical types. This is important because - // for bindings that use the tuple_element protocol, the non-canonical types - // would be "tuple_element::type". - // For "auto", we often prefer sugared types. // Not setting PrintCanonicalTypes for "auto" allows // SuppressDefaultTemplateArgs (set by default) to have an effect. - StructuredBindingPolicy = TypeHintPolicy; - StructuredBindingPolicy.PrintCanonicalTypes = true; } bool VisitTypeLoc(TypeLoc TL) { @@ -298,8 +291,12 @@ // but show hints for the individual bindings. if (auto *DD = dyn_cast(D)) { for (auto *Binding : DD->bindings()) { - addTypeHint(Binding->getLocation(), Binding->getType(), /*Prefix=*/": ", - StructuredBindingPolicy); + // For structured bindings, print canonical types. This is important + // because for bindings that use the tuple_element protocol, the + // non-canonical types would be "tuple_element::type". + if (auto Type = Binding->getType(); !Type.isNull()) + addTypeHint(Binding->getLocation(), Type.getCanonicalType(), + /*Prefix=*/": ", TypeHintPolicy); } return true; } @@ -707,14 +704,7 @@ FileID MainFileID; StringRef MainFileBuf; const HeuristicResolver *Resolver; - // We want to suppress default template arguments, but otherwise print - // canonical types. Unfortunately, they're conflicting policies so we can't - // have both. For regular types, suppressing template arguments is more - // important, whereas printing canonical types is crucial for structured - // bindings, so we use two separate policies. (See the constructor where - // the policies are initialized for more details.) PrintingPolicy TypeHintPolicy; - PrintingPolicy StructuredBindingPolicy; }; } // namespace diff --git a/clang-tools-extra/clangd/unittests/InlayHintTests.cpp b/clang-tools-extra/clangd/unittests/InlayHintTests.cpp --- a/clang-tools-extra/clangd/unittests/InlayHintTests.cpp +++ b/clang-tools-extra/clangd/unittests/InlayHintTests.cpp @@ -1347,8 +1347,11 @@ struct A {}; A foo(); auto $var[[var]] = foo(); + A bar[1]; + auto [$binding[[value]]] = bar; )cpp", - ExpectedHint{": A", "var"}); + ExpectedHint{": A", "var"}, + ExpectedHint{": A", "binding"}); } TEST(TypeHints, Deduplication) {