@@ -1321,14 +1321,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1321
1321
let span = expr. span . shrink_to_hi ( ) ;
1322
1322
let subdiag = if self . type_is_copy_modulo_regions ( self . param_env , ty) {
1323
1323
errors:: OptionResultRefMismatch :: Copied { span, def_path }
1324
- } else if let Some ( clone_did) = self . tcx . lang_items ( ) . clone_trait ( )
1325
- && rustc_trait_selection:: traits:: type_known_to_meet_bound_modulo_regions (
1326
- self ,
1327
- self . param_env ,
1328
- ty,
1329
- clone_did,
1330
- )
1331
- {
1324
+ } else if self . type_is_clone_modulo_regions ( self . param_env , ty) {
1332
1325
errors:: OptionResultRefMismatch :: Cloned { span, def_path }
1333
1326
} else {
1334
1327
return false ;
@@ -2183,6 +2176,87 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2183
2176
}
2184
2177
}
2185
2178
2179
+ /// Suggest replacing comma with semicolon in incorrect repeat expressions
2180
+ /// like `["_", 10]` or `vec![String::new(), 10]`.
2181
+ pub ( crate ) fn suggest_semicolon_in_repeat_expr (
2182
+ & self ,
2183
+ err : & mut Diag < ' _ > ,
2184
+ expr : & hir:: Expr < ' _ > ,
2185
+ expr_ty : Ty < ' tcx > ,
2186
+ ) -> bool {
2187
+ // Check if `expr` is contained in array of two elements
2188
+ if let hir:: Node :: Expr ( array_expr) = self . tcx . parent_hir_node ( expr. hir_id )
2189
+ && let hir:: ExprKind :: Array ( elements) = array_expr. kind
2190
+ && let [ first, second] = & elements[ ..]
2191
+ && second. hir_id == expr. hir_id
2192
+ {
2193
+ // Span between the two elements of the array
2194
+ let comma_span = first. span . between ( second. span ) ;
2195
+
2196
+ // Check if `expr` is a constant value of type `usize`.
2197
+ // This can only detect const variable declarations and
2198
+ // calls to const functions.
2199
+
2200
+ // Checking this here instead of rustc_hir::hir because
2201
+ // this check needs access to `self.tcx` but rustc_hir
2202
+ // has no access to `TyCtxt`.
2203
+ let expr_is_const_usize = expr_ty. is_usize ( )
2204
+ && match expr. kind {
2205
+ ExprKind :: Path ( QPath :: Resolved (
2206
+ None ,
2207
+ Path { res : Res :: Def ( DefKind :: Const , _) , .. } ,
2208
+ ) ) => true ,
2209
+ ExprKind :: Call (
2210
+ Expr {
2211
+ kind :
2212
+ ExprKind :: Path ( QPath :: Resolved (
2213
+ None ,
2214
+ Path { res : Res :: Def ( DefKind :: Fn , fn_def_id) , .. } ,
2215
+ ) ) ,
2216
+ ..
2217
+ } ,
2218
+ _,
2219
+ ) => self . tcx . is_const_fn ( * fn_def_id) ,
2220
+ _ => false ,
2221
+ } ;
2222
+
2223
+ // Type of the first element is guaranteed to be checked
2224
+ // when execution reaches here because `mismatched types`
2225
+ // error occurs only when type of second element of array
2226
+ // is not the same as type of first element.
2227
+ let first_ty = self . typeck_results . borrow ( ) . expr_ty ( first) ;
2228
+
2229
+ // `array_expr` is from a macro `vec!["a", 10]` if
2230
+ // 1. array expression's span is imported from a macro
2231
+ // 2. first element of array implements `Clone` trait
2232
+ // 3. second element is an integer literal or is an expression of `usize` like type
2233
+ if self . tcx . sess . source_map ( ) . is_imported ( array_expr. span )
2234
+ && self . type_is_clone_modulo_regions ( self . param_env , first_ty)
2235
+ && ( expr. is_size_lit ( ) || expr_ty. is_usize_like ( ) )
2236
+ {
2237
+ err. subdiagnostic ( errors:: ReplaceCommaWithSemicolon {
2238
+ comma_span,
2239
+ descr : "a vector" ,
2240
+ } ) ;
2241
+ return true ;
2242
+ }
2243
+
2244
+ // `array_expr` is from an array `["a", 10]` if
2245
+ // 1. first element of array implements `Copy` trait
2246
+ // 2. second element is an integer literal or is a const value of type `usize`
2247
+ if self . type_is_copy_modulo_regions ( self . param_env , first_ty)
2248
+ && ( expr. is_size_lit ( ) || expr_is_const_usize)
2249
+ {
2250
+ err. subdiagnostic ( errors:: ReplaceCommaWithSemicolon {
2251
+ comma_span,
2252
+ descr : "an array" ,
2253
+ } ) ;
2254
+ return true ;
2255
+ }
2256
+ }
2257
+ false
2258
+ }
2259
+
2186
2260
/// If the expected type is an enum (Issue #55250) with any variants whose
2187
2261
/// sole field is of the found type, suggest such variants. (Issue #42764)
2188
2262
pub ( crate ) fn suggest_compatible_variants (
0 commit comments