Auto merge of #18136 - valadaptive:no-mangle-lints, r=Veykril

Don't lint names of #[no_mangle] extern fns

[Rust doesn't run the `non_snake_case_name` lint on `extern fn`s with the `#[no_mangle]` attribute](https://github.com/rust-lang/rust/pull/44966).

The conditions are:
- The function must be `extern` and have a `#[no_mangle]` attribute.
- The function's ABI must not be explicitly set to "Rust".

This PR replicates that logic here.
This commit is contained in:
bors 2024-09-18 08:50:40 +00:00
commit 5b7c812634
2 changed files with 73 additions and 6 deletions

View file

@ -24,6 +24,7 @@ use hir_expand::{
name::{AsName, Name},
HirFileId, HirFileIdExt,
};
use intern::sym;
use stdx::{always, never};
use syntax::{
ast::{self, HasName},
@ -197,12 +198,20 @@ impl<'a> DeclValidator<'a> {
// Skipped if function is an associated item of a trait implementation.
if !self.is_trait_impl_container(container) {
let data = self.db.function_data(func);
self.create_incorrect_case_diagnostic_for_item_name(
func,
&data.name,
CaseType::LowerSnakeCase,
IdentType::Function,
);
// Don't run the lint on extern "[not Rust]" fn items with the
// #[no_mangle] attribute.
let no_mangle = data.attrs.by_key(&sym::no_mangle).exists();
if no_mangle && data.abi.as_ref().is_some_and(|abi| *abi != sym::Rust) {
cov_mark::hit!(extern_func_no_mangle_ignored);
} else {
self.create_incorrect_case_diagnostic_for_item_name(
func,
&data.name,
CaseType::LowerSnakeCase,
IdentType::Function,
);
}
} else {
cov_mark::hit!(trait_impl_assoc_func_name_incorrect_case_ignored);
}

View file

@ -418,6 +418,64 @@ fn f((_O): u8) {}
)
}
#[test]
fn ignores_no_mangle_items() {
cov_mark::check!(extern_func_no_mangle_ignored);
check_diagnostics(
r#"
#[no_mangle]
extern "C" fn NonSnakeCaseName(some_var: u8) -> u8;
"#,
);
}
#[test]
fn ignores_no_mangle_items_with_no_abi() {
cov_mark::check!(extern_func_no_mangle_ignored);
check_diagnostics(
r#"
#[no_mangle]
extern fn NonSnakeCaseName(some_var: u8) -> u8;
"#,
);
}
#[test]
fn no_mangle_items_with_rust_abi() {
check_diagnostics(
r#"
#[no_mangle]
extern "Rust" fn NonSnakeCaseName(some_var: u8) -> u8;
// ^^^^^^^^^^^^^^^^ 💡 warn: Function `NonSnakeCaseName` should have snake_case name, e.g. `non_snake_case_name`
"#,
);
}
#[test]
fn no_mangle_items_non_extern() {
check_diagnostics(
r#"
#[no_mangle]
fn NonSnakeCaseName(some_var: u8) -> u8;
// ^^^^^^^^^^^^^^^^ 💡 warn: Function `NonSnakeCaseName` should have snake_case name, e.g. `non_snake_case_name`
"#,
);
}
#[test]
fn extern_fn_name() {
check_diagnostics(
r#"
extern "C" fn NonSnakeCaseName(some_var: u8) -> u8;
// ^^^^^^^^^^^^^^^^ 💡 warn: Function `NonSnakeCaseName` should have snake_case name, e.g. `non_snake_case_name`
extern "Rust" fn NonSnakeCaseName(some_var: u8) -> u8;
// ^^^^^^^^^^^^^^^^ 💡 warn: Function `NonSnakeCaseName` should have snake_case name, e.g. `non_snake_case_name`
extern fn NonSnakeCaseName(some_var: u8) -> u8;
// ^^^^^^^^^^^^^^^^ 💡 warn: Function `NonSnakeCaseName` should have snake_case name, e.g. `non_snake_case_name`
"#,
);
}
#[test]
fn ignores_extern_items() {
cov_mark::check!(extern_func_incorrect_case_ignored);