@@ -224,13 +224,11 @@ impl<'a, 'tcx> FormatArgsExpr<'a, 'tcx> {
224224 if let FormatArgsPiece :: Placeholder ( placeholder) = piece
225225 && let Ok ( index) = placeholder. argument . index
226226 && let Some ( arg) = self . format_args . arguments . all_args ( ) . get ( index)
227+ && let Some ( arg_expr) = find_format_arg_expr ( self . expr , arg)
227228 {
228- let arg_expr = find_format_arg_expr ( self . expr , arg) ;
229-
230229 self . check_unused_format_specifier ( placeholder, arg_expr) ;
231230
232- if let Ok ( arg_expr) = arg_expr
233- && placeholder. format_trait == FormatTrait :: Display
231+ if placeholder. format_trait == FormatTrait :: Display
234232 && placeholder. format_options == FormatOptions :: default ( )
235233 && !self . is_aliased ( index)
236234 {
@@ -242,28 +240,13 @@ impl<'a, 'tcx> FormatArgsExpr<'a, 'tcx> {
242240 }
243241 }
244242
245- fn check_unused_format_specifier (
246- & self ,
247- placeholder : & FormatPlaceholder ,
248- arg_expr : Result < & Expr < ' _ > , & rustc_ast:: Expr > ,
249- ) {
250- let ty_or_ast_expr = arg_expr. map ( |expr| self . cx . typeck_results ( ) . expr_ty ( expr) . peel_refs ( ) ) ;
251-
252- let is_format_args = match ty_or_ast_expr {
253- Ok ( ty) => is_type_lang_item ( self . cx , ty, LangItem :: FormatArguments ) ,
254- Err ( expr) => matches ! ( expr. peel_parens_and_refs( ) . kind, rustc_ast:: ExprKind :: FormatArgs ( _) ) ,
255- } ;
256-
243+ fn check_unused_format_specifier ( & self , placeholder : & FormatPlaceholder , arg : & Expr < ' _ > ) {
257244 let options = & placeholder. format_options ;
258245
259- let arg_span = match arg_expr {
260- Ok ( expr) => expr. span ,
261- Err ( expr) => expr. span ,
262- } ;
263-
264246 if let Some ( placeholder_span) = placeholder. span
265- && is_format_args
266247 && * options != FormatOptions :: default ( )
248+ && let ty = self . cx . typeck_results ( ) . expr_ty ( arg) . peel_refs ( )
249+ && is_type_lang_item ( self . cx , ty, LangItem :: FormatArguments )
267250 {
268251 span_lint_and_then (
269252 self . cx ,
@@ -274,7 +257,7 @@ impl<'a, 'tcx> FormatArgsExpr<'a, 'tcx> {
274257 let mut suggest_format = |spec| {
275258 let message = format ! ( "for the {spec} to apply consider using `format!()`" ) ;
276259
277- if let Some ( mac_call) = matching_root_macro_call ( self . cx , arg_span , sym:: format_args_macro) {
260+ if let Some ( mac_call) = matching_root_macro_call ( self . cx , arg . span , sym:: format_args_macro) {
278261 diag. span_suggestion (
279262 self . cx . sess ( ) . source_map ( ) . span_until_char ( mac_call. span , '!' ) ,
280263 message,
0 commit comments