Skip to content

Commit 600f7cf

Browse files
committed
Auto merge of rust-lang#18041 - roife:fix-issue-17631, r=Veykril
feat: better name suggestions for fn fix rust-lang#17631. Better name suggestions for fn-calls / method-calls in the form of `from()`, `from_xxx()`, `into()`, etc.
2 parents f16a03f + e1653b6 commit 600f7cf

File tree

1 file changed

+95
-1
lines changed

1 file changed

+95
-1
lines changed

src/tools/rust-analyzer/crates/ide-db/src/syntax_helpers/suggest_name.rs

+95-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ const USELESS_TRAITS: &[&str] = &["Send", "Sync", "Copy", "Clone", "Eq", "Partia
1818
///
1919
/// **NOTE**: they all must be snake lower case
2020
const USELESS_NAMES: &[&str] =
21-
&["new", "default", "option", "some", "none", "ok", "err", "str", "string"];
21+
&["new", "default", "option", "some", "none", "ok", "err", "str", "string", "from", "into"];
22+
23+
const USELESS_NAME_PREFIXES: &[&str] = &["from_", "with_", "into_"];
2224

2325
/// Generic types replaced by their first argument
2426
///
@@ -189,6 +191,10 @@ fn normalize(name: &str) -> Option<String> {
189191
return None;
190192
}
191193

194+
if USELESS_NAME_PREFIXES.iter().any(|prefix| name.starts_with(prefix)) {
195+
return None;
196+
}
197+
192198
if !is_valid_name(&name) {
193199
return None;
194200
}
@@ -831,4 +837,92 @@ fn foo<T>(some_struct: S<T>) { $0some_struct.some_field$0 }
831837
"some_field",
832838
);
833839
}
840+
841+
#[test]
842+
fn from_and_to_func() {
843+
check(
844+
r#"
845+
//- minicore: from
846+
struct Foo;
847+
struct Bar;
848+
849+
impl From<Foo> for Bar {
850+
fn from(_: Foo) -> Self {
851+
Bar;
852+
}
853+
}
854+
855+
fn f(_: Bar) {}
856+
857+
fn main() {
858+
let foo = Foo {};
859+
f($0Bar::from(foo)$0);
860+
}
861+
"#,
862+
"bar",
863+
);
864+
865+
check(
866+
r#"
867+
//- minicore: from
868+
struct Foo;
869+
struct Bar;
870+
871+
impl From<Foo> for Bar {
872+
fn from(_: Foo) -> Self {
873+
Bar;
874+
}
875+
}
876+
877+
fn f(_: Bar) {}
878+
879+
fn main() {
880+
let foo = Foo {};
881+
f($0Into::<Bar>::into(foo)$0);
882+
}
883+
"#,
884+
"bar",
885+
);
886+
}
887+
888+
#[test]
889+
fn useless_name_prefix() {
890+
check(
891+
r#"
892+
struct Foo;
893+
struct Bar;
894+
895+
impl Bar {
896+
fn from_foo(_: Foo) -> Self {
897+
Foo {}
898+
}
899+
}
900+
901+
fn main() {
902+
let foo = Foo {};
903+
let _ = $0Bar::from_foo(foo)$0;
904+
}
905+
"#,
906+
"bar",
907+
);
908+
909+
check(
910+
r#"
911+
struct Foo;
912+
struct Bar;
913+
914+
impl Bar {
915+
fn with_foo(_: Foo) -> Self {
916+
Bar {}
917+
}
918+
}
919+
920+
fn main() {
921+
let foo = Foo {};
922+
let _ = $0Bar::with_foo(foo)$0;
923+
}
924+
"#,
925+
"bar",
926+
);
927+
}
834928
}

0 commit comments

Comments
 (0)