Auto merge of #12274 - nyurik:nit-format-impl, r=xFrednet

Minor refactor format-impls

Move all linting logic into a single format implementations struct

This should help with the future format-args improvements.

TODO: do the same with format_args.rs, perhaps in the same PR

**NOTE TO REVIEWERS**:  use "hide whitespace" in the github diff -- most of the code has shifted, but relatively low number of lines actually modified.

changelog: none
This commit is contained in:
bors 2024-02-11 17:53:31 +00:00
commit d427b70fcf

View File

@ -7,7 +7,7 @@ use rustc_hir::{Expr, ExprKind, Impl, ImplItem, ImplItemKind, QPath};
use rustc_lint::{LateContext, LateLintPass}; use rustc_lint::{LateContext, LateLintPass};
use rustc_session::impl_lint_pass; use rustc_session::impl_lint_pass;
use rustc_span::symbol::kw; use rustc_span::symbol::kw;
use rustc_span::{sym, Span, Symbol}; use rustc_span::{sym, Symbol};
declare_clippy_lint! { declare_clippy_lint! {
/// ### What it does /// ### What it does
@ -119,123 +119,132 @@ impl<'tcx> LateLintPass<'tcx> for FormatImpl {
} }
fn check_impl_item_post(&mut self, cx: &LateContext<'_>, impl_item: &ImplItem<'_>) { fn check_impl_item_post(&mut self, cx: &LateContext<'_>, impl_item: &ImplItem<'_>) {
// Assume no nested Impl of Debug and Display within eachother // Assume no nested Impl of Debug and Display within each other
if is_format_trait_impl(cx, impl_item).is_some() { if is_format_trait_impl(cx, impl_item).is_some() {
self.format_trait_impl = None; self.format_trait_impl = None;
} }
} }
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
let Some(format_trait_impl) = self.format_trait_impl else { if let Some(format_trait_impl) = self.format_trait_impl {
return; let linter = FormatImplExpr {
}; cx,
expr,
if format_trait_impl.name == sym::Display { format_trait_impl,
check_to_string_in_display(cx, expr); };
linter.check_to_string_in_display();
linter.check_self_in_format_args();
linter.check_print_in_format_impl();
} }
check_self_in_format_args(cx, expr, format_trait_impl);
check_print_in_format_impl(cx, expr, format_trait_impl);
} }
} }
fn check_to_string_in_display(cx: &LateContext<'_>, expr: &Expr<'_>) { struct FormatImplExpr<'a, 'tcx> {
if let ExprKind::MethodCall(path, self_arg, ..) = expr.kind cx: &'a LateContext<'tcx>,
// Get the hir_id of the object we are calling the method on expr: &'tcx Expr<'tcx>,
// Is the method to_string() ? format_trait_impl: FormatTraitNames,
&& path.ident.name == sym::to_string
// Is the method a part of the ToString trait? (i.e. not to_string() implemented
// separately)
&& let Some(expr_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id)
&& is_diag_trait_item(cx, expr_def_id, sym::ToString)
// Is the method is called on self
&& let ExprKind::Path(QPath::Resolved(_, path)) = self_arg.kind
&& let [segment] = path.segments
&& segment.ident.name == kw::SelfLower
{
span_lint(
cx,
RECURSIVE_FORMAT_IMPL,
expr.span,
"using `self.to_string` in `fmt::Display` implementation will cause infinite recursion",
);
}
} }
fn check_self_in_format_args<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, impl_trait: FormatTraitNames) { impl<'a, 'tcx> FormatImplExpr<'a, 'tcx> {
// Check each arg in format calls - do we ever use Display on self (directly or via deref)? fn check_to_string_in_display(&self) {
if let Some(outer_macro) = root_macro_call_first_node(cx, expr) if self.format_trait_impl.name == sym::Display
&& let macro_def_id = outer_macro.def_id && let ExprKind::MethodCall(path, self_arg, ..) = self.expr.kind
&& is_format_macro(cx, macro_def_id) // Get the hir_id of the object we are calling the method on
&& let Some(format_args) = find_format_args(cx, expr, outer_macro.expn) // Is the method to_string() ?
{ && path.ident.name == sym::to_string
for piece in &format_args.template { // Is the method a part of the ToString trait? (i.e. not to_string() implemented
if let FormatArgsPiece::Placeholder(placeholder) = piece // separately)
&& let trait_name = match placeholder.format_trait { && let Some(expr_def_id) = self.cx.typeck_results().type_dependent_def_id(self.expr.hir_id)
FormatTrait::Display => sym::Display, && is_diag_trait_item(self.cx, expr_def_id, sym::ToString)
FormatTrait::Debug => sym::Debug, // Is the method is called on self
FormatTrait::LowerExp => sym!(LowerExp), && let ExprKind::Path(QPath::Resolved(_, path)) = self_arg.kind
FormatTrait::UpperExp => sym!(UpperExp), && let [segment] = path.segments
FormatTrait::Octal => sym!(Octal), && segment.ident.name == kw::SelfLower
FormatTrait::Pointer => sym::Pointer, {
FormatTrait::Binary => sym!(Binary), span_lint(
FormatTrait::LowerHex => sym!(LowerHex), self.cx,
FormatTrait::UpperHex => sym!(UpperHex), RECURSIVE_FORMAT_IMPL,
self.expr.span,
"using `self.to_string` in `fmt::Display` implementation will cause infinite recursion",
);
}
}
fn check_self_in_format_args(&self) {
// Check each arg in format calls - do we ever use Display on self (directly or via deref)?
if let Some(outer_macro) = root_macro_call_first_node(self.cx, self.expr)
&& let macro_def_id = outer_macro.def_id
&& is_format_macro(self.cx, macro_def_id)
&& let Some(format_args) = find_format_args(self.cx, self.expr, outer_macro.expn)
{
for piece in &format_args.template {
if let FormatArgsPiece::Placeholder(placeholder) = piece
&& let trait_name = match placeholder.format_trait {
FormatTrait::Display => sym::Display,
FormatTrait::Debug => sym::Debug,
FormatTrait::LowerExp => sym!(LowerExp),
FormatTrait::UpperExp => sym!(UpperExp),
FormatTrait::Octal => sym!(Octal),
FormatTrait::Pointer => sym::Pointer,
FormatTrait::Binary => sym!(Binary),
FormatTrait::LowerHex => sym!(LowerHex),
FormatTrait::UpperHex => sym!(UpperHex),
}
&& 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)
{
self.check_format_arg_self(arg_expr);
} }
&& trait_name == impl_trait.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(expr, arg)
{
check_format_arg_self(cx, expr.span, arg_expr, impl_trait);
} }
} }
} }
}
fn check_format_arg_self(cx: &LateContext<'_>, span: Span, arg: &Expr<'_>, impl_trait: FormatTraitNames) { fn check_format_arg_self(&self, arg: &Expr<'_>) {
// Handle multiple dereferencing of references e.g. &&self // Handle multiple dereferencing of references e.g. &&self
// Handle dereference of &self -> self that is equivalent (i.e. via *self in fmt() impl) // Handle dereference of &self -> self that is equivalent (i.e. via *self in fmt() impl)
// Since the argument to fmt is itself a reference: &self // Since the argument to fmt is itself a reference: &self
let reference = peel_ref_operators(cx, arg); let reference = peel_ref_operators(self.cx, arg);
let map = cx.tcx.hir(); let map = self.cx.tcx.hir();
// Is the reference self? // Is the reference self?
if path_to_local(reference).map(|x| map.name(x)) == Some(kw::SelfLower) { if path_to_local(reference).map(|x| map.name(x)) == Some(kw::SelfLower) {
let FormatTraitNames { name, .. } = impl_trait; let FormatTraitNames { name, .. } = self.format_trait_impl;
span_lint( span_lint(
cx, self.cx,
RECURSIVE_FORMAT_IMPL, RECURSIVE_FORMAT_IMPL,
span, self.expr.span,
&format!("using `self` as `{name}` in `impl {name}` will cause infinite recursion"), &format!("using `self` as `{name}` in `impl {name}` will cause infinite recursion"),
); );
}
} }
}
fn check_print_in_format_impl(cx: &LateContext<'_>, expr: &Expr<'_>, impl_trait: FormatTraitNames) { fn check_print_in_format_impl(&self) {
if let Some(macro_call) = root_macro_call_first_node(cx, expr) if let Some(macro_call) = root_macro_call_first_node(self.cx, self.expr)
&& let Some(name) = cx.tcx.get_diagnostic_name(macro_call.def_id) && let Some(name) = self.cx.tcx.get_diagnostic_name(macro_call.def_id)
{ {
let replacement = match name { let replacement = match name {
sym::print_macro | sym::eprint_macro => "write", sym::print_macro | sym::eprint_macro => "write",
sym::println_macro | sym::eprintln_macro => "writeln", sym::println_macro | sym::eprintln_macro => "writeln",
_ => return, _ => return,
}; };
let name = name.as_str().strip_suffix("_macro").unwrap(); let name = name.as_str().strip_suffix("_macro").unwrap();
span_lint_and_sugg( span_lint_and_sugg(
cx, self.cx,
PRINT_IN_FORMAT_IMPL, PRINT_IN_FORMAT_IMPL,
macro_call.span, macro_call.span,
&format!("use of `{name}!` in `{}` impl", impl_trait.name), &format!("use of `{name}!` in `{}` impl", self.format_trait_impl.name),
"replace with", "replace with",
if let Some(formatter_name) = impl_trait.formatter_name { if let Some(formatter_name) = self.format_trait_impl.formatter_name {
format!("{replacement}!({formatter_name}, ..)") format!("{replacement}!({formatter_name}, ..)")
} else { } else {
format!("{replacement}!(..)") format!("{replacement}!(..)")
}, },
Applicability::HasPlaceholders, Applicability::HasPlaceholders,
); );
}
} }
} }