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