From 456b3c7ba824a52153bc3dd9ae82b4ffe123852c Mon Sep 17 00:00:00 2001 From: annieversary Date: Sun, 22 Nov 2026 08:36:50 +0000 Subject: [PATCH 1/2] Add lint against duplicate parameter names in fns without body * add t/u/lint/duplicated-parameter-names.rs * warn on duplicate parameter names in trait and extern functions * add check_generics_args to EarlyLintPass `walk_generic_args` exists in `ast_visit`, so i'm assuming it was forgotten in `EarlyLintPass` * warn on duplicate parameter names in parenthesized generic args * warn on duplicate parameter names in function pointers * update tests for duplicated_parameter_names * test that duplicated-parameter-names does not warn on patterns patterns are already disallowed on both functions without body, fn pts, and parenthesized generic arguments, so there should be no warning on them * use normalize_to_macro_rules rather than name for idents * rename type in test * add missing compile_fail to duplicate_parameter_names * apply requested changes from review * fix ci and use param.is_self() * add test case for duplicated underscore not linting --- compiler/rustc_lint/src/builtin.rs | 98 +++++++++++-- compiler/rustc_lint/src/diagnostics.rs | 11 ++ compiler/rustc_lint/src/early.rs | 5 + compiler/rustc_lint/src/lib.rs | 1 + compiler/rustc_lint/src/passes.rs | 1 + .../core_arch/src/aarch64/neon/generated.rs | 2 +- .../spec/neon/aarch64.spec.yml | 4 +- tests/ui/abi/numbers-arithmetic/i128-ffi.rs | 2 +- tests/ui/fn/fn-ptr-pattern.rs | 2 + tests/ui/fn/fn-ptr-pattern.stderr | 68 +++++---- tests/ui/fn/named-fn-trait-parameters.rs | 2 + tests/ui/fn/named-fn-trait-parameters.stderr | 76 ++++++---- tests/ui/lint/duplicate-parameter-names.rs | 63 +++++++++ .../ui/lint/duplicate-parameter-names.stderr | 132 ++++++++++++++++++ .../resolve/name-collision-in-trait-fn-sig.rs | 11 -- 15 files changed, 400 insertions(+), 78 deletions(-) create mode 100644 tests/ui/lint/duplicate-parameter-names.rs create mode 100644 tests/ui/lint/duplicate-parameter-names.stderr delete mode 100644 tests/ui/resolve/name-collision-in-trait-fn-sig.rs diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index 3b335aaa55f7c..2f17c810c7403 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -24,6 +24,7 @@ use rustc_ast_pretty::pprust::expr_to_string; use rustc_attr_ir::lang_items::LangItem; use rustc_attr_ir::{AttributeKind, DocAttribute, find_attr}; use rustc_attr_parsing::AttributeParser; +use rustc_data_structures::fx::FxHashMap; use rustc_errors::{Applicability, Diagnostic, msg}; use rustc_feature::GateIssue; use rustc_hir::def::{DefKind, Res}; @@ -48,15 +49,16 @@ use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _ use crate::diagnostics::{ BuiltinAnonymousParams, BuiltinConstNoMangle, BuiltinDerefNullptr, BuiltinDoubleNegations, - BuiltinDoubleNegationsAddParens, BuiltinEllipsisInclusiveRangePatterns, - BuiltinEllipsisInclusiveRangePatternsLint, BuiltinExplicitOutlives, - BuiltinExplicitOutlivesSuggestion, BuiltinFeatureIssueNote, BuiltinIncompleteFeatures, - BuiltinIncompleteFeaturesHelp, BuiltinInternalFeatures, BuiltinKeywordIdents, - BuiltinMissingCopyImpl, BuiltinMissingDebugImpl, BuiltinMissingDoc, BuiltinMutablesTransmutes, - BuiltinNonShorthandFieldPatterns, BuiltinSpecialModuleNameUsed, BuiltinTrivialBounds, - BuiltinTypeAliasBounds, BuiltinUngatedAsyncFnTrackCaller, BuiltinUnpermittedTypeInit, - BuiltinUnpermittedTypeInitSub, BuiltinUnreachablePub, BuiltinUnsafe, BuiltinUnstableFeatures, - BuiltinWhileTrue, EqInternalMethodImplemented, InvalidAsmLabel, + BuiltinDoubleNegationsAddParens, BuiltinDuplicateParameterNames, + BuiltinEllipsisInclusiveRangePatterns, BuiltinEllipsisInclusiveRangePatternsLint, + BuiltinExplicitOutlives, BuiltinExplicitOutlivesSuggestion, BuiltinFeatureIssueNote, + BuiltinIncompleteFeatures, BuiltinIncompleteFeaturesHelp, BuiltinInternalFeatures, + BuiltinKeywordIdents, BuiltinMissingCopyImpl, BuiltinMissingDebugImpl, BuiltinMissingDoc, + BuiltinMutablesTransmutes, BuiltinNonShorthandFieldPatterns, BuiltinSpecialModuleNameUsed, + BuiltinTrivialBounds, BuiltinTypeAliasBounds, BuiltinUngatedAsyncFnTrackCaller, + BuiltinUnpermittedTypeInit, BuiltinUnpermittedTypeInitSub, BuiltinUnreachablePub, + BuiltinUnsafe, BuiltinUnstableFeatures, BuiltinWhileTrue, EqInternalMethodImplemented, + InvalidAsmLabel, }; use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext}; @@ -737,6 +739,84 @@ impl EarlyLintPass for AnonymousParameters { } } +declare_lint! { + /// The `duplicate_parameter_names` lint detects duplicate parameter names + /// in functions without bodies. + /// + /// ### Example + /// + /// ```rust,compile_fail + /// #![deny(duplicate_parameter_names)] + /// pub trait Foo { + /// fn foo(x: usize, x: usize); + /// } + /// fn main() {} + /// ``` + /// + /// {{produces}} + /// + /// ### Explanation + /// + /// Duplicate parameter names are allowed in body-less functions, + /// in function pointers, and in parenthesised generic args, but this + /// is confusing and inconsistent behaviour, and is discouraged. + pub DUPLICATE_PARAMETER_NAMES, + Warn, + "detects duplicate parameter names in functions without bodies", +} + +declare_lint_pass!( + /// Checks for duplicate parameter names + DuplicateParameterNames => [DUPLICATE_PARAMETER_NAMES] +); + +fn find_duplicated_parameters(cx: &EarlyContext<'_>, params: &[ast::Param]) { + let mut map = FxHashMap::default(); + + for param in params { + if let ast::PatKind::Ident(_, ident, _) = param.pat.kind { + if param.is_self() { + continue; + } + + let name = ident.normalize_to_macro_rules(); + + if let Some(&prev_use) = map.get(&name) { + cx.emit_span_lint( + DUPLICATE_PARAMETER_NAMES, + ident.span, + BuiltinDuplicateParameterNames { span: ident.span, ident, prev_use }, + ); + } else { + map.insert(name, ident.span); + } + } + } +} + +impl EarlyLintPass for DuplicateParameterNames { + fn check_fn(&mut self, cx: &EarlyContext<'_>, fk: FnKind<'_>, _: Span, _: ast::NodeId) { + if let FnKind::Fn(_, _, Fn { sig, body, .. }) = fk { + // if there's a body, an error is already thrown, so we shouldn't warn + if body.is_none() { + find_duplicated_parameters(cx, &sig.decl.inputs); + } + } + } + + fn check_ty(&mut self, cx: &EarlyContext<'_>, ty: &ast::Ty) { + if let TyKind::FnPtr(f) = &ty.kind { + find_duplicated_parameters(cx, &f.decl.inputs); + } + } + + fn check_generic_args(&mut self, cx: &EarlyContext<'_>, args: &ast::GenericArgs) { + if let ast::GenericArgs::Parenthesized(parens) = args { + find_duplicated_parameters(cx, &parens.inputs); + } + } +} + declare_lint! { /// The `no_mangle_const_items` lint detects any `const` items with the /// [`no_mangle` attribute]. diff --git a/compiler/rustc_lint/src/diagnostics.rs b/compiler/rustc_lint/src/diagnostics.rs index 69cdee19a56fb..05af781dab003 100644 --- a/compiler/rustc_lint/src/diagnostics.rs +++ b/compiler/rustc_lint/src/diagnostics.rs @@ -312,6 +312,17 @@ pub(crate) struct BuiltinAnonymousParams<'a> { pub ty_snip: &'a str, } +#[derive(Diagnostic)] +#[diag("identifier `{$ident}` is bound more than once in this parameter list")] +pub(crate) struct BuiltinDuplicateParameterNames { + #[primary_span] + #[label("used as parameter more than once")] + pub(crate) span: Span, + pub(crate) ident: Ident, + #[label("used here already")] + pub(crate) prev_use: Span, +} + #[derive(Diagnostic)] #[diag("const items should never be `#[no_mangle]`")] pub(crate) struct BuiltinConstNoMangle { diff --git a/compiler/rustc_lint/src/early.rs b/compiler/rustc_lint/src/early.rs index d55ea5553d2df..565838cec1844 100644 --- a/compiler/rustc_lint/src/early.rs +++ b/compiler/rustc_lint/src/early.rs @@ -181,6 +181,11 @@ impl<'ast, 'ecx, T: EarlyLintPass> ast_visit::Visitor<'ast> for EarlyContextAndP ast_visit::walk_generic_arg(self, arg); } + fn visit_generic_args(&mut self, args: &'ast ast::GenericArgs) { + lint_callback!(self, check_generic_args, args); + ast_visit::walk_generic_args(self, args); + } + fn visit_generic_param(&mut self, param: &'ast ast::GenericParam) { self.with_lint_attrs(param.id, ¶m.attrs, |cx| { lint_callback!(cx, check_generic_param, param); diff --git a/compiler/rustc_lint/src/lib.rs b/compiler/rustc_lint/src/lib.rs index 2c3365028844a..a0e2b4841bdf6 100644 --- a/compiler/rustc_lint/src/lib.rs +++ b/compiler/rustc_lint/src/lib.rs @@ -184,6 +184,7 @@ early_lint_methods!( // tidy-alphabetical-start AnonymousParameters: AnonymousParameters, DoubleNegations: DoubleNegations, + DuplicateParameterNames: DuplicateParameterNames, EllipsisInclusiveRangePatterns: EllipsisInclusiveRangePatterns::default(), Expr2024: Expr2024, IncompleteInternalFeatures: IncompleteInternalFeatures, diff --git a/compiler/rustc_lint/src/passes.rs b/compiler/rustc_lint/src/passes.rs index 98ab44c37e26a..fa9b2c4f67af7 100644 --- a/compiler/rustc_lint/src/passes.rs +++ b/compiler/rustc_lint/src/passes.rs @@ -148,6 +148,7 @@ macro_rules! early_lint_methods { fn check_expr_post(a: &rustc_ast::Expr); fn check_ty(a: &rustc_ast::Ty); fn check_generic_arg(a: &rustc_ast::GenericArg); + fn check_generic_args(a: &rustc_ast::GenericArgs); fn check_generic_param(a: &rustc_ast::GenericParam); fn check_generics(a: &rustc_ast::Generics); fn check_poly_trait_ref(a: &rustc_ast::PolyTraitRef); diff --git a/library/stdarch/crates/core_arch/src/aarch64/neon/generated.rs b/library/stdarch/crates/core_arch/src/aarch64/neon/generated.rs index 72f65d0d14344..62f634172945d 100644 --- a/library/stdarch/crates/core_arch/src/aarch64/neon/generated.rs +++ b/library/stdarch/crates/core_arch/src/aarch64/neon/generated.rs @@ -12063,7 +12063,7 @@ pub unsafe fn vluti4q_lane_s16_x2(a: int16x8x2_t, b: uint8x8_t) any(target_arch = "aarch64", target_arch = "arm64ec"), link_name = "llvm.aarch64.neon.vluti4q.lane.x2.v8i16" )] - fn _vluti4q_lane_s16_x2(a: int16x8_t, a: int16x8_t, b: uint8x8_t, n: i32) -> int16x8_t; + fn _vluti4q_lane_s16_x2(a1: int16x8_t, a2: int16x8_t, b: uint8x8_t, n: i32) -> int16x8_t; } _vluti4q_lane_s16_x2(a.0, a.1, b, LANE) } diff --git a/library/stdarch/crates/stdarch-gen-arm/spec/neon/aarch64.spec.yml b/library/stdarch/crates/stdarch-gen-arm/spec/neon/aarch64.spec.yml index 4cb43ee580ecf..2b853d46d42d7 100644 --- a/library/stdarch/crates/stdarch-gen-arm/spec/neon/aarch64.spec.yml +++ b/library/stdarch/crates/stdarch-gen-arm/spec/neon/aarch64.spec.yml @@ -14059,8 +14059,8 @@ intrinsics: - LLVMLink: name: "vluti4q_lane_{neon_type[0]}_x2" arguments: - - 'a: {neon_type[2]}' - - 'a: {neon_type[2]}' + - 'a1: {neon_type[2]}' + - 'a2: {neon_type[2]}' - 'b: {neon_type[1]}' - 'n: i32' links: diff --git a/tests/ui/abi/numbers-arithmetic/i128-ffi.rs b/tests/ui/abi/numbers-arithmetic/i128-ffi.rs index 26d65e8bbf853..2248e9a75b04b 100644 --- a/tests/ui/abi/numbers-arithmetic/i128-ffi.rs +++ b/tests/ui/abi/numbers-arithmetic/i128-ffi.rs @@ -12,7 +12,7 @@ extern "C" { fn identity(f: u128) -> u128; fn square(f: i128) -> i128; - fn sub(f: i128, f: i128) -> i128; + fn sub(f1: i128, f2: i128) -> i128; } fn main() { diff --git a/tests/ui/fn/fn-ptr-pattern.rs b/tests/ui/fn/fn-ptr-pattern.rs index 465fa9f9e20b5..ff78c9ac36055 100644 --- a/tests/ui/fn/fn-ptr-pattern.rs +++ b/tests/ui/fn/fn-ptr-pattern.rs @@ -5,6 +5,7 @@ fn allowed( f3: fn(String, msg: String), f4: fn(msg: String, String), f5: fn(duplicate_name: bool, duplicate_name: bool), + //~^ WARN identifier `duplicate_name` is bound more than once in this parameter list ) { } @@ -46,6 +47,7 @@ fn semantics( variadics: extern "C" fn(...), duplicate_names: fn(x: usize, x: usize), + //~^ WARN identifier `x` is bound more than once in this parameter list ) { } // Patterns are also syntactically rejected, but restricted patterns are not diff --git a/tests/ui/fn/fn-ptr-pattern.stderr b/tests/ui/fn/fn-ptr-pattern.stderr index bf99fd06cf184..78c3b96c0327a 100644 --- a/tests/ui/fn/fn-ptr-pattern.stderr +++ b/tests/ui/fn/fn-ptr-pattern.stderr @@ -1,5 +1,5 @@ error[E0642]: patterns aren't allowed in function pointer types - --> $DIR/fn-ptr-pattern.rs:13:14 + --> $DIR/fn-ptr-pattern.rs:14:14 | LL | pat1: fn(1..3: bool), | ^^^^ @@ -11,7 +11,7 @@ LL + pat1: fn(_: bool), | error[E0642]: patterns aren't allowed in function pointer types - --> $DIR/fn-ptr-pattern.rs:15:14 + --> $DIR/fn-ptr-pattern.rs:16:14 | LL | pat2: fn((x, y): (bool, bool)), | ^^^^^^ @@ -23,7 +23,7 @@ LL + pat2: fn(_: (bool, bool)), | error[E0642]: patterns aren't allowed in function pointer types - --> $DIR/fn-ptr-pattern.rs:17:14 + --> $DIR/fn-ptr-pattern.rs:18:14 | LL | pat3: fn(Thing { a, b }: Thing), | ^^^^^^^^^^^^^^ @@ -35,7 +35,7 @@ LL + pat3: fn(_: Thing), | error[E0642]: patterns aren't allowed in function pointer types - --> $DIR/fn-ptr-pattern.rs:19:14 + --> $DIR/fn-ptr-pattern.rs:20:14 | LL | pat4: fn(NoThing { a, b }: NoThing), | ^^^^^^^^^^^^^^^^ @@ -47,7 +47,7 @@ LL + pat4: fn(_: NoThing), | error[E0642]: patterns aren't allowed in function pointer types - --> $DIR/fn-ptr-pattern.rs:22:14 + --> $DIR/fn-ptr-pattern.rs:23:14 | LL | pat5: fn((((((x))))): bool), | ^^^^^^^^^^^ @@ -59,19 +59,19 @@ LL + pat5: fn(_: bool), | error: unexpected `self` parameter in function - --> $DIR/fn-ptr-pattern.rs:27:21 + --> $DIR/fn-ptr-pattern.rs:28:21 | LL | self2: fn(self, self), | ^^^^ must be the first parameter of an associated function error: unexpected `self` parameter in function - --> $DIR/fn-ptr-pattern.rs:30:21 + --> $DIR/fn-ptr-pattern.rs:31:21 | LL | self3: fn(bool, self), | ^^^^ must be the first parameter of an associated function error[E0642]: patterns aren't allowed in function pointer types - --> $DIR/fn-ptr-pattern.rs:54:14 + --> $DIR/fn-ptr-pattern.rs:56:14 | LL | pat1: fn(1..3: bool), | ^^^^ @@ -83,7 +83,7 @@ LL + pat1: fn(_: bool), | error[E0642]: patterns aren't allowed in function pointer types - --> $DIR/fn-ptr-pattern.rs:56:14 + --> $DIR/fn-ptr-pattern.rs:58:14 | LL | pat2: fn((x, y): (bool, bool)), | ^^^^^^ @@ -95,7 +95,7 @@ LL + pat2: fn(_: (bool, bool)), | error[E0642]: patterns aren't allowed in function pointer types - --> $DIR/fn-ptr-pattern.rs:58:14 + --> $DIR/fn-ptr-pattern.rs:60:14 | LL | pat3: fn(Thing { a, b }: Thing), | ^^^^^^^^^^^^^^ @@ -107,7 +107,7 @@ LL + pat3: fn(_: Thing), | error[E0642]: patterns aren't allowed in function pointer types - --> $DIR/fn-ptr-pattern.rs:60:14 + --> $DIR/fn-ptr-pattern.rs:62:14 | LL | pat4: fn(NoThing { a, b }: NoThing), | ^^^^^^^^^^^^^^^^ @@ -119,7 +119,7 @@ LL + pat4: fn(_: NoThing), | error[E0642]: patterns aren't allowed in function pointer types - --> $DIR/fn-ptr-pattern.rs:62:14 + --> $DIR/fn-ptr-pattern.rs:64:14 | LL | pat5: fn((((((x))))): bool), | ^^^^^^^^^^^ @@ -131,19 +131,19 @@ LL + pat5: fn(_: bool), | error: unexpected `self` parameter in function - --> $DIR/fn-ptr-pattern.rs:66:21 + --> $DIR/fn-ptr-pattern.rs:68:21 | LL | self2: fn(self, self), | ^^^^ must be the first parameter of an associated function error: unexpected `self` parameter in function - --> $DIR/fn-ptr-pattern.rs:68:21 + --> $DIR/fn-ptr-pattern.rs:70:21 | LL | self3: fn(bool, self), | ^^^^ must be the first parameter of an associated function error: `self` parameter is only allowed in associated functions - --> $DIR/fn-ptr-pattern.rs:25:15 + --> $DIR/fn-ptr-pattern.rs:26:15 | LL | self1: fn(self), | ^^^^ not semantically valid as function parameter @@ -151,7 +151,7 @@ LL | self1: fn(self), = note: associated functions are those in `impl` or `trait` definitions error: `self` parameter is only allowed in associated functions - --> $DIR/fn-ptr-pattern.rs:27:15 + --> $DIR/fn-ptr-pattern.rs:28:15 | LL | self2: fn(self, self), | ^^^^ not semantically valid as function parameter @@ -159,49 +159,49 @@ LL | self2: fn(self, self), = note: associated functions are those in `impl` or `trait` definitions error[E0561]: patterns aren't allowed in function pointer types - --> $DIR/fn-ptr-pattern.rs:33:25 + --> $DIR/fn-ptr-pattern.rs:34:25 | LL | restricted_pat1: fn(mut x: ()), | ^^^^^ error[E0561]: patterns aren't allowed in function pointer types - --> $DIR/fn-ptr-pattern.rs:35:25 + --> $DIR/fn-ptr-pattern.rs:36:25 | LL | restricted_pat2: fn(&x: ()), | ^^ error[E0561]: patterns aren't allowed in function pointer types - --> $DIR/fn-ptr-pattern.rs:37:25 + --> $DIR/fn-ptr-pattern.rs:38:25 | LL | restricted_pat3: fn(&&x: ()), | ^^^ error[E0561]: patterns aren't allowed in function pointer types - --> $DIR/fn-ptr-pattern.rs:39:25 + --> $DIR/fn-ptr-pattern.rs:40:25 | LL | restricted_pat4: fn(false: ()), | ^^^^^ error[E0561]: patterns aren't allowed in function pointer types - --> $DIR/fn-ptr-pattern.rs:41:25 + --> $DIR/fn-ptr-pattern.rs:42:25 | LL | restricted_pat5: fn(&_: ()), | ^^ error[E0561]: patterns aren't allowed in function pointer types - --> $DIR/fn-ptr-pattern.rs:43:25 + --> $DIR/fn-ptr-pattern.rs:44:25 | LL | restricted_pat6: fn(&true: ()), | ^^^^^ error[E0425]: cannot find type `NoThing` in this scope - --> $DIR/fn-ptr-pattern.rs:19:32 + --> $DIR/fn-ptr-pattern.rs:20:32 | LL | pat4: fn(NoThing { a, b }: NoThing), | ^^^^^^^ not found in this scope | note: similarly named struct `Thing` defined here - --> $DIR/fn-ptr-pattern.rs:81:1 + --> $DIR/fn-ptr-pattern.rs:83:1 | LL | struct Thing { a: bool, b: bool } | ^^^^^^^^^^^^ @@ -211,7 +211,25 @@ LL - pat4: fn(NoThing { a, b }: NoThing), LL + pat4: fn(NoThing { a, b }: Thing), | -error: aborting due to 23 previous errors +warning: identifier `duplicate_name` is bound more than once in this parameter list + --> $DIR/fn-ptr-pattern.rs:7:34 + | +LL | f5: fn(duplicate_name: bool, duplicate_name: bool), + | -------------- ^^^^^^^^^^^^^^ used as parameter more than once + | | + | used here already + | + = note: `#[warn(duplicate_parameter_names)]` on by default + +warning: identifier `x` is bound more than once in this parameter list + --> $DIR/fn-ptr-pattern.rs:49:35 + | +LL | duplicate_names: fn(x: usize, x: usize), + | - ^ used as parameter more than once + | | + | used here already + +error: aborting due to 23 previous errors; 2 warnings emitted Some errors have detailed explanations: E0425, E0561, E0642. For more information about an error, try `rustc --explain E0425`. diff --git a/tests/ui/fn/named-fn-trait-parameters.rs b/tests/ui/fn/named-fn-trait-parameters.rs index fd9ce5559fe01..0e81b1339c011 100644 --- a/tests/ui/fn/named-fn-trait-parameters.rs +++ b/tests/ui/fn/named-fn-trait-parameters.rs @@ -7,6 +7,7 @@ fn allowed( f3: impl Fn(String, msg: String), f4: impl Fn(msg: String, String), f5: impl Fn(duplicate_name: bool, duplicate_name: bool), + //~^ WARN identifier `duplicate_name` is bound more than once in this parameter list fg: F ) where F: Fn(msg: String) { } @@ -57,6 +58,7 @@ fn semantics( //~^ ERROR C-variadic parameters aren't allowed in parenthesized argument lists duplicate_names: impl Fn(x: usize, x: usize), + //~^ WARN identifier `x` is bound more than once in this parameter list ) { } // Patterns are also syntactically rejected, but restricted patterns are not diff --git a/tests/ui/fn/named-fn-trait-parameters.stderr b/tests/ui/fn/named-fn-trait-parameters.stderr index dfdf4cd6c3fb4..ba01a3e64b299 100644 --- a/tests/ui/fn/named-fn-trait-parameters.stderr +++ b/tests/ui/fn/named-fn-trait-parameters.stderr @@ -1,5 +1,5 @@ error[E0642]: patterns aren't allowed in parenthesized argument list - --> $DIR/named-fn-trait-parameters.rs:17:19 + --> $DIR/named-fn-trait-parameters.rs:18:19 | LL | pat1: impl Fn(1..3: bool), | ^^^^ @@ -11,7 +11,7 @@ LL + pat1: impl Fn(_: bool), | error[E0642]: patterns aren't allowed in parenthesized argument list - --> $DIR/named-fn-trait-parameters.rs:19:19 + --> $DIR/named-fn-trait-parameters.rs:20:19 | LL | pat2: impl Fn((x, y): (bool, bool)), | ^^^^^^ @@ -23,7 +23,7 @@ LL + pat2: impl Fn(_: (bool, bool)), | error[E0642]: patterns aren't allowed in parenthesized argument list - --> $DIR/named-fn-trait-parameters.rs:21:19 + --> $DIR/named-fn-trait-parameters.rs:22:19 | LL | pat3: impl Fn(Thing { a, b }: Thing), | ^^^^^^^^^^^^^^ @@ -35,7 +35,7 @@ LL + pat3: impl Fn(_: Thing), | error[E0642]: patterns aren't allowed in parenthesized argument list - --> $DIR/named-fn-trait-parameters.rs:23:19 + --> $DIR/named-fn-trait-parameters.rs:24:19 | LL | pat4: impl Fn(NoThing { a, b }: NoThing), | ^^^^^^^^^^^^^^^^ @@ -47,7 +47,7 @@ LL + pat4: impl Fn(_: NoThing), | error[E0642]: patterns aren't allowed in parenthesized argument list - --> $DIR/named-fn-trait-parameters.rs:26:19 + --> $DIR/named-fn-trait-parameters.rs:27:19 | LL | pat5: impl Fn((((((x))))): bool), | ^^^^^^^^^^^ @@ -59,19 +59,19 @@ LL + pat5: impl Fn(_: bool), | error: unexpected `self` parameter in function - --> $DIR/named-fn-trait-parameters.rs:31:26 + --> $DIR/named-fn-trait-parameters.rs:32:26 | LL | self2: impl Fn(self, self), | ^^^^ must be the first parameter of an associated function error: unexpected `self` parameter in function - --> $DIR/named-fn-trait-parameters.rs:34:26 + --> $DIR/named-fn-trait-parameters.rs:35:26 | LL | self3: impl Fn(bool, self), | ^^^^ must be the first parameter of an associated function error[E0642]: patterns aren't allowed in parenthesized argument list - --> $DIR/named-fn-trait-parameters.rs:65:19 + --> $DIR/named-fn-trait-parameters.rs:67:19 | LL | pat1: impl Fn(1..3: bool), | ^^^^ @@ -83,7 +83,7 @@ LL + pat1: impl Fn(_: bool), | error[E0642]: patterns aren't allowed in parenthesized argument list - --> $DIR/named-fn-trait-parameters.rs:67:19 + --> $DIR/named-fn-trait-parameters.rs:69:19 | LL | pat2: impl Fn((x, y): (bool, bool)), | ^^^^^^ @@ -95,7 +95,7 @@ LL + pat2: impl Fn(_: (bool, bool)), | error[E0642]: patterns aren't allowed in parenthesized argument list - --> $DIR/named-fn-trait-parameters.rs:69:19 + --> $DIR/named-fn-trait-parameters.rs:71:19 | LL | pat3: impl Fn(Thing { a, b }: Thing), | ^^^^^^^^^^^^^^ @@ -107,7 +107,7 @@ LL + pat3: impl Fn(_: Thing), | error[E0642]: patterns aren't allowed in parenthesized argument list - --> $DIR/named-fn-trait-parameters.rs:71:19 + --> $DIR/named-fn-trait-parameters.rs:73:19 | LL | pat4: impl Fn(NoThing { a, b }: NoThing), | ^^^^^^^^^^^^^^^^ @@ -119,7 +119,7 @@ LL + pat4: impl Fn(_: NoThing), | error[E0642]: patterns aren't allowed in parenthesized argument list - --> $DIR/named-fn-trait-parameters.rs:73:19 + --> $DIR/named-fn-trait-parameters.rs:75:19 | LL | pat5: impl Fn((((((x))))): bool), | ^^^^^^^^^^^ @@ -131,19 +131,19 @@ LL + pat5: impl Fn(_: bool), | error: unexpected `self` parameter in function - --> $DIR/named-fn-trait-parameters.rs:77:26 + --> $DIR/named-fn-trait-parameters.rs:79:26 | LL | self2: impl Fn(self, self), | ^^^^ must be the first parameter of an associated function error: unexpected `self` parameter in function - --> $DIR/named-fn-trait-parameters.rs:79:26 + --> $DIR/named-fn-trait-parameters.rs:81:26 | LL | self3: impl Fn(bool, self), | ^^^^ must be the first parameter of an associated function error: `self` parameter is only allowed in associated functions - --> $DIR/named-fn-trait-parameters.rs:29:20 + --> $DIR/named-fn-trait-parameters.rs:30:20 | LL | self1: impl Fn(self), | ^^^^ not semantically valid as function parameter @@ -151,7 +151,7 @@ LL | self1: impl Fn(self), = note: associated functions are those in `impl` or `trait` definitions error: `self` parameter is only allowed in associated functions - --> $DIR/named-fn-trait-parameters.rs:31:20 + --> $DIR/named-fn-trait-parameters.rs:32:20 | LL | self2: impl Fn(self, self), | ^^^^ not semantically valid as function parameter @@ -159,43 +159,43 @@ LL | self2: impl Fn(self, self), = note: associated functions are those in `impl` or `trait` definitions error[E0561]: patterns aren't allowed in parenthesized argument lists - --> $DIR/named-fn-trait-parameters.rs:37:30 + --> $DIR/named-fn-trait-parameters.rs:38:30 | LL | restricted_pat1: impl Fn(mut x: ()), | ^^^^^ error[E0561]: patterns aren't allowed in parenthesized argument lists - --> $DIR/named-fn-trait-parameters.rs:39:30 + --> $DIR/named-fn-trait-parameters.rs:40:30 | LL | restricted_pat2: impl Fn(&x: ()), | ^^ error[E0561]: patterns aren't allowed in parenthesized argument lists - --> $DIR/named-fn-trait-parameters.rs:41:30 + --> $DIR/named-fn-trait-parameters.rs:42:30 | LL | restricted_pat3: impl Fn(&&x: ()), | ^^^ error[E0561]: patterns aren't allowed in parenthesized argument lists - --> $DIR/named-fn-trait-parameters.rs:43:30 + --> $DIR/named-fn-trait-parameters.rs:44:30 | LL | restricted_pat4: impl Fn(false: ()), | ^^^^^ error[E0561]: patterns aren't allowed in parenthesized argument lists - --> $DIR/named-fn-trait-parameters.rs:45:30 + --> $DIR/named-fn-trait-parameters.rs:46:30 | LL | restricted_pat5: impl Fn(&_: ()), | ^^ error[E0561]: patterns aren't allowed in parenthesized argument lists - --> $DIR/named-fn-trait-parameters.rs:47:30 + --> $DIR/named-fn-trait-parameters.rs:48:30 | LL | restricted_pat6: impl Fn(&true: ()), | ^^^^^ error: C-variadic parameters aren't allowed in parenthesized argument lists - --> $DIR/named-fn-trait-parameters.rs:50:29 + --> $DIR/named-fn-trait-parameters.rs:51:29 | LL | variadics1: impl Fn(a1: ...), | ----^^^ @@ -203,7 +203,7 @@ LL | variadics1: impl Fn(a1: ...), | help: remove the C-variadics error: C-variadic parameters aren't allowed in parenthesized argument lists - --> $DIR/named-fn-trait-parameters.rs:52:29 + --> $DIR/named-fn-trait-parameters.rs:53:29 | LL | variadics2: impl Fn(a1: ...), | ----^^^ @@ -211,7 +211,7 @@ LL | variadics2: impl Fn(a1: ...), | help: remove the C-variadics error: C-variadic parameters aren't allowed in parenthesized argument lists - --> $DIR/named-fn-trait-parameters.rs:54:40 + --> $DIR/named-fn-trait-parameters.rs:55:40 | LL | variadics3: impl Fn(a1: usize, a2: ...), | ----^^^ @@ -219,19 +219,19 @@ LL | variadics3: impl Fn(a1: usize, a2: ...), | help: remove the C-variadics error: C-variadic parameters aren't allowed in parenthesized argument lists - --> $DIR/named-fn-trait-parameters.rs:56:29 + --> $DIR/named-fn-trait-parameters.rs:57:29 | LL | variadics4: impl Fn(a1: ..., a2: usize), | ----^^^-- help: remove the C-variadics error[E0425]: cannot find type `NoThing` in this scope - --> $DIR/named-fn-trait-parameters.rs:23:37 + --> $DIR/named-fn-trait-parameters.rs:24:37 | LL | pat4: impl Fn(NoThing { a, b }: NoThing), | ^^^^^^^ not found in this scope | note: similarly named struct `Thing` defined here - --> $DIR/named-fn-trait-parameters.rs:93:1 + --> $DIR/named-fn-trait-parameters.rs:95:1 | LL | struct Thing { a: bool, b: bool } | ^^^^^^^^^^^^ @@ -241,7 +241,25 @@ LL - pat4: impl Fn(NoThing { a, b }: NoThing), LL + pat4: impl Fn(NoThing { a, b }: Thing), | -error: aborting due to 27 previous errors +warning: identifier `duplicate_name` is bound more than once in this parameter list + --> $DIR/named-fn-trait-parameters.rs:9:39 + | +LL | f5: impl Fn(duplicate_name: bool, duplicate_name: bool), + | -------------- ^^^^^^^^^^^^^^ used as parameter more than once + | | + | used here already + | + = note: `#[warn(duplicate_parameter_names)]` on by default + +warning: identifier `x` is bound more than once in this parameter list + --> $DIR/named-fn-trait-parameters.rs:60:40 + | +LL | duplicate_names: impl Fn(x: usize, x: usize), + | - ^ used as parameter more than once + | | + | used here already + +error: aborting due to 27 previous errors; 2 warnings emitted Some errors have detailed explanations: E0425, E0561, E0642. For more information about an error, try `rustc --explain E0425`. diff --git a/tests/ui/lint/duplicate-parameter-names.rs b/tests/ui/lint/duplicate-parameter-names.rs new file mode 100644 index 0000000000000..422fa8d5caa94 --- /dev/null +++ b/tests/ui/lint/duplicate-parameter-names.rs @@ -0,0 +1,63 @@ +// Functions without bodies, function pointers, and parenthesized generic args +// should lint against parameters with duplicate names + +#![allow(unused)] +#![feature(named_fn_trait_parameters)] + +trait Test { + fn trait_fn(x: usize, x: usize); + //~^ WARN identifier `x` is bound more than once in this parameter list + + // Functions with a body should hard error, and should not show a warning + fn fn_with_body(x: usize, x: u64) {} + //~^ ERROR identifier `x` is bound more than once in this parameter list + + fn three_args(x: usize, x: usize, x: u8); + //~^ WARN identifier `x` is bound more than once in this parameter list + //~| WARN identifier `x` is bound more than once in this parameter list + + fn underscore(_: bool, _: bool); +} + +unsafe extern "C" { + fn extern_fn(x: usize, x: usize); + //~^ WARN identifier `x` is bound more than once in this parameter list +} + +type FnPointer = fn(x: usize, x: u64); +//~^ WARN identifier `x` is bound more than once in this parameter list + +fn generic_type() +//~^ WARN identifier `x` is bound more than once in this parameter list + where U: FnOnce(x: usize, x: usize) +//~^ WARN identifier `x` is bound more than once in this parameter list + {} + +fn impl_trait(f: impl FnOnce(x: usize, x: usize)) {} +//~^ WARN identifier `x` is bound more than once in this parameter list + +fn main() {} + +// Functions with a body already hard error if they have duplicated parameter names, +// so they shouldn't warn on top of the error +fn fn_with_body(x: usize, x: u64) {} +//~^ ERROR identifier `x` is bound more than once in this parameter list + +// Patterns aren't allowed, so they shouldn't show a warning on top of the error +trait Error { + fn pattern_in_args((x, x): usize, x: impl Fn((a, a): (bool, bool)), f: fn((b, b): (u64, u64))); + //~^ ERROR patterns aren't allowed in methods without bodies + //~| ERROR patterns aren't allowed in function pointer types + //~| ERROR patterns aren't allowed in parenthesized argument list +} + +// Free fns throw an error, but it's unrelated so we do warn +fn free_fn_without_body(x: u8, x: u8); +//~^ ERROR free function without a body +//~| WARN identifier `x` is bound more than once in this parameter list + +// The warning should respect hygiene +macro_rules! decl { + ($p:ident) => { trait T { fn decl(x: u8, $p: u8); } }; +} +decl!(x); diff --git a/tests/ui/lint/duplicate-parameter-names.stderr b/tests/ui/lint/duplicate-parameter-names.stderr new file mode 100644 index 0000000000000..87128ec15c3f4 --- /dev/null +++ b/tests/ui/lint/duplicate-parameter-names.stderr @@ -0,0 +1,132 @@ +error[E0642]: patterns aren't allowed in methods without bodies + --> $DIR/duplicate-parameter-names.rs:48:24 + | +LL | fn pattern_in_args((x, x): usize, x: impl Fn((a, a): (bool, bool)), f: fn((b, b): (u64, u64))); + | ^^^^^^ + | +help: give this argument a name or use an underscore to ignore it + | +LL - fn pattern_in_args((x, x): usize, x: impl Fn((a, a): (bool, bool)), f: fn((b, b): (u64, u64))); +LL + fn pattern_in_args(_: usize, x: impl Fn((a, a): (bool, bool)), f: fn((b, b): (u64, u64))); + | + +error[E0642]: patterns aren't allowed in parenthesized argument list + --> $DIR/duplicate-parameter-names.rs:48:50 + | +LL | fn pattern_in_args((x, x): usize, x: impl Fn((a, a): (bool, bool)), f: fn((b, b): (u64, u64))); + | ^^^^^^ + | +help: give this argument a name or use an underscore to ignore it + | +LL - fn pattern_in_args((x, x): usize, x: impl Fn((a, a): (bool, bool)), f: fn((b, b): (u64, u64))); +LL + fn pattern_in_args((x, x): usize, x: impl Fn(_: (bool, bool)), f: fn((b, b): (u64, u64))); + | + +error[E0642]: patterns aren't allowed in function pointer types + --> $DIR/duplicate-parameter-names.rs:48:79 + | +LL | fn pattern_in_args((x, x): usize, x: impl Fn((a, a): (bool, bool)), f: fn((b, b): (u64, u64))); + | ^^^^^^ + | +help: give this argument a name or use an underscore to ignore it + | +LL - fn pattern_in_args((x, x): usize, x: impl Fn((a, a): (bool, bool)), f: fn((b, b): (u64, u64))); +LL + fn pattern_in_args((x, x): usize, x: impl Fn((a, a): (bool, bool)), f: fn(_: (u64, u64))); + | + +error: free function without a body + --> $DIR/duplicate-parameter-names.rs:55:1 + | +LL | fn free_fn_without_body(x: u8, x: u8); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- + | | + | help: provide a definition for the function: `{ }` + +error[E0415]: identifier `x` is bound more than once in this parameter list + --> $DIR/duplicate-parameter-names.rs:12:31 + | +LL | fn fn_with_body(x: usize, x: u64) {} + | ^ used as parameter more than once + +error[E0415]: identifier `x` is bound more than once in this parameter list + --> $DIR/duplicate-parameter-names.rs:43:27 + | +LL | fn fn_with_body(x: usize, x: u64) {} + | ^ used as parameter more than once + +warning: identifier `x` is bound more than once in this parameter list + --> $DIR/duplicate-parameter-names.rs:8:27 + | +LL | fn trait_fn(x: usize, x: usize); + | - ^ used as parameter more than once + | | + | used here already + | + = note: `#[warn(duplicate_parameter_names)]` on by default + +warning: identifier `x` is bound more than once in this parameter list + --> $DIR/duplicate-parameter-names.rs:15:29 + | +LL | fn three_args(x: usize, x: usize, x: u8); + | - ^ used as parameter more than once + | | + | used here already + +warning: identifier `x` is bound more than once in this parameter list + --> $DIR/duplicate-parameter-names.rs:15:39 + | +LL | fn three_args(x: usize, x: usize, x: u8); + | - used here already ^ used as parameter more than once + +warning: identifier `x` is bound more than once in this parameter list + --> $DIR/duplicate-parameter-names.rs:23:28 + | +LL | fn extern_fn(x: usize, x: usize); + | - ^ used as parameter more than once + | | + | used here already + +warning: identifier `x` is bound more than once in this parameter list + --> $DIR/duplicate-parameter-names.rs:27:31 + | +LL | type FnPointer = fn(x: usize, x: u64); + | - ^ used as parameter more than once + | | + | used here already + +warning: identifier `x` is bound more than once in this parameter list + --> $DIR/duplicate-parameter-names.rs:30:33 + | +LL | fn generic_type() + | - ^ used as parameter more than once + | | + | used here already + +warning: identifier `x` is bound more than once in this parameter list + --> $DIR/duplicate-parameter-names.rs:32:31 + | +LL | where U: FnOnce(x: usize, x: usize) + | - ^ used as parameter more than once + | | + | used here already + +warning: identifier `x` is bound more than once in this parameter list + --> $DIR/duplicate-parameter-names.rs:36:40 + | +LL | fn impl_trait(f: impl FnOnce(x: usize, x: usize)) {} + | - ^ used as parameter more than once + | | + | used here already + +warning: identifier `x` is bound more than once in this parameter list + --> $DIR/duplicate-parameter-names.rs:55:32 + | +LL | fn free_fn_without_body(x: u8, x: u8); + | - ^ used as parameter more than once + | | + | used here already + +error: aborting due to 6 previous errors; 9 warnings emitted + +Some errors have detailed explanations: E0415, E0642. +For more information about an error, try `rustc --explain E0415`. diff --git a/tests/ui/resolve/name-collision-in-trait-fn-sig.rs b/tests/ui/resolve/name-collision-in-trait-fn-sig.rs deleted file mode 100644 index f405ab5c59dd5..0000000000000 --- a/tests/ui/resolve/name-collision-in-trait-fn-sig.rs +++ /dev/null @@ -1,11 +0,0 @@ -//@ check-pass -// This is currently stable behavior, which was almost accidentally made an -// error in #102161 since there is no test exercising it. I am not sure if -// this _should_ be the desired behavior, but at least we should know if it -// changes. - -fn main() {} - -trait Foo { - fn fn_with_type_named_same_as_local_in_param(b: i32, b: i32); -} From 2bd564d211cac582110cd8752500228c76aa294f Mon Sep 17 00:00:00 2001 From: Jonathan Brouwer Date: Sun, 22 Nov 2026 11:57:35 +0100 Subject: [PATCH 2/2] Add bootstrap gate to the lint doctest --- compiler/rustc_lint/src/builtin.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index 2f17c810c7403..d552b6c4ac2c9 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -745,13 +745,14 @@ declare_lint! { /// /// ### Example /// - /// ```rust,compile_fail + #[cfg_attr(bootstrap, doc = "```rust,skip")] + #[cfg_attr(not(bootstrap), doc = "```rust,compile_fail")] /// #![deny(duplicate_parameter_names)] /// pub trait Foo { /// fn foo(x: usize, x: usize); /// } /// fn main() {} - /// ``` + #[doc = "```"] /// /// {{produces}} ///