Skip to content

Commit 1b783b0

Browse files
committed
Auto merge of rust-lang#17713 - alibektas:17710, r=Veykril
fix: early exit if unresolved field is an index Fixes rust-lang#17710
2 parents 13f0a02 + a15fbe6 commit 1b783b0

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/unresolved_field.rs

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,12 @@ fn add_field_to_struct_fix(
152152
} else {
153153
Some(make::visibility_pub_crate())
154154
};
155-
let field_name = make::name(field_name);
155+
156+
let field_name = match field_name.chars().next() {
157+
Some(ch) if ch.is_numeric() => return None,
158+
Some(_) => make::name(field_name),
159+
None => return None,
160+
};
156161

157162
let (offset, record_field) = record_field_layout(
158163
visibility,
@@ -178,7 +183,12 @@ fn add_field_to_struct_fix(
178183
None => {
179184
// Add a field list to the Unit Struct
180185
let mut src_change_builder = SourceChangeBuilder::new(struct_range.file_id);
181-
let field_name = make::name(field_name);
186+
let field_name = match field_name.chars().next() {
187+
// FIXME : See match arm below regarding tuple structs.
188+
Some(ch) if ch.is_numeric() => return None,
189+
Some(_) => make::name(field_name),
190+
None => return None,
191+
};
182192
let visibility = if error_range.file_id == struct_range.file_id {
183193
None
184194
} else {
@@ -274,7 +284,7 @@ mod tests {
274284
use crate::{
275285
tests::{
276286
check_diagnostics, check_diagnostics_with_config, check_diagnostics_with_disabled,
277-
check_fix,
287+
check_fix, check_no_fix,
278288
},
279289
DiagnosticsConfig,
280290
};
@@ -459,4 +469,36 @@ fn foo() {
459469
"#,
460470
);
461471
}
472+
473+
#[test]
474+
fn no_fix_when_indexed() {
475+
check_no_fix(
476+
r#"
477+
struct Kek {}
478+
impl Kek {
479+
pub fn foo(self) {
480+
self.$00
481+
}
482+
}
483+
484+
fn main() {}
485+
"#,
486+
)
487+
}
488+
489+
#[test]
490+
fn no_fix_when_without_field() {
491+
check_no_fix(
492+
r#"
493+
struct Kek {}
494+
impl Kek {
495+
pub fn foo(self) {
496+
self.$0
497+
}
498+
}
499+
500+
fn main() {}
501+
"#,
502+
)
503+
}
462504
}

0 commit comments

Comments
 (0)