Unnamed repository; edit this file 'description' to name the repository.
early exit if unresolved field is an index
Ali Bektas 2024-07-27
parent aae54dd · commit 480db31
-rw-r--r--crates/ide-diagnostics/src/handlers/unresolved_field.rs48
1 files changed, 45 insertions, 3 deletions
diff --git a/crates/ide-diagnostics/src/handlers/unresolved_field.rs b/crates/ide-diagnostics/src/handlers/unresolved_field.rs
index 467d4edbf2..eb8eea69f6 100644
--- a/crates/ide-diagnostics/src/handlers/unresolved_field.rs
+++ b/crates/ide-diagnostics/src/handlers/unresolved_field.rs
@@ -152,7 +152,12 @@ fn add_field_to_struct_fix(
} else {
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(
visibility,
@@ -178,7 +183,12 @@ fn add_field_to_struct_fix(
None => {
// Add a field list to the Unit Struct
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 {
None
} else {
@@ -274,7 +284,7 @@ mod tests {
use crate::{
tests::{
check_diagnostics, check_diagnostics_with_config, check_diagnostics_with_disabled,
- check_fix,
+ check_fix, check_no_fix,
},
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() {}
+ "#,
+ )
+ }
}