Remove find_format_arg_expr AST fallback

This commit is contained in:
Alex Macleod 2024-08-10 10:50:47 +00:00
parent cb806113e0
commit 4231e93bf7
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); span_useless_format(cx, call_site, sugg, applicability);
}, },
([arg], [piece]) => { ([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 && let FormatArgsPiece::Placeholder(placeholder) = piece
&& placeholder.format_trait == FormatTrait::Display && placeholder.format_trait == FormatTrait::Display
&& placeholder.format_options == FormatOptions::default() && placeholder.format_options == FormatOptions::default()

View File

@ -224,13 +224,11 @@ fn check_templates(&self) {
if let FormatArgsPiece::Placeholder(placeholder) = piece if let FormatArgsPiece::Placeholder(placeholder) = piece
&& let Ok(index) = placeholder.argument.index && let Ok(index) = placeholder.argument.index
&& let Some(arg) = self.format_args.arguments.all_args().get(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); self.check_unused_format_specifier(placeholder, arg_expr);
if let Ok(arg_expr) = arg_expr if placeholder.format_trait == FormatTrait::Display
&& placeholder.format_trait == FormatTrait::Display
&& placeholder.format_options == FormatOptions::default() && placeholder.format_options == FormatOptions::default()
&& !self.is_aliased(index) && !self.is_aliased(index)
{ {
@ -242,28 +240,13 @@ fn check_templates(&self) {
} }
} }
fn check_unused_format_specifier( fn check_unused_format_specifier(&self, placeholder: &FormatPlaceholder, arg: &Expr<'_>) {
&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(_)),
};
let options = &placeholder.format_options; 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 if let Some(placeholder_span) = placeholder.span
&& is_format_args
&& *options != FormatOptions::default() && *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( span_lint_and_then(
self.cx, self.cx,
@ -274,7 +257,7 @@ fn check_unused_format_specifier(
let mut suggest_format = |spec| { let mut suggest_format = |spec| {
let message = format!("for the {spec} to apply consider using `format!()`"); 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( diag.span_suggestion(
self.cx.sess().source_map().span_until_char(mac_call.span, '!'), self.cx.sess().source_map().span_until_char(mac_call.span, '!'),
message, message,

View File

@ -196,7 +196,7 @@ fn check_self_in_format_args(&self) {
&& trait_name == self.format_trait_impl.name && trait_name == self.format_trait_impl.name
&& let Ok(index) = placeholder.argument.index && let Ok(index) = placeholder.argument.index
&& let Some(arg) = format_args.arguments.all_args().get(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); 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 /// Attempt to find the [`rustc_hir::Expr`] that corresponds to the [`FormatArgument`]'s value
/// it cannot be found it will return the [`rustc_ast::Expr`]. pub fn find_format_arg_expr<'hir>(start: &'hir Expr<'hir>, target: &FormatArgument) -> Option<&'hir Expr<'hir>> {
pub fn find_format_arg_expr<'hir, 'ast>(
start: &'hir Expr<'hir>,
target: &'ast FormatArgument,
) -> Result<&'hir Expr<'hir>, &'ast rustc_ast::Expr> {
let SpanData { let SpanData {
lo, lo,
hi, hi,
@ -449,7 +445,6 @@ pub fn find_format_arg_expr<'hir, 'ast>(
ControlFlow::Continue(()) ControlFlow::Continue(())
} }
}) })
.ok_or(&target.expr)
} }
/// Span of the `:` and format specifiers /// Span of the `:` and format specifiers