Auto merge of #17713 - alibektas:17710, r=Veykril

fix: early exit if unresolved field is an index

Fixes #17710
This commit is contained in:
bors 2024-07-27 04:19:09 +00:00
commit 072d7107bc

View file

@ -152,7 +152,12 @@ fn add_field_to_struct_fix(
} else { } else {
Some(make::visibility_pub_crate()) Some(make::visibility_pub_crate())
}; };
let field_name = make::name(field_name);
let field_name = match field_name.chars().next() {
Some(ch) if ch.is_numeric() => return None,
Some(_) => make::name(field_name),
None => return None,
};
let (offset, record_field) = record_field_layout( let (offset, record_field) = record_field_layout(
visibility, visibility,
@ -178,7 +183,12 @@ fn add_field_to_struct_fix(
None => { None => {
// Add a field list to the Unit Struct // Add a field list to the Unit Struct
let mut src_change_builder = SourceChangeBuilder::new(struct_range.file_id); let mut src_change_builder = SourceChangeBuilder::new(struct_range.file_id);
let field_name = make::name(field_name); let field_name = match field_name.chars().next() {
// FIXME : See match arm below regarding tuple structs.
Some(ch) if ch.is_numeric() => return None,
Some(_) => make::name(field_name),
None => return None,
};
let visibility = if error_range.file_id == struct_range.file_id { let visibility = if error_range.file_id == struct_range.file_id {
None None
} else { } else {
@ -274,7 +284,7 @@ mod tests {
use crate::{ use crate::{
tests::{ tests::{
check_diagnostics, check_diagnostics_with_config, check_diagnostics_with_disabled, check_diagnostics, check_diagnostics_with_config, check_diagnostics_with_disabled,
check_fix, check_fix, check_no_fix,
}, },
DiagnosticsConfig, DiagnosticsConfig,
}; };
@ -459,4 +469,36 @@ fn foo() {
"#, "#,
); );
} }
#[test]
fn no_fix_when_indexed() {
check_no_fix(
r#"
struct Kek {}
impl Kek {
pub fn foo(self) {
self.$00
}
}
fn main() {}
"#,
)
}
#[test]
fn no_fix_when_without_field() {
check_no_fix(
r#"
struct Kek {}
impl Kek {
pub fn foo(self) {
self.$0
}
}
fn main() {}
"#,
)
}
} }