diff --git a/src/items.rs b/src/items.rs index a72646ef897..1f4a14211b5 100644 --- a/src/items.rs +++ b/src/items.rs @@ -248,7 +248,6 @@ fn from_foreign_mod(fm: &'a ast::ForeignMod, span: Span, config: &Config) -> Ite abi: format_extern( ast::Extern::from_abi(fm.abi, DUMMY_SP), config.force_explicit_abi(), - true, ), vis: None, body: fm @@ -336,7 +335,6 @@ fn to_str(&self, context: &RewriteContext<'_>) -> String { result.push_str(&format_extern( self.ext, context.config.force_explicit_abi(), - false, )); result } diff --git a/src/types.rs b/src/types.rs index 8be474d5bca..f2e229d7477 100644 --- a/src/types.rs +++ b/src/types.rs @@ -892,7 +892,6 @@ fn rewrite_bare_fn( result.push_str(&format_extern( bare_fn.ext, context.config.force_explicit_abi(), - false, )); result.push_str("fn"); diff --git a/src/utils.rs b/src/utils.rs index d1cb197cb51..ae7c50b4ca2 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -131,23 +131,18 @@ pub(crate) fn format_mutability(mutability: ast::Mutability) -> &'static str { } #[inline] -pub(crate) fn format_extern( - ext: ast::Extern, - explicit_abi: bool, - is_mod: bool, -) -> Cow<'static, str> { - let abi = match ext { - ast::Extern::None => "Rust".to_owned(), - ast::Extern::Implicit(_) => "C".to_owned(), - ast::Extern::Explicit(abi, _) => abi.symbol_unescaped.to_string(), - }; - - if abi == "Rust" && !is_mod { - Cow::from("") - } else if abi == "C" && !explicit_abi { - Cow::from("extern ") - } else { - Cow::from(format!(r#"extern "{abi}" "#)) +pub(crate) fn format_extern(ext: ast::Extern, explicit_abi: bool) -> Cow<'static, str> { + match ext { + ast::Extern::None => Cow::from(""), + ast::Extern::Implicit(_) if explicit_abi => Cow::from("extern \"C\" "), + ast::Extern::Implicit(_) => Cow::from("extern "), + // turn `extern "C"` into `extern` when `explicit_abi` is set to false + ast::Extern::Explicit(abi, _) if abi.symbol_unescaped == sym::C && !explicit_abi => { + Cow::from("extern ") + } + ast::Extern::Explicit(abi, _) => { + Cow::from(format!(r#"extern "{}" "#, abi.symbol_unescaped)) + } } } diff --git a/tests/target/extern-rust.rs b/tests/target/extern-rust.rs new file mode 100644 index 00000000000..32824c91203 --- /dev/null +++ b/tests/target/extern-rust.rs @@ -0,0 +1 @@ +extern "Rust" fn uwu() {}