@@ -1150,8 +1150,6 @@ fn detect_same_item_push<'tcx>(
1150
1150
{
1151
1151
// Make sure that the push does not involve possibly mutating values
1152
1152
if let PatKind :: Wild = pat. kind {
1153
- let vec_str = snippet_with_macro_callsite ( cx, vec. span , "" ) ;
1154
- let item_str = snippet_with_macro_callsite ( cx, pushed_item. span , "" ) ;
1155
1153
if let ExprKind :: Path ( ref qpath) = pushed_item. kind {
1156
1154
match qpath_res ( cx, qpath, pushed_item. hir_id ) {
1157
1155
// immutable bindings that are initialized with literal or constant
@@ -1167,33 +1165,11 @@ fn detect_same_item_push<'tcx>(
1167
1165
then {
1168
1166
match init. kind {
1169
1167
// immutable bindings that are initialized with literal
1170
- ExprKind :: Lit ( ..) => {
1171
- span_lint_and_help(
1172
- cx,
1173
- SAME_ITEM_PUSH ,
1174
- vec. span,
1175
- "it looks like the same item is being pushed into this Vec" ,
1176
- None ,
1177
- & format!(
1178
- "try using vec![{};SIZE] or {}.resize(NEW_SIZE, {})" ,
1179
- item_str, vec_str, item_str
1180
- ) ,
1181
- )
1182
- } ,
1168
+ ExprKind :: Lit ( ..) => emit_lint( cx, vec, pushed_item) ,
1183
1169
// immutable bindings that are initialized with constant
1184
1170
ExprKind :: Path ( ref path) => {
1185
1171
if let Res :: Def ( DefKind :: Const , ..) = qpath_res( cx, path, init. hir_id) {
1186
- span_lint_and_help(
1187
- cx,
1188
- SAME_ITEM_PUSH ,
1189
- vec. span,
1190
- "it looks like the same item is being pushed into this Vec" ,
1191
- None ,
1192
- & format!(
1193
- "try using vec![{};SIZE] or {}.resize(NEW_SIZE, {})" ,
1194
- item_str, vec_str, item_str
1195
- ) ,
1196
- )
1172
+ emit_lint( cx, vec, pushed_item) ;
1197
1173
}
1198
1174
}
1199
1175
_ => { } ,
@@ -1202,37 +1178,34 @@ fn detect_same_item_push<'tcx>(
1202
1178
}
1203
1179
} ,
1204
1180
// constant
1205
- Res :: Def ( DefKind :: Const , ..) => span_lint_and_help (
1206
- cx,
1207
- SAME_ITEM_PUSH ,
1208
- vec. span ,
1209
- "it looks like the same item is being pushed into this Vec" ,
1210
- None ,
1211
- & format ! (
1212
- "try using vec![{};SIZE] or {}.resize(NEW_SIZE, {})" ,
1213
- item_str, vec_str, item_str
1214
- ) ,
1215
- ) ,
1181
+ Res :: Def ( DefKind :: Const , ..) => emit_lint ( cx, vec, pushed_item) ,
1216
1182
_ => { } ,
1217
1183
}
1218
1184
} else if let ExprKind :: Lit ( ..) = pushed_item. kind {
1219
1185
// literal
1220
- span_lint_and_help (
1221
- cx,
1222
- SAME_ITEM_PUSH ,
1223
- vec. span ,
1224
- "it looks like the same item is being pushed into this Vec" ,
1225
- None ,
1226
- & format ! (
1227
- "try using vec![{};SIZE] or {}.resize(NEW_SIZE, {})" ,
1228
- item_str, vec_str, item_str
1229
- ) ,
1230
- )
1186
+ emit_lint ( cx, vec, pushed_item) ;
1231
1187
}
1232
1188
}
1233
1189
}
1234
1190
}
1235
1191
}
1192
+
1193
+ fn emit_lint ( cx : & LateContext < ' _ > , vec : & Expr < ' _ > , pushed_item : & Expr < ' _ > ) {
1194
+ let vec_str = snippet_with_macro_callsite ( cx, vec. span , "" ) ;
1195
+ let item_str = snippet_with_macro_callsite ( cx, pushed_item. span , "" ) ;
1196
+
1197
+ span_lint_and_help (
1198
+ cx,
1199
+ SAME_ITEM_PUSH ,
1200
+ vec. span ,
1201
+ "it looks like the same item is being pushed into this Vec" ,
1202
+ None ,
1203
+ & format ! (
1204
+ "try using vec![{};SIZE] or {}.resize(NEW_SIZE, {})" ,
1205
+ item_str, vec_str, item_str
1206
+ ) ,
1207
+ )
1208
+ }
1236
1209
}
1237
1210
1238
1211
/// Checks for looping over a range and then indexing a sequence with it.
0 commit comments