Skip to content

Commit 231b8ab

Browse files
committed
fix: show no error when parameters match macro names
1 parent a91049e commit 231b8ab

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/tools/rust-analyzer/crates/ide-assists/src/handlers/add_missing_impl_members.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2470,4 +2470,39 @@ impl b::Checker for MyChecker {
24702470
}"#,
24712471
);
24722472
}
2473+
2474+
#[test]
2475+
fn test_parameter_names_matching_macros_not_qualified() {
2476+
check_assist(
2477+
add_missing_impl_members,
2478+
r#"
2479+
trait Foo {
2480+
fn foo(&self, vec: usize);
2481+
fn bar(&self, format: String, panic: bool);
2482+
}
2483+
2484+
struct Bar;
2485+
2486+
impl Foo for Bar {$0}
2487+
"#,
2488+
r#"
2489+
trait Foo {
2490+
fn foo(&self, vec: usize);
2491+
fn bar(&self, format: String, panic: bool);
2492+
}
2493+
2494+
struct Bar;
2495+
2496+
impl Foo for Bar {
2497+
fn foo(&self, vec: usize) {
2498+
${0:todo!()}
2499+
}
2500+
2501+
fn bar(&self, format: String, panic: bool) {
2502+
todo!()
2503+
}
2504+
}
2505+
"#,
2506+
);
2507+
}
24732508
}

src/tools/rust-analyzer/crates/ide-db/src/path_transform.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,14 @@ impl Ctx<'_> {
538538
editor: &mut SyntaxEditor,
539539
ident_pat: &ast::IdentPat,
540540
) -> Option<()> {
541+
// Check if IdentPat is inside a function parameter.
542+
// Parameter names are bindings, not references, thus should not be qualified.
543+
for ancestor in ident_pat.syntax().ancestors() {
544+
if ast::Param::can_cast(ancestor.kind()) {
545+
return None;
546+
}
547+
}
548+
541549
let name = ident_pat.name()?;
542550

543551
let temp_path = make::path_from_text(&name.text());
@@ -546,6 +554,11 @@ impl Ctx<'_> {
546554

547555
match resolution {
548556
hir::PathResolution::Def(def) if def.as_assoc_item(self.source_scope.db).is_none() => {
557+
// Don't qualify macros - they can't be used in pattern position
558+
if matches!(def, hir::ModuleDef::Macro(_)) {
559+
return None;
560+
}
561+
549562
let cfg = FindPathConfig {
550563
prefer_no_std: false,
551564
prefer_prelude: true,

0 commit comments

Comments
 (0)