diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index 13696b8f1ba08..5593bbd70676d 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -861,7 +861,7 @@ impl<'hir> LoweringContext<'_, 'hir> { AssocItemKind::Fn(Fn { sig, ident, generics, body: None, define_opaque, .. }) => { // FIXME(contracts): Deny contract here since it won't apply to // any impl method or callees. - let idents = self.lower_fn_params_to_idents(&sig.decl); + let params = self.lower_fn_params(&sig.decl); let (generics, sig) = self.lower_method_sig( generics, sig, @@ -870,8 +870,7 @@ impl<'hir> LoweringContext<'_, 'hir> { FnDeclKind::Trait, sig.header.coroutine_marker, attrs, - // Parameters are discarded for functions without a body - DiscardParams::Yes, + DiscardParams::No, ); if define_opaque.is_some() { self.dcx().span_err( @@ -882,7 +881,7 @@ impl<'hir> LoweringContext<'_, 'hir> { ( *ident, generics, - hir::TraitItemKind::Fn(sig, hir::TraitFn::Required(idents)), + hir::TraitItemKind::Fn(sig, hir::TraitFn::Required(params)), false, ) } diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 2ee73dbc58570..832306f748490 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -3074,7 +3074,7 @@ impl<'hir> TraitItem<'hir> { #[derive(Debug, Clone, Copy, StableHash)] pub enum TraitFn<'hir> { /// No default body in the trait, just a signature. - Required(&'hir [Option]), + Required(&'hir [Param<'hir>]), /// Both signature and body are provided in the trait. Provided(BodyId), diff --git a/compiler/rustc_hir/src/intravisit.rs b/compiler/rustc_hir/src/intravisit.rs index cdb0cc524e6ce..3fc2ed47a6640 100644 --- a/compiler/rustc_hir/src/intravisit.rs +++ b/compiler/rustc_hir/src/intravisit.rs @@ -1309,10 +1309,10 @@ pub fn walk_trait_item<'v, V: Visitor<'v>>( try_visit!(visitor.visit_ty_unambig(ty)); visit_opt!(visitor, visit_const_item_rhs, default); } - TraitItemKind::Fn(ref sig, TraitFn::Required(param_idents)) => { + TraitItemKind::Fn(ref sig, TraitFn::Required(params)) => { try_visit!(visitor.visit_fn_decl(sig.decl)); - for ident in param_idents.iter().copied() { - visit_opt!(visitor, visit_ident, ident); + for param in params { + try_visit!(visitor.visit_param(param)); } } TraitItemKind::Fn(ref sig, TraitFn::Provided(body_id)) => { diff --git a/compiler/rustc_hir_pretty/src/lib.rs b/compiler/rustc_hir_pretty/src/lib.rs index 7758d49d39675..fad2343c19924 100644 --- a/compiler/rustc_hir_pretty/src/lib.rs +++ b/compiler/rustc_hir_pretty/src/lib.rs @@ -20,8 +20,8 @@ use rustc_attr_ir::{AttrArgs, AttrItem, Attribute, AttributeKind, PrintAttribute use rustc_hir as hir; use rustc_hir::{ BindingMode, ByRef, ConstArg, ConstArgExprField, ConstArgKind, GenericArg, GenericBound, - GenericParam, GenericParamKind, HirId, ImplicitSelfKind, LifetimeParamKind, Node, PatKind, - PreciseCapturingArg, RangeEnd, Term, TyFieldPath, TyPatKind, + GenericParam, GenericParamKind, HirId, ImplicitSelfKind, LifetimeParamKind, Node, Param, + PatKind, PreciseCapturingArg, RangeEnd, Term, TyFieldPath, TyPatKind, }; use rustc_span::source_map::SourceMap; use rustc_span::{DUMMY_SP, FileName, Ident, Span, Spanned, Symbol, kw, sym}; @@ -990,10 +990,10 @@ impl<'a> State<'a> { ident: Ident, m: &hir::FnSig<'_>, generics: &hir::Generics<'_>, - arg_idents: &[Option], + params: &[Param<'_>], body_id: Option, ) { - self.print_fn(m.header, Some(ident.name), generics, m.decl, arg_idents, &[], body_id); + self.print_fn(m.header, Some(ident.name), generics, m.decl, &[], params, body_id); } fn print_trait_item(&mut self, ti: &hir::TraitItem<'_>) { @@ -1005,8 +1005,8 @@ impl<'a> State<'a> { hir::TraitItemKind::Const(ty, default) => { self.print_associated_const(ti.ident, ti.generics, ty, default); } - hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Required(arg_idents)) => { - self.print_method_sig(ti.ident, sig, ti.generics, arg_idents, None); + hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Required(params)) => { + self.print_method_sig(ti.ident, sig, ti.generics, params, None); self.word(";"); } hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Provided(body)) => { diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs index 383256184a6a3..930b6b97cbe6f 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs @@ -2142,16 +2142,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { is_method: bool, ) -> Option<(IndexVec, FnParam<'_>)>, &hir::Generics<'_>)> { - let (sig, generics, params, idents) = match self.tcx.hir_get_if_local(def_id)? { + let (sig, generics, params) = match self.tcx.hir_get_if_local(def_id)? { hir::Node::TraitItem(&hir::TraitItem { generics, kind: hir::TraitItemKind::Fn(sig, trait_fn), .. }) => match trait_fn { - hir::TraitFn::Required(params) => (sig, generics, None, Some(params)), - hir::TraitFn::Provided(body) => { - (sig, generics, Some(self.tcx.hir_body(body).params), None) - } + hir::TraitFn::Required(params) => (sig, generics, params), + hir::TraitFn::Provided(body) => (sig, generics, self.tcx.hir_body(body).params), }, hir::Node::ImplItem(&hir::ImplItem { generics, @@ -2161,11 +2159,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | hir::Node::Item(&hir::Item { kind: hir::ItemKind::Fn { sig, generics, body, .. }, .. - }) => (sig, generics, Some(self.tcx.hir_body(body).params), None), + }) => (sig, generics, self.tcx.hir_body(body).params), hir::Node::ForeignItem(&hir::ForeignItem { kind: hir::ForeignItemKind::Fn(sig, params, generics), .. - }) => (sig, generics, Some(params), None), + }) => (sig, generics, params), _ => return None, }; @@ -2186,28 +2184,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { None } }); - let mut params_with_generics: IndexVec = match (params, idents) { - (Some(_), Some(_)) | (None, None) => unreachable!(), - (Some(params), None) => { - let params = params - .get(is_method as usize..params.len() - sig.decl.c_variadic() as usize)?; - debug_assert_eq!(params.len(), fn_inputs.len()); - fn_inputs.zip(params.iter().map(FnParam::Param)).collect() - } - (None, Some(params)) => { - let params = params - .get(is_method as usize..params.len() - sig.decl.c_variadic() as usize)?; - debug_assert_eq!(params.len(), fn_inputs.len()); - fn_inputs.zip(params.iter().map(|&ident| FnParam::Ident(ident))).collect() - } - }; + + let params = + params.get(is_method as usize..params.len() - sig.decl.c_variadic() as usize)?; + debug_assert_eq!(params.len(), fn_inputs.len()); + let mut params_with_generics: IndexVec = + fn_inputs.zip(params.iter().map(FnParam::Param)).collect(); + for param in generics.params { if let hir::GenericParamKind::Const { arg_pos: Some(pos), .. } = param.kind && let Some(idx) = (pos as usize).checked_sub(is_method as usize) { - params_with_generics - .raw - .insert(idx, (None, FnParam::Ident(Some(param.name.ident())))); + params_with_generics.raw.insert(idx, (None, FnParam::GenericParam(param))); } } Some((params_with_generics, generics)) @@ -2237,20 +2225,15 @@ impl<'tcx> Visitor<'tcx> for FindClosureArg<'tcx> { #[derive(Clone, Copy)] enum FnParam<'hir> { Param(&'hir hir::Param<'hir>), - Ident(Option), + /// For function args const generics + GenericParam(&'hir hir::GenericParam<'hir>), } impl FnParam<'_> { fn span(&self) -> Span { match self { Self::Param(param) => param.span, - Self::Ident(ident) => { - if let Some(ident) = ident { - ident.span - } else { - DUMMY_SP - } - } + Self::GenericParam(param) => param.span, } } @@ -2266,8 +2249,8 @@ impl FnParam<'_> { { Some(ident.name) } - FnParam::Ident(ident) - if let Some(ident) = ident + FnParam::GenericParam(param) + if let ident = param.name.ident() && ident.name != kw::Underscore => { Some(ident.name) diff --git a/compiler/rustc_hir_typeck/src/method/suggest.rs b/compiler/rustc_hir_typeck/src/method/suggest.rs index fb0dd0c284ec8..f4338182b3149 100644 --- a/compiler/rustc_hir_typeck/src/method/suggest.rs +++ b/compiler/rustc_hir_typeck/src/method/suggest.rs @@ -4580,23 +4580,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { .. })) = id { - let self_first_arg = match method { - hir::TraitFn::Required([ident, ..]) => { - matches!(ident, Some(Ident { name: kw::SelfLower, .. })) - } + let params = match method { + hir::TraitFn::Required(params) => params, hir::TraitFn::Provided(body_id) => { - self.tcx.hir_body(*body_id).params.first().is_some_and( - |param| { - matches!( - param.pat.kind, - hir::PatKind::Binding(_, _, ident, _) - if ident.name == kw::SelfLower - ) - }, - ) + self.tcx.hir_body(*body_id).params } - _ => false, }; + let self_first_arg = params.first().is_some_and(|param| { + matches!( + param.pat.kind, + hir::PatKind::Binding(_, _, ident, _) if ident.name == kw::SelfLower + ) + }); if !fn_sig.decl.implicit_self().has_implicit_self() && self_first_arg diff --git a/compiler/rustc_lint/src/nonstandard_style.rs b/compiler/rustc_lint/src/nonstandard_style.rs index 2b569ea433313..417d0797c5d7c 100644 --- a/compiler/rustc_lint/src/nonstandard_style.rs +++ b/compiler/rustc_lint/src/nonstandard_style.rs @@ -428,13 +428,8 @@ impl<'tcx> LateLintPass<'tcx> for NonSnakeCase { } fn check_trait_item(&mut self, cx: &LateContext<'_>, item: &hir::TraitItem<'_>) { - if let hir::TraitItemKind::Fn(_, hir::TraitFn::Required(param_idents)) = item.kind { + if let hir::TraitItemKind::Fn(_, hir::TraitFn::Required(_)) = item.kind { self.check_snake_case(cx, "trait method", &item.ident); - for param_ident in param_idents { - if let Some(param_ident) = param_ident { - self.check_snake_case(cx, "variable", param_ident); - } - } } } diff --git a/compiler/rustc_middle/src/hir/mod.rs b/compiler/rustc_middle/src/hir/mod.rs index bc1f9c1180b2b..6a972cf00e66b 100644 --- a/compiler/rustc_middle/src/hir/mod.rs +++ b/compiler/rustc_middle/src/hir/mod.rs @@ -482,15 +482,13 @@ pub fn provide(providers: &mut Providers) { } else if let Node::ForeignItem(&ForeignItem { kind: ForeignItemKind::Fn(_, params, _), .. - }) = node - { - tcx.arena.alloc_from_iter(tcx.param_idents(params)) - } else if let Node::TraitItem(&TraitItem { - kind: TraitItemKind::Fn(_, TraitFn::Required(idents)), + }) + | Node::TraitItem(&TraitItem { + kind: TraitItemKind::Fn(_, TraitFn::Required(params)), .. }) = node { - idents + tcx.arena.alloc_from_iter(tcx.param_idents(params)) } else { span_bug!( tcx.hir_span(tcx.local_def_id_to_hir_id(def_id)), diff --git a/compiler/rustc_trait_selection/src/traits/dyn_compatibility.rs b/compiler/rustc_trait_selection/src/traits/dyn_compatibility.rs index e95e6411e908d..87d9b566537ae 100644 --- a/compiler/rustc_trait_selection/src/traits/dyn_compatibility.rs +++ b/compiler/rustc_trait_selection/src/traits/dyn_compatibility.rs @@ -540,10 +540,10 @@ fn virtual_call_violations_for_method<'tcx>( }; // Get the `Span` for all of `self: Ty`, not just `Ty`. match trait_fn { - hir::TraitFn::Required([Some(name), ..]) - if name.span.ctxt() == sig.decl.inputs[0].span.ctxt() => + hir::TraitFn::Required([param, ..]) + if param.span.ctxt() == sig.decl.inputs[0].span.ctxt() => { - Some(name.span.to(sig.decl.inputs[0].span)) + Some(param.span.to(sig.decl.inputs[0].span)) } hir::TraitFn::Provided(body_id) if let body = tcx.hir_body(*body_id) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 489862459d71a..b33d3d3646571 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -1113,7 +1113,7 @@ fn clean_fn_or_proc_macro<'tcx>( cx, sig, generics, - ParamsSrc::Params(cx.tcx.hir_body(body_id).params), + cx.tcx.hir_body(body_id).params, item.owner_id.to_def_id(), ); let mut func = Function { generics, decl }; @@ -1145,16 +1145,11 @@ fn clean_fn_decl_legacy_const_generics(func: &mut Function, attrs: &[rustc_attr_ } } -enum ParamsSrc<'tcx> { - Idents(&'tcx [Option]), - Params(&'tcx [hir::Param<'tcx>]), -} - fn clean_function<'tcx>( cx: &mut DocContext<'tcx>, sig: &hir::FnSig<'tcx>, generics: &hir::Generics<'tcx>, - params: ParamsSrc<'tcx>, + params: &'tcx [hir::Param<'tcx>], def_id: DefId, ) -> (Generics, FnDecl) { enter_impl_trait(cx, |cx| { @@ -1170,20 +1165,14 @@ fn clean_function<'tcx>( let sig = cx.tcx.fn_sig(def_id).instantiate_identity().skip_norm_wip(); clean_poly_fn_sig(cx, Some(def_id), sig) } else { - let params = match params { - ParamsSrc::Params(body_id) => clean_params_via_body(cx, sig.decl, body_id), - // Let's not perpetuate anon params from Rust 2015; use `_` for them. - ParamsSrc::Idents(idents) => clean_params(cx, sig.decl, idents, |ident| { - Some(ident.map_or(kw::Underscore, |ident| ident.name)) - }), - }; + let params = clean_params(cx, sig.decl, params); clean_fn_decl_with_params(cx, sig.decl, Some(&sig.header), params) }; (generics, decl) }) } -fn clean_params<'tcx>( +fn clean_params_from_idents<'tcx>( cx: &mut DocContext<'tcx>, decl: &hir::FnDecl<'_>, idents: &[Option], @@ -1201,7 +1190,7 @@ fn clean_params<'tcx>( .collect() } -fn clean_params_via_body<'tcx>( +fn clean_params<'tcx>( cx: &mut DocContext<'tcx>, decl: &hir::FnDecl<'tcx>, params: &'tcx [hir::Param<'tcx>], @@ -1312,10 +1301,10 @@ fn clean_trait_item<'tcx>(trait_item: &hir::TraitItem<'tcx>, cx: &mut DocContext hir::TraitItemKind::Fn(ref sig, body) => { let (params, body) = match body { hir::TraitFn::Provided(body) => ( - ParamsSrc::Params(cx.tcx.hir_body(body).params), + cx.tcx.hir_body(body).params, Some(Defaultness::from_trait_item(trait_item.defaultness)), ), - hir::TraitFn::Required(idents) => (ParamsSrc::Idents(idents), None), + hir::TraitFn::Required(params) => (params, None), }; let (generics, decl) = clean_function(cx, sig, trait_item.generics, params, local_did); @@ -1360,7 +1349,7 @@ pub(crate) fn clean_impl_item<'tcx>( cx, sig, impl_.generics, - ParamsSrc::Params(cx.tcx.hir_body(body).params), + cx.tcx.hir_body(body).params, local_did, ); ItemKind::AssocFunction(Box::new(AssocFunction { @@ -2746,7 +2735,7 @@ fn clean_bare_fn_ty<'tcx>( }; let fallback = bare_fn.param_idents.iter().copied().find_map(filter).map(|_| kw::Underscore); - let params = clean_params(cx, bare_fn.decl, bare_fn.param_idents, |ident| { + let params = clean_params_from_idents(cx, bare_fn.decl, bare_fn.param_idents, |ident| { filter(ident).or(fallback) }); let decl = clean_fn_decl_with_params(cx, bare_fn.decl, None, params); @@ -3380,8 +3369,7 @@ fn clean_maybe_renamed_foreign_item<'tcx>( cx.with_param_env(def_id, |cx| { let kind = match item.kind { hir::ForeignItemKind::Fn(sig, params, generics) => { - let (generics, decl) = - clean_function(cx, &sig, generics, ParamsSrc::Params(params), def_id); + let (generics, decl) = clean_function(cx, &sig, generics, params, def_id); ItemKind::ForeignFunction( Box::new(Function { generics, decl }), sig.header.safety(), diff --git a/src/tools/clippy/clippy_lints/src/lifetimes.rs b/src/tools/clippy/clippy_lints/src/lifetimes.rs index 214e68c612cea..97540d97800a7 100644 --- a/src/tools/clippy/clippy_lints/src/lifetimes.rs +++ b/src/tools/clippy/clippy_lints/src/lifetimes.rs @@ -16,7 +16,7 @@ use rustc_hir::intravisit::{ use rustc_hir::{ AmbigArg, BodyId, FnDecl, FnPtrTy, FnSig, GenericArg, GenericArgs, GenericBound, GenericParam, GenericParamKind, Generics, HirId, Impl, ImplItem, ImplItemKind, Item, ItemKind, Lifetime, LifetimeKind, LifetimeParamKind, Node, - PolyTraitRef, PredicateOrigin, TraitFn, TraitItem, TraitItemKind, TraitRef, Ty, TyKind, WhereBoundPredicate, + Param, PolyTraitRef, PredicateOrigin, TraitFn, TraitItem, TraitItemKind, TraitRef, Ty, TyKind, WhereBoundPredicate, WherePredicate, WherePredicateKind, }; use rustc_lint::{LateContext, LateLintPass, LintContext as _, impl_lint_pass}; @@ -24,7 +24,7 @@ use rustc_middle::hir::nested_filter as middle_nested_filter; use rustc_middle::ty::TyCtxt; use rustc_span::Span; use rustc_span::def_id::LocalDefId; -use rustc_span::symbol::{Ident, kw}; +use rustc_span::symbol::kw; use std::ops::ControlFlow; declare_clippy_lint! { @@ -149,7 +149,7 @@ impl<'tcx> LateLintPass<'tcx> for Lifetimes { .. } = item.kind { - check_fn_inner(cx, sig, Some(id), None, generics, item.span, true, self.msrv, || { + check_fn_inner(cx, sig, Some(id), cx.tcx.hir_body(id).params, generics, item.span, true, self.msrv, || { is_from_proc_macro(cx, item) }); } else if let ItemKind::Impl(impl_) = &item.kind @@ -173,7 +173,7 @@ impl<'tcx> LateLintPass<'tcx> for Lifetimes { cx, sig, Some(id), - None, + cx.tcx.hir_body(id).params, item.generics, item.span, report_extra_lifetimes, @@ -185,15 +185,15 @@ impl<'tcx> LateLintPass<'tcx> for Lifetimes { fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx TraitItem<'_>) { if let TraitItemKind::Fn(ref sig, ref body) = item.kind { - let (body, trait_sig) = match *body { - TraitFn::Required(sig) => (None, Some(sig)), - TraitFn::Provided(id) => (Some(id), None), + let (body, params) = match *body { + TraitFn::Required(params) => (None, params), + TraitFn::Provided(id) => (Some(id), cx.tcx.hir_body(id).params), }; check_fn_inner( cx, sig, body, - trait_sig, + params, item.generics, item.span, true, @@ -209,7 +209,7 @@ fn check_fn_inner<'tcx>( cx: &LateContext<'tcx>, sig: &'tcx FnSig<'_>, body: Option, - trait_sig: Option<&[Option]>, + params: &[Param<'tcx>], generics: &'tcx Generics<'_>, span: Span, report_extra_lifetimes: bool, @@ -266,7 +266,7 @@ fn check_fn_inner<'tcx>( } } - let elidable = could_use_elision(cx, sig.decl, body, trait_sig, generics.params, msrv); + let elidable = could_use_elision(cx, sig.decl, body, params, generics.params, msrv); let has_elidable_lts = elidable .as_ref() .is_some_and(|(_, usages)| !usages.iter().any(|usage| usage.ident.span.ctxt() != span.ctxt())); @@ -294,7 +294,7 @@ fn could_use_elision<'tcx>( cx: &LateContext<'tcx>, func: &'tcx FnDecl<'_>, body: Option, - trait_sig: Option<&[Option]>, + params: &[Param<'tcx>], named_generics: &'tcx [GenericParam<'_>], msrv: Msrv, ) -> Option<(Vec, Vec)> { @@ -330,20 +330,13 @@ fn could_use_elision<'tcx>( let input_lts = input_visitor.lts; let output_lts = output_visitor.lts; - if let Some(&[trait_sig]) = trait_sig - && non_elidable_self_type(cx, func, trait_sig, msrv) - { + if non_elidable_self_type(cx, func, params, msrv) { return None; } if let Some(body_id) = body { let body = cx.tcx.hir_body(body_id); - let first_ident = body.params.first().and_then(|param| param.pat.simple_ident()); - if non_elidable_self_type(cx, func, first_ident, msrv) { - return None; - } - let mut checker = BodyLifetimeChecker::new(cx); if checker.visit_expr(body.value).is_break() { return None; @@ -414,9 +407,10 @@ fn allowed_lts_from(named_generics: &[GenericParam<'_>]) -> FxIndexSet(cx: &LateContext<'tcx>, func: &FnDecl<'tcx>, ident: Option, msrv: Msrv) -> bool { - if let Some(ident) = ident - && ident.name == kw::SelfLower +fn non_elidable_self_type<'tcx>(cx: &LateContext<'tcx>, func: &FnDecl<'tcx>, params: &[Param<'tcx>], msrv: Msrv) -> bool { + if let &[first_param, ..] = params + && let Some(first_ident) = first_param.pat.simple_ident() + && first_ident.name == kw::SelfLower && !func.implicit_self().has_implicit_self() && let Some(self_ty) = func.inputs.first() && !msrv.meets(cx, msrvs::EXPLICIT_SELF_TYPE_ELISION) diff --git a/src/tools/clippy/clippy_lints/src/min_ident_chars.rs b/src/tools/clippy/clippy_lints/src/min_ident_chars.rs index ee546528020ac..11ce72e8b7dab 100644 --- a/src/tools/clippy/clippy_lints/src/min_ident_chars.rs +++ b/src/tools/clippy/clippy_lints/src/min_ident_chars.rs @@ -173,15 +173,7 @@ impl LateLintPass<'_> for MinIdentChars { if self.min_chars_threshold == 0 { return; } - if let TraitItemKind::Fn(_, TraitFn::Required(idents)) = i.kind { - for &ident in idents { - if let Some(ident) = ident - && let Some(missing) = self.check_sym(ident.name) - { - self.emit(cx, ident, missing); - } - } - } + if let Some(missing) = self.check_sym(i.ident.name) && !(matches!(i.kind, TraitItemKind::Fn(_, TraitFn::Provided(_))) && cx.tcx.codegen_fn_attrs(i.owner_id.def_id).contains_extern_indicator()) diff --git a/src/tools/clippy/clippy_lints/src/shadow.rs b/src/tools/clippy/clippy_lints/src/shadow.rs index 427cf180adf2c..a1dd6fdfe998f 100644 --- a/src/tools/clippy/clippy_lints/src/shadow.rs +++ b/src/tools/clippy/clippy_lints/src/shadow.rs @@ -139,7 +139,10 @@ impl<'tcx> LateLintPass<'tcx> for Shadow { let HirId { owner, local_id } = id; // get (or insert) the list of items for this owner and symbol - let (ref mut data, scope_owner) = *self.bindings.last_mut().unwrap(); + let Some((data, scope_owner)) = self.bindings.last_mut() else { + // This pattern is part of a `Param` of a body-less function, skip it + return + }; let items_with_name = data.entry(ident.name).or_default(); // check other bindings with the same name, most recently seen first @@ -149,7 +152,7 @@ impl<'tcx> LateLintPass<'tcx> for Shadow { return; } - if is_shadow(cx, scope_owner, prev, local_id) { + if is_shadow(cx, *scope_owner, prev, local_id) { let prev_hir_id = HirId { owner, local_id: prev }; lint_shadow(cx, pat, prev_hir_id, ident.span); // only lint against the "nearest" shadowed binding diff --git a/src/tools/clippy/tests/ui-toml/min_ident_chars_trait_impl/min_ident_chars.stderr b/src/tools/clippy/tests/ui-toml/min_ident_chars_trait_impl/min_ident_chars.stderr index b483a21f37b47..d0b5ac04da8ef 100644 --- a/src/tools/clippy/tests/ui-toml/min_ident_chars_trait_impl/min_ident_chars.stderr +++ b/src/tools/clippy/tests/ui-toml/min_ident_chars_trait_impl/min_ident_chars.stderr @@ -1,17 +1,17 @@ error: this identifier consists of only a single character - --> tests/ui-toml/min_ident_chars_trait_impl/min_ident_chars.rs:4:10 + --> tests/ui-toml/min_ident_chars_trait_impl/min_ident_chars.rs:4:8 | LL | fn f(g: i32); - | ^ + | ^ | = note: `-D clippy::min-ident-chars` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::min_ident_chars)]` error: this identifier consists of only a single character - --> tests/ui-toml/min_ident_chars_trait_impl/min_ident_chars.rs:4:8 + --> tests/ui-toml/min_ident_chars_trait_impl/min_ident_chars.rs:4:10 | LL | fn f(g: i32); - | ^ + | ^ error: this identifier consists of only a single character --> tests/ui-toml/min_ident_chars_trait_impl/min_ident_chars.rs:12:10 diff --git a/src/tools/clippy/tests/ui/min_ident_chars.stderr b/src/tools/clippy/tests/ui/min_ident_chars.stderr index 07082bbc82936..bfa2113d62a24 100644 --- a/src/tools/clippy/tests/ui/min_ident_chars.stderr +++ b/src/tools/clippy/tests/ui/min_ident_chars.stderr @@ -220,16 +220,16 @@ LL | trait D { | ^ error: this identifier consists of only a single character - --> tests/ui/min_ident_chars.rs:151:10 + --> tests/ui/min_ident_chars.rs:151:8 | LL | fn f(g: i32); - | ^ + | ^ error: this identifier consists of only a single character - --> tests/ui/min_ident_chars.rs:151:8 + --> tests/ui/min_ident_chars.rs:151:10 | LL | fn f(g: i32); - | ^ + | ^ error: this identifier consists of only a single character --> tests/ui/min_ident_chars.rs:156:8 @@ -280,16 +280,16 @@ LL | trait A { | ^ error: this identifier consists of only a single character - --> tests/ui/min_ident_chars.rs:180:14 + --> tests/ui/min_ident_chars.rs:180:12 | LL | fn f(h: [u32; 0]); - | ^ + | ^ error: this identifier consists of only a single character - --> tests/ui/min_ident_chars.rs:180:12 + --> tests/ui/min_ident_chars.rs:180:14 | LL | fn f(h: [u32; 0]); - | ^ + | ^ error: this identifier consists of only a single character --> tests/ui/min_ident_chars.rs:192:41 diff --git a/src/tools/clippy/tests/ui/shadow.rs b/src/tools/clippy/tests/ui/shadow.rs index a569469ff44cf..f03deebfca060 100644 --- a/src/tools/clippy/tests/ui/shadow.rs +++ b/src/tools/clippy/tests/ui/shadow.rs @@ -204,4 +204,16 @@ fn tuple_unrelated((x,): (u8,)) { //~^ shadow_unrelated } +unsafe extern "C" { + fn foreign(x: u8); +} + +pub trait Trait { + fn required_method(x: u8); + fn provided_method(x: u8) { + let x = 1u8; + //~^ shadow_unrelated + } +} + fn main() {} diff --git a/src/tools/clippy/tests/ui/shadow.stderr b/src/tools/clippy/tests/ui/shadow.stderr index c19aacab2e99e..d94b1ad07480b 100644 --- a/src/tools/clippy/tests/ui/shadow.stderr +++ b/src/tools/clippy/tests/ui/shadow.stderr @@ -388,5 +388,17 @@ note: previous binding is here LL | fn tuple_unrelated((x,): (u8,)) { | ^ -error: aborting due to 32 previous errors +error: `x` shadows a previous, unrelated binding + --> tests/ui/shadow.rs:214:13 + | +LL | let x = 1u8; + | ^ + | +note: previous binding is here + --> tests/ui/shadow.rs:213:24 + | +LL | fn provided_method(x: u8) { + | ^ + +error: aborting due to 33 previous errors diff --git a/tests/pretty/hir-fn-params.pp b/tests/pretty/hir-fn-params.pp index 15373bba24d3f..94842bb65e3ac 100644 --- a/tests/pretty/hir-fn-params.pp +++ b/tests/pretty/hir-fn-params.pp @@ -33,5 +33,5 @@ } trait T { - fn trait_fn(u32, _: u32, a: u32); + fn trait_fn(_: u32, _: u32, a: u32); } diff --git a/tests/ui/const-generics/function_arg_const_generics/typeck-errors.stderr b/tests/ui/const-generics/function_arg_const_generics/typeck-errors.stderr index 7ee6c024a7b07..2dfa7902c59da 100644 --- a/tests/ui/const-generics/function_arg_const_generics/typeck-errors.stderr +++ b/tests/ui/const-generics/function_arg_const_generics/typeck-errors.stderr @@ -54,7 +54,7 @@ note: function defined here --> $DIR/typeck-errors.rs:3:4 | LL | fn foo(const N: usize) -> [u8; N] { - | ^^^ - + | ^^^ -------------- error: aborting due to 5 previous errors diff --git a/tests/ui/delegation/glob-glob-conflict.stderr b/tests/ui/delegation/glob-glob-conflict.stderr index f5629bb14e9a9..e1f5ea6cd0245 100644 --- a/tests/ui/delegation/glob-glob-conflict.stderr +++ b/tests/ui/delegation/glob-glob-conflict.stderr @@ -35,7 +35,7 @@ note: method defined here | LL | trait Trait2 { LL | fn method(&self) -> u8; - | ^^^^^^ ---- + | ^^^^^^ ----- help: provide the argument | LL | reuse Trait2::*(/* value */); @@ -61,7 +61,7 @@ note: method defined here | LL | trait Trait1 { LL | fn method(&self) -> u8; - | ^^^^^^ ---- + | ^^^^^^ ----- help: provide the argument | LL | reuse Trait1::*(/* value */); diff --git a/tests/ui/delegation/self-mapping-arguments-errors.stderr b/tests/ui/delegation/self-mapping-arguments-errors.stderr index 032561ddd8816..8807283249057 100644 --- a/tests/ui/delegation/self-mapping-arguments-errors.stderr +++ b/tests/ui/delegation/self-mapping-arguments-errors.stderr @@ -59,7 +59,7 @@ note: method defined here | LL | trait MyAdd { LL | fn add(self, other: Self) -> Self; - | ^^^ ----- + | ^^^ ----------- help: provide the argument | LL ~ }({ @@ -112,7 +112,7 @@ note: method defined here | LL | trait MyAdd { LL | fn add(self, other: Box) -> Self; - | ^^^ ----- + | ^^^ ---------------- note: method provided through this `impl` --> $DIR/self-mapping-arguments-errors.rs:37:5 | diff --git a/tests/ui/delegation/self-mapping-output.stderr b/tests/ui/delegation/self-mapping-output.stderr index 90155fa62e1aa..f7c17fc76696c 100644 --- a/tests/ui/delegation/self-mapping-output.stderr +++ b/tests/ui/delegation/self-mapping-output.stderr @@ -145,7 +145,7 @@ note: method defined here | LL | trait Trait { LL | fn method(&self) -> (); - | ^^^^^^ ---- + | ^^^^^^ ----- help: consider borrowing here | LL | reuse Trait::method { &self.0 } @@ -204,7 +204,7 @@ note: method defined here | LL | trait Trait { LL | fn method(&self) -> (); - | ^^^^^^ ---- + | ^^^^^^ ----- help: consider borrowing here | LL | reuse Trait::method { &self.0 } @@ -264,7 +264,7 @@ note: method defined here | LL | trait Trait { LL | fn method(&self) -> Self; - | ^^^^^^ ---- + | ^^^^^^ ----- note: method provided through this `impl` --> $DIR/self-mapping-output.rs:242:5 | @@ -322,7 +322,7 @@ note: method defined here | LL | trait Trait { LL | fn method(&self) -> Self; - | ^^^^^^ ---- + | ^^^^^^ ----- note: method provided through this `impl` --> $DIR/self-mapping-output.rs:242:5 | @@ -380,7 +380,7 @@ note: method defined here | LL | trait Trait { LL | fn method(&self) -> Self; - | ^^^^^^ ---- + | ^^^^^^ ----- error[E0308]: mismatched types --> $DIR/self-mapping-output.rs:308:31 @@ -398,7 +398,7 @@ note: method defined here | LL | trait Trait { LL | fn method(&self) -> Self; - | ^^^^^^ ---- + | ^^^^^^ ----- error[E0616]: field `0` of struct `private::W` is private --> $DIR/self-mapping-output.rs:334:36 diff --git a/tests/ui/fn/param-mismatch-trait-fn.stderr b/tests/ui/fn/param-mismatch-trait-fn.stderr index b96c83e1bd6f4..5c5c6089f3d4d 100644 --- a/tests/ui/fn/param-mismatch-trait-fn.stderr +++ b/tests/ui/fn/param-mismatch-trait-fn.stderr @@ -14,7 +14,7 @@ note: associated function defined here | LL | trait Foo { LL | fn same_type(_: T, _: T); - | ^^^^^^^^^ - - - this parameter needs to match the `[X; 1]` type of parameter #1 + | ^^^^^^^^^ - ---- ---- this parameter needs to match the `[X; 1]` type of parameter #1 | | | | | parameter #2 needs to match the `[X; 1]` type of this parameter | parameter #1 and parameter #2 both reference this parameter `T` diff --git a/tests/ui/higher-ranked/relate-bound-region-ice-144033.stderr b/tests/ui/higher-ranked/relate-bound-region-ice-144033.stderr index 6f6c2c4feb33f..f175d99b1dcda 100644 --- a/tests/ui/higher-ranked/relate-bound-region-ice-144033.stderr +++ b/tests/ui/higher-ranked/relate-bound-region-ice-144033.stderr @@ -46,7 +46,7 @@ note: method defined here | LL | trait FooMut { LL | fn bar(self, _: I) - | ^^^ - + | ^^^ ---- error: aborting due to 2 previous errors diff --git a/tests/ui/lint/non-snake-case/lint-non-snake-case-functions.rs b/tests/ui/lint/non-snake-case/lint-non-snake-case-functions.rs index fa64a9f980e75..e08c01d1492a4 100644 --- a/tests/ui/lint/non-snake-case/lint-non-snake-case-functions.rs +++ b/tests/ui/lint/non-snake-case/lint-non-snake-case-functions.rs @@ -16,6 +16,9 @@ impl Foo { fn render_HTML() {} //~^ ERROR method `render_HTML` should have a snake case name + + fn param(NOT_SNAKE: usize) {} + //~^ ERROR variable `NOT_SNAKE` should have a snake case name } trait X { @@ -27,12 +30,21 @@ trait X { fn something__else(&mut self); //~^ ERROR trait method `something__else` should have a snake case name + + fn param1(NOT_SNAKE: usize); + //~^ ERROR variable `NOT_SNAKE` should have a snake case name + + fn param2(NOT_SNAKE: usize) {} + //~^ ERROR variable `NOT_SNAKE` should have a snake case name } impl X for Foo { // These errors should be caught at the trait definition not the impl fn ABC() {} fn something__else(&mut self) {} + + fn param1(NOT_SNAKE: usize) {} + //~^ ERROR variable `NOT_SNAKE` should have a snake case name } fn Cookie() {} diff --git a/tests/ui/lint/non-snake-case/lint-non-snake-case-functions.stderr b/tests/ui/lint/non-snake-case/lint-non-snake-case-functions.stderr index f6ac6b99b602f..575405108068e 100644 --- a/tests/ui/lint/non-snake-case/lint-non-snake-case-functions.stderr +++ b/tests/ui/lint/non-snake-case/lint-non-snake-case-functions.stderr @@ -28,35 +28,59 @@ error: method `render_HTML` should have a snake case name LL | fn render_HTML() {} | ^^^^^^^^^^^ help: convert the identifier to snake case: `render_html` +error: variable `NOT_SNAKE` should have a snake case name + --> $DIR/lint-non-snake-case-functions.rs:20:14 + | +LL | fn param(NOT_SNAKE: usize) {} + | ^^^^^^^^^ help: convert the identifier to snake case: `not_snake` + error: trait method `ABC` should have a snake case name - --> $DIR/lint-non-snake-case-functions.rs:22:8 + --> $DIR/lint-non-snake-case-functions.rs:25:8 | LL | fn ABC(); | ^^^ help: convert the identifier to snake case: `abc` error: trait method `a_b_C` should have a snake case name - --> $DIR/lint-non-snake-case-functions.rs:25:8 + --> $DIR/lint-non-snake-case-functions.rs:28:8 | LL | fn a_b_C(&self) {} | ^^^^^ help: convert the identifier to snake case (notice the capitalization): `a_b_c` error: trait method `something__else` should have a snake case name - --> $DIR/lint-non-snake-case-functions.rs:28:8 + --> $DIR/lint-non-snake-case-functions.rs:31:8 | LL | fn something__else(&mut self); | ^^^^^^^^^^^^^^^ help: convert the identifier to snake case: `something_else` +error: variable `NOT_SNAKE` should have a snake case name + --> $DIR/lint-non-snake-case-functions.rs:34:15 + | +LL | fn param1(NOT_SNAKE: usize); + | ^^^^^^^^^ help: convert the identifier to snake case: `not_snake` + +error: variable `NOT_SNAKE` should have a snake case name + --> $DIR/lint-non-snake-case-functions.rs:37:15 + | +LL | fn param2(NOT_SNAKE: usize) {} + | ^^^^^^^^^ help: convert the identifier to snake case: `not_snake` + +error: variable `NOT_SNAKE` should have a snake case name + --> $DIR/lint-non-snake-case-functions.rs:46:15 + | +LL | fn param1(NOT_SNAKE: usize) {} + | ^^^^^^^^^ help: convert the identifier to snake case: `not_snake` + error: function `Cookie` should have a snake case name - --> $DIR/lint-non-snake-case-functions.rs:38:4 + --> $DIR/lint-non-snake-case-functions.rs:50:4 | LL | fn Cookie() {} | ^^^^^^ help: convert the identifier to snake case (notice the capitalization): `cookie` error: function `bi_S_Cuit` should have a snake case name - --> $DIR/lint-non-snake-case-functions.rs:41:8 + --> $DIR/lint-non-snake-case-functions.rs:53:8 | LL | pub fn bi_S_Cuit() {} | ^^^^^^^^^ help: convert the identifier to snake case (notice the capitalization): `bi_s_cuit` -error: aborting due to 9 previous errors +error: aborting due to 13 previous errors diff --git a/tests/ui/methods/issues/issue-61525.stderr b/tests/ui/methods/issues/issue-61525.stderr index 3a3e103098ccf..d1c5e8adfcc9e 100644 --- a/tests/ui/methods/issues/issue-61525.stderr +++ b/tests/ui/methods/issues/issue-61525.stderr @@ -34,7 +34,7 @@ note: method defined here | LL | pub trait Example { LL | fn query(self, q: Q); - | ^^^^^ - + | ^^^^^ ---- error: aborting due to 2 previous errors diff --git a/tests/ui/stats/input-stats.stderr b/tests/ui/stats/input-stats.stderr index e4f23db345c88..ee4b59598ff0c 100644 --- a/tests/ui/stats/input-stats.stderr +++ b/tests/ui/stats/input-stats.stderr @@ -86,11 +86,11 @@ hir-stats Ty 720 (NN.N%) 15 48 hir-stats - Ptr 48 (NN.N%) 1 hir-stats - Ref 48 (NN.N%) 1 hir-stats - Path 624 (NN.N%) 13 -hir-stats Generics 560 (NN.N%) 10 56 -hir-stats Pat 480 (NN.N%) 6 80 +hir-stats Pat 560 (NN.N%) 7 80 hir-stats - Struct 80 (NN.N%) 1 hir-stats - Wild 80 (NN.N%) 1 -hir-stats - Binding 320 (NN.N%) 4 +hir-stats - Binding 400 (NN.N%) 5 +hir-stats Generics 560 (NN.N%) 10 56 hir-stats GenericParam 400 (NN.N%) 5 80 hir-stats Block 288 (NN.N%) 6 48 hir-stats GenericBound 256 (NN.N%) 4 64 @@ -99,12 +99,12 @@ hir-stats Attribute 160 (NN.N%) 4 40 hir-stats Variant 144 (NN.N%) 2 72 hir-stats GenericArgs 144 (NN.N%) 3 48 hir-stats FieldDef 144 (NN.N%) 2 72 +hir-stats Param 128 (NN.N%) 4 32 hir-stats FnDecl 120 (NN.N%) 3 40 hir-stats Stmt 96 (NN.N%) 3 32 hir-stats - Expr 32 (NN.N%) 1 hir-stats - Let 32 (NN.N%) 1 hir-stats - Semi 32 (NN.N%) 1 -hir-stats Param 96 (NN.N%) 3 32 hir-stats Arm 80 (NN.N%) 2 40 hir-stats WherePredicate 72 (NN.N%) 3 24 hir-stats - BoundPredicate 72 (NN.N%) 3 @@ -121,5 +121,5 @@ hir-stats TraitItemId 8 (NN.N%) 2 4 hir-stats ImplItemId 8 (NN.N%) 2 4 hir-stats ForeignItemId 4 (NN.N%) 1 4 hir-stats ---------------------------------------------------------------- -hir-stats Total 8_704 174 +hir-stats Total 8_816 176 hir-stats ================================================================ diff --git a/tests/ui/suggestions/trait-with-missing-associated-type-restriction.stderr b/tests/ui/suggestions/trait-with-missing-associated-type-restriction.stderr index 196eafa5bba68..71befa7023745 100644 --- a/tests/ui/suggestions/trait-with-missing-associated-type-restriction.stderr +++ b/tests/ui/suggestions/trait-with-missing-associated-type-restriction.stderr @@ -100,7 +100,7 @@ note: method defined here LL | trait Trait { ... LL | fn funk(&self, _: Self::A); - | ^^^^ - + | ^^^^ ---------- help: consider constraining the associated type `>::A` to `{integer}` | LL | fn bar2>(x: T) { diff --git a/tests/ui/traits/issue-52893.stderr b/tests/ui/traits/issue-52893.stderr index 565a9fbc788af..6ce265a8c2a18 100644 --- a/tests/ui/traits/issue-52893.stderr +++ b/tests/ui/traits/issue-52893.stderr @@ -24,7 +24,7 @@ note: method defined here LL | trait Push { LL | type PushRes; LL | fn push(self, other: T) -> Self::PushRes; - | ^^^^ ----- + | ^^^^ -------- error: aborting due to 1 previous error