Skip to content

Commit

Permalink
Auto merge of rust-lang#18041 - roife:fix-issue-17631, r=Veykril
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Sep 9, 2024
2 parents f16a03f + e1653b6 commit 600f7cf
Showing 1 changed file with 95 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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",
);
}
}

0 comments on commit 600f7cf

Please sign in to comment.