Skip to content

Commit dd92647

Browse files
committed
Use Option<Ident> for lowered param names.
Parameter patterns are lowered to an `Ident` by `lower_fn_params_to_names`, which is used when lowering bare function types, trait methods, and foreign functions. Currently, there are two exceptional cases where the lowered param can become an empty `Ident`. - If the incoming pattern is an empty `Ident`. This occurs if the parameter is anonymous, e.g. in a bare function type. - If the incoming pattern is neither an ident nor an underscore. Any such parameter will have triggered a compile error (hence the `span_delayed_bug`), but lowering still occurs. This commit replaces these empty `Ident` results with `None`, which eliminates a number of `kw::Empty` uses, and makes it impossible to fail to check for these exceptional cases. Note: the `FIXME` comment in `is_unwrap_or_empty_symbol` is removed. It actually should have been removed in rust-lang#138482, the precursor to this PR. That PR changed the lowering of wild patterns to `_` symbols instead of empty symbols, which made the mentioned underscore check load-bearing.
1 parent 9932e5e commit dd92647

File tree

2 files changed

+29
-26
lines changed

2 files changed

+29
-26
lines changed

clippy_lints/src/functions/renamed_function_params.rs

+24-21
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_hir::hir_id::OwnerId;
55
use rustc_hir::{Impl, ImplItem, ImplItemKind, ImplItemRef, ItemKind, Node, TraitRef};
66
use rustc_lint::LateContext;
77
use rustc_span::Span;
8-
use rustc_span::symbol::{Ident, Symbol, kw};
8+
use rustc_span::symbol::{Ident, kw};
99

1010
use super::RENAMED_FUNCTION_PARAMS;
1111

@@ -51,22 +51,33 @@ struct RenamedFnArgs(Vec<(Span, String)>);
5151
impl RenamedFnArgs {
5252
/// Comparing between an iterator of default names and one with current names,
5353
/// then collect the ones that got renamed.
54-
fn new<I, T>(default_names: &mut I, current_names: &mut T) -> Self
54+
fn new<I1, I2>(default_idents: &mut I1, current_idents: &mut I2) -> Self
5555
where
56-
I: Iterator<Item = Ident>,
57-
T: Iterator<Item = Ident>,
56+
I1: Iterator<Item = Option<Ident>>,
57+
I2: Iterator<Item = Option<Ident>>,
5858
{
5959
let mut renamed: Vec<(Span, String)> = vec![];
6060

61-
debug_assert!(default_names.size_hint() == current_names.size_hint());
62-
while let (Some(def_name), Some(cur_name)) = (default_names.next(), current_names.next()) {
63-
let current_name = cur_name.name;
64-
let default_name = def_name.name;
65-
if is_unused_or_empty_symbol(current_name) || is_unused_or_empty_symbol(default_name) {
66-
continue;
67-
}
68-
if current_name != default_name {
69-
renamed.push((cur_name.span, default_name.to_string()));
61+
debug_assert!(default_idents.size_hint() == current_idents.size_hint());
62+
while let (Some(default_ident), Some(current_ident)) =
63+
(default_idents.next(), current_idents.next())
64+
{
65+
let has_name_to_check = |ident: Option<Ident>| {
66+
if let Some(ident) = ident
67+
&& ident.name != kw::Underscore
68+
&& !ident.name.as_str().starts_with('_')
69+
{
70+
Some(ident)
71+
} else {
72+
None
73+
}
74+
};
75+
76+
if let Some(default_ident) = has_name_to_check(default_ident)
77+
&& let Some(current_ident) = has_name_to_check(current_ident)
78+
&& default_ident.name != current_ident.name
79+
{
80+
renamed.push((current_ident.span, default_ident.to_string()));
7081
}
7182
}
7283

@@ -83,14 +94,6 @@ impl RenamedFnArgs {
8394
}
8495
}
8596

86-
fn is_unused_or_empty_symbol(symbol: Symbol) -> bool {
87-
// FIXME: `body_param_names` currently returning empty symbols for `wild` as well,
88-
// so we need to check if the symbol is empty first.
89-
// Therefore the check of whether it's equal to [`kw::Underscore`] has no use for now,
90-
// but it would be nice to keep it here just to be future-proof.
91-
symbol.is_empty() || symbol == kw::Underscore || symbol.as_str().starts_with('_')
92-
}
93-
9497
/// Get the [`trait_item_def_id`](ImplItemRef::trait_item_def_id) of a relevant impl item.
9598
fn trait_item_def_id_of_impl(items: &[ImplItemRef], target: OwnerId) -> Option<DefId> {
9699
items.iter().find_map(|item| {

clippy_lints/src/lifetimes.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ fn check_fn_inner<'tcx>(
189189
cx: &LateContext<'tcx>,
190190
sig: &'tcx FnSig<'_>,
191191
body: Option<BodyId>,
192-
trait_sig: Option<&[Ident]>,
192+
trait_sig: Option<&[Option<Ident>]>,
193193
generics: &'tcx Generics<'_>,
194194
span: Span,
195195
report_extra_lifetimes: bool,
@@ -264,7 +264,7 @@ fn could_use_elision<'tcx>(
264264
cx: &LateContext<'tcx>,
265265
func: &'tcx FnDecl<'_>,
266266
body: Option<BodyId>,
267-
trait_sig: Option<&[Ident]>,
267+
trait_sig: Option<&[Option<Ident>]>,
268268
named_generics: &'tcx [GenericParam<'_>],
269269
msrv: Msrv,
270270
) -> Option<(Vec<LocalDefId>, Vec<Lifetime>)> {
@@ -310,7 +310,7 @@ fn could_use_elision<'tcx>(
310310
let body = cx.tcx.hir_body(body_id);
311311

312312
let first_ident = body.params.first().and_then(|param| param.pat.simple_ident());
313-
if non_elidable_self_type(cx, func, first_ident, msrv) {
313+
if non_elidable_self_type(cx, func, Some(first_ident), msrv) {
314314
return None;
315315
}
316316

@@ -384,8 +384,8 @@ fn allowed_lts_from(named_generics: &[GenericParam<'_>]) -> FxIndexSet<LocalDefI
384384
}
385385

386386
// elision doesn't work for explicit self types before Rust 1.81, see rust-lang/rust#69064
387-
fn non_elidable_self_type<'tcx>(cx: &LateContext<'tcx>, func: &FnDecl<'tcx>, ident: Option<Ident>, msrv: Msrv) -> bool {
388-
if let Some(ident) = ident
387+
fn non_elidable_self_type<'tcx>(cx: &LateContext<'tcx>, func: &FnDecl<'tcx>, ident: Option<Option<Ident>>, msrv: Msrv) -> bool {
388+
if let Some(Some(ident)) = ident
389389
&& ident.name == kw::SelfLower
390390
&& !func.implicit_self.has_implicit_self()
391391
&& let Some(self_ty) = func.inputs.first()

0 commit comments

Comments
 (0)