Skip to content

Commit bfe7f07

Browse files
committed
Auto merge of rust-lang#13248 - Alexendoo:format-arg-ast-fallback, r=xFrednet
Remove `find_format_arg_expr` AST fallback If the function fails where it shouldn't we can fix that directly, but the fallback path is untested as I'm not aware of a case where it would fail changelog: none
2 parents 233b5f2 + 4231e93 commit bfe7f07

File tree

4 files changed

+10
-32
lines changed

4 files changed

+10
-32
lines changed

clippy_lints/src/format.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl<'tcx> LateLintPass<'tcx> for UselessFormat {
7373
span_useless_format(cx, call_site, sugg, applicability);
7474
},
7575
([arg], [piece]) => {
76-
if let Ok(value) = find_format_arg_expr(expr, arg)
76+
if let Some(value) = find_format_arg_expr(expr, arg)
7777
&& let FormatArgsPiece::Placeholder(placeholder) = piece
7878
&& placeholder.format_trait == FormatTrait::Display
7979
&& placeholder.format_options == FormatOptions::default()

clippy_lints/src/format_args.rs

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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,

clippy_lints/src/format_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ impl<'a, 'tcx> FormatImplExpr<'a, 'tcx> {
196196
&& trait_name == self.format_trait_impl.name
197197
&& let Ok(index) = placeholder.argument.index
198198
&& let Some(arg) = format_args.arguments.all_args().get(index)
199-
&& let Ok(arg_expr) = find_format_arg_expr(self.expr, arg)
199+
&& let Some(arg_expr) = find_format_arg_expr(self.expr, arg)
200200
{
201201
self.check_format_arg_self(arg_expr);
202202
}

clippy_utils/src/macros.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -426,12 +426,8 @@ impl FormatArgsStorage {
426426
}
427427
}
428428

429-
/// Attempt to find the [`rustc_hir::Expr`] that corresponds to the [`FormatArgument`]'s value, if
430-
/// it cannot be found it will return the [`rustc_ast::Expr`].
431-
pub fn find_format_arg_expr<'hir, 'ast>(
432-
start: &'hir Expr<'hir>,
433-
target: &'ast FormatArgument,
434-
) -> Result<&'hir Expr<'hir>, &'ast rustc_ast::Expr> {
429+
/// Attempt to find the [`rustc_hir::Expr`] that corresponds to the [`FormatArgument`]'s value
430+
pub fn find_format_arg_expr<'hir>(start: &'hir Expr<'hir>, target: &FormatArgument) -> Option<&'hir Expr<'hir>> {
435431
let SpanData {
436432
lo,
437433
hi,
@@ -449,7 +445,6 @@ pub fn find_format_arg_expr<'hir, 'ast>(
449445
ControlFlow::Continue(())
450446
}
451447
})
452-
.ok_or(&target.expr)
453448
}
454449

455450
/// Span of the `:` and format specifiers

0 commit comments

Comments
 (0)