@@ -202,33 +202,59 @@ pub fn suggest_constraining_type_param(
202202 // Suggestion:
203203 // fn foo<T>(t: T) where T: Foo, T: Bar {... }
204204 // - insert: `, T: Zar`
205+ //
206+ // Additionally, there may be no `where` clause whatsoever in the case that this was
207+ // reached becauase the generic parameter has a default:
208+ //
209+ // Message:
210+ // trait Foo<T=()> {... }
211+ // - help: consider further restricting this type parameter with `where T: Zar`
212+ //
213+ // Suggestion:
214+ // trait Foo<T=()> where T: Zar {... }
215+ // - insert: `where T: Zar`
205216
206- let mut param_spans = Vec :: new ( ) ;
217+ if matches ! ( param. kind, hir:: GenericParamKind :: Type { default : Some ( _) , .. } )
218+ && generics. where_clause . predicates . len ( ) == 0
219+ {
220+ // Suggest a bound, but there are no existing where clauses for this `<T=Foo>`, so
221+ // suggest adding one.
222+ err. span_suggestion_verbose (
223+ generics. where_clause . tail_span_for_suggestion ( ) ,
224+ & msg_restrict_type_further,
225+ format ! ( " where {}: {}" , param_name, constraint) ,
226+ Applicability :: MachineApplicable ,
227+ ) ;
228+ } else {
229+ let mut param_spans = Vec :: new ( ) ;
207230
208- for predicate in generics. where_clause . predicates {
209- if let WherePredicate :: BoundPredicate ( WhereBoundPredicate {
210- span, bounded_ty, ..
211- } ) = predicate
212- {
213- if let TyKind :: Path ( QPath :: Resolved ( _, path) ) = & bounded_ty. kind {
214- if let Some ( segment) = path. segments . first ( ) {
215- if segment. ident . to_string ( ) == param_name {
216- param_spans. push ( span) ;
231+ for predicate in generics. where_clause . predicates {
232+ if let WherePredicate :: BoundPredicate ( WhereBoundPredicate {
233+ span,
234+ bounded_ty,
235+ ..
236+ } ) = predicate
237+ {
238+ if let TyKind :: Path ( QPath :: Resolved ( _, path) ) = & bounded_ty. kind {
239+ if let Some ( segment) = path. segments . first ( ) {
240+ if segment. ident . to_string ( ) == param_name {
241+ param_spans. push ( span) ;
242+ }
217243 }
218244 }
219245 }
220246 }
221- }
222247
223- match & param_spans[ ..] {
224- & [ & param_span] => suggest_restrict ( param_span. shrink_to_hi ( ) ) ,
225- _ => {
226- err. span_suggestion_verbose (
227- generics. where_clause . tail_span_for_suggestion ( ) ,
228- & msg_restrict_type_further,
229- format ! ( ", {}: {}" , param_name, constraint) ,
230- Applicability :: MachineApplicable ,
231- ) ;
248+ match & param_spans[ ..] {
249+ & [ & param_span] => suggest_restrict ( param_span. shrink_to_hi ( ) ) ,
250+ _ => {
251+ err. span_suggestion_verbose (
252+ generics. where_clause . tail_span_for_suggestion ( ) ,
253+ & msg_restrict_type_further,
254+ format ! ( ", {}: {}" , param_name, constraint) ,
255+ Applicability :: MachineApplicable ,
256+ ) ;
257+ }
232258 }
233259 }
234260
0 commit comments