@@ -1150,8 +1150,6 @@ fn detect_same_item_push<'tcx>(
11501150 {
11511151 // Make sure that the push does not involve possibly mutating values
11521152 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 , "" ) ;
11551153 if let ExprKind :: Path ( ref qpath) = pushed_item. kind {
11561154 match qpath_res ( cx, qpath, pushed_item. hir_id ) {
11571155 // immutable bindings that are initialized with literal or constant
@@ -1167,33 +1165,11 @@ fn detect_same_item_push<'tcx>(
11671165 then {
11681166 match init. kind {
11691167 // 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) ,
11831169 // immutable bindings that are initialized with constant
11841170 ExprKind :: Path ( ref path) => {
11851171 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) ;
11971173 }
11981174 }
11991175 _ => { } ,
@@ -1202,37 +1178,34 @@ fn detect_same_item_push<'tcx>(
12021178 }
12031179 } ,
12041180 // 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) ,
12161182 _ => { } ,
12171183 }
12181184 } else if let ExprKind :: Lit ( ..) = pushed_item. kind {
12191185 // 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) ;
12311187 }
12321188 }
12331189 }
12341190 }
12351191 }
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+ }
12361209}
12371210
12381211/// Checks for looping over a range and then indexing a sequence with it.
0 commit comments