about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-09-09 13:55:20 +0000
committerbors <bors@rust-lang.org>2024-09-09 13:55:20 +0000
commit600f7cfb05eff85e9e70ec2e588cbd263e7c756e (patch)
tree43be197b08e51999491a6eb3b67cc46aebda5cdc /src
parentf16a03f3a96d5b82f30f46d979989ef3f529f031 (diff)
parente1653b6d5e39612a7731d209f1eb6b5d5cbe656e (diff)
downloadrust-600f7cfb05eff85e9e70ec2e588cbd263e7c756e.tar.gz
rust-600f7cfb05eff85e9e70ec2e588cbd263e7c756e.zip
Auto merge of #18041 - roife:fix-issue-17631, r=Veykril
feat: better name suggestions for fn

fix #17631.

Better name suggestions for fn-calls / method-calls in the form of `from()`, `from_xxx()`, `into()`, etc.
Diffstat (limited to 'src')
-rw-r--r--src/tools/rust-analyzer/crates/ide-db/src/syntax_helpers/suggest_name.rs96
1 files changed, 95 insertions, 1 deletions
diff --git a/src/tools/rust-analyzer/crates/ide-db/src/syntax_helpers/suggest_name.rs b/src/tools/rust-analyzer/crates/ide-db/src/syntax_helpers/suggest_name.rs
index 6ee526a67ea..e60deb3bf59 100644
--- a/src/tools/rust-analyzer/crates/ide-db/src/syntax_helpers/suggest_name.rs
+++ b/src/tools/rust-analyzer/crates/ide-db/src/syntax_helpers/suggest_name.rs
@@ -18,7 +18,9 @@ const USELESS_TRAITS: &[&str] = &["Send", "Sync", "Copy", "Clone", "Eq", "Partia
 ///
 /// **NOTE**: they all must be snake lower case
 const USELESS_NAMES: &[&str] =
-    &["new", "default", "option", "some", "none", "ok", "err", "str", "string"];
+    &["new", "default", "option", "some", "none", "ok", "err", "str", "string", "from", "into"];
+
+const USELESS_NAME_PREFIXES: &[&str] = &["from_", "with_", "into_"];
 
 /// Generic types replaced by their first argument
 ///
@@ -189,6 +191,10 @@ fn normalize(name: &str) -> Option<String> {
         return None;
     }
 
+    if USELESS_NAME_PREFIXES.iter().any(|prefix| name.starts_with(prefix)) {
+        return None;
+    }
+
     if !is_valid_name(&name) {
         return None;
     }
@@ -831,4 +837,92 @@ fn foo<T>(some_struct: S<T>) { $0some_struct.some_field$0 }
             "some_field",
         );
     }
+
+    #[test]
+    fn from_and_to_func() {
+        check(
+            r#"
+//- minicore: from
+struct Foo;
+struct Bar;
+
+impl From<Foo> for Bar {
+    fn from(_: Foo) -> Self {
+        Bar;
+    }
+}
+
+fn f(_: Bar) {}
+
+fn main() {
+    let foo = Foo {};
+    f($0Bar::from(foo)$0);
+}
+"#,
+            "bar",
+        );
+
+        check(
+            r#"
+//- minicore: from
+struct Foo;
+struct Bar;
+
+impl From<Foo> for Bar {
+    fn from(_: Foo) -> Self {
+        Bar;
+    }
+}
+
+fn f(_: Bar) {}
+
+fn main() {
+    let foo = Foo {};
+    f($0Into::<Bar>::into(foo)$0);
+}
+"#,
+            "bar",
+        );
+    }
+
+    #[test]
+    fn useless_name_prefix() {
+        check(
+            r#"
+struct Foo;
+struct Bar;
+
+impl Bar {
+    fn from_foo(_: Foo) -> Self {
+        Foo {}
+    }
+}
+
+fn main() {
+    let foo = Foo {};
+    let _ = $0Bar::from_foo(foo)$0;
+}
+"#,
+            "bar",
+        );
+
+        check(
+            r#"
+struct Foo;
+struct Bar;
+
+impl Bar {
+    fn with_foo(_: Foo) -> Self {
+        Bar {}
+    }
+}
+
+fn main() {
+    let foo = Foo {};
+    let _ = $0Bar::with_foo(foo)$0;
+}
+"#,
+            "bar",
+        );
+    }
 }