Skip to content

Commit

Permalink
Remove find_format_arg_expr AST fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexendoo committed Aug 10, 2024
1 parent cb80611 commit 4231e93
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 32 deletions.
2 changes: 1 addition & 1 deletion clippy_lints/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl<'tcx> LateLintPass<'tcx> for UselessFormat {
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()
Expand Down
29 changes: 6 additions & 23 deletions clippy_lints/src/format_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,11 @@ impl<'a, 'tcx> FormatArgsExpr<'a, 'tcx> {
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)
{
Expand All @@ -242,28 +240,13 @@ impl<'a, 'tcx> FormatArgsExpr<'a, 'tcx> {
}
}

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,
Expand All @@ -274,7 +257,7 @@ impl<'a, 'tcx> FormatArgsExpr<'a, 'tcx> {
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,
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/format_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ impl<'a, 'tcx> FormatImplExpr<'a, 'tcx> {
&& 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);
}
Expand Down
9 changes: 2 additions & 7 deletions clippy_utils/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,12 +426,8 @@ impl FormatArgsStorage {
}
}

/// 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,
Expand All @@ -449,7 +445,6 @@ pub fn find_format_arg_expr<'hir, 'ast>(
ControlFlow::Continue(())
}
})
.ok_or(&target.expr)
}

/// Span of the `:` and format specifiers
Expand Down

0 comments on commit 4231e93

Please sign in to comment.