Auto merge of #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
This commit is contained in:
bors 2024-08-18 09:11:03 +00:00
commit bfe7f070bd
4 changed files with 10 additions and 32 deletions

View File

@ -73,7 +73,7 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
span_useless_format(cx, call_site, sugg, applicability);
},
([arg], [piece]) => {
if let Ok(value) = find_format_arg_expr(expr, arg)
if let Some(value) = find_format_arg_expr(expr, arg)
&& let FormatArgsPiece::Placeholder(placeholder) = piece
&& placeholder.format_trait == FormatTrait::Display
&& placeholder.format_options == FormatOptions::default()

View File

@ -224,13 +224,11 @@ fn check_templates(&self) {
if let FormatArgsPiece::Placeholder(placeholder) = piece
&& let Ok(index) = placeholder.argument.index
&& let Some(arg) = self.format_args.arguments.all_args().get(index)
&& let Some(arg_expr) = find_format_arg_expr(self.expr, arg)
{
let arg_expr = find_format_arg_expr(self.expr, arg);
self.check_unused_format_specifier(placeholder, arg_expr);
if let Ok(arg_expr) = arg_expr
&& placeholder.format_trait == FormatTrait::Display
if placeholder.format_trait == FormatTrait::Display
&& placeholder.format_options == FormatOptions::default()
&& !self.is_aliased(index)
{
@ -242,28 +240,13 @@ fn check_templates(&self) {
}
}
fn check_unused_format_specifier(
&self,
placeholder: &FormatPlaceholder,
arg_expr: Result<&Expr<'_>, &rustc_ast::Expr>,
) {
let ty_or_ast_expr = arg_expr.map(|expr| self.cx.typeck_results().expr_ty(expr).peel_refs());
let is_format_args = match ty_or_ast_expr {
Ok(ty) => is_type_lang_item(self.cx, ty, LangItem::FormatArguments),
Err(expr) => matches!(expr.peel_parens_and_refs().kind, rustc_ast::ExprKind::FormatArgs(_)),
};
fn check_unused_format_specifier(&self, placeholder: &FormatPlaceholder, arg: &Expr<'_>) {
let options = &placeholder.format_options;
let arg_span = match arg_expr {
Ok(expr) => expr.span,
Err(expr) => expr.span,
};
if let Some(placeholder_span) = placeholder.span
&& is_format_args
&& *options != FormatOptions::default()
&& let ty = self.cx.typeck_results().expr_ty(arg).peel_refs()
&& is_type_lang_item(self.cx, ty, LangItem::FormatArguments)
{
span_lint_and_then(
self.cx,
@ -274,7 +257,7 @@ fn check_unused_format_specifier(
let mut suggest_format = |spec| {
let message = format!("for the {spec} to apply consider using `format!()`");
if let Some(mac_call) = matching_root_macro_call(self.cx, arg_span, sym::format_args_macro) {
if let Some(mac_call) = matching_root_macro_call(self.cx, arg.span, sym::format_args_macro) {
diag.span_suggestion(
self.cx.sess().source_map().span_until_char(mac_call.span, '!'),
message,

View File

@ -196,7 +196,7 @@ fn check_self_in_format_args(&self) {
&& trait_name == self.format_trait_impl.name
&& let Ok(index) = placeholder.argument.index
&& let Some(arg) = format_args.arguments.all_args().get(index)
&& let Ok(arg_expr) = find_format_arg_expr(self.expr, arg)
&& let Some(arg_expr) = find_format_arg_expr(self.expr, arg)
{
self.check_format_arg_self(arg_expr);
}

View File

@ -426,12 +426,8 @@ pub fn set(&self, format_args: FxHashMap<Span, FormatArgs>) {
}
}
/// Attempt to find the [`rustc_hir::Expr`] that corresponds to the [`FormatArgument`]'s value, if
/// it cannot be found it will return the [`rustc_ast::Expr`].
pub fn find_format_arg_expr<'hir, 'ast>(
start: &'hir Expr<'hir>,
target: &'ast FormatArgument,
) -> Result<&'hir Expr<'hir>, &'ast rustc_ast::Expr> {
/// Attempt to find the [`rustc_hir::Expr`] that corresponds to the [`FormatArgument`]'s value
pub fn find_format_arg_expr<'hir>(start: &'hir Expr<'hir>, target: &FormatArgument) -> Option<&'hir Expr<'hir>> {
let SpanData {
lo,
hi,
@ -449,7 +445,6 @@ pub fn find_format_arg_expr<'hir, 'ast>(
ControlFlow::Continue(())
}
})
.ok_or(&target.expr)
}
/// Span of the `:` and format specifiers