incorrect case diagnostics for trait assoc functions and consts

This commit is contained in:
davidsemakula 2024-02-02 02:11:43 +03:00
parent 5fe3b75677
commit 6d2d6323ac
4 changed files with 32 additions and 6 deletions

View File

@ -563,6 +563,17 @@ impl Module {
for diag in db.trait_data_with_diagnostics(t.id).1.iter() {
emit_def_diagnostic(db, acc, diag);
}
for item in t.items(db) {
let def: DefWithBody = match item {
AssocItem::Function(it) => it.into(),
AssocItem::Const(it) => it.into(),
AssocItem::TypeAlias(_) => continue,
};
def.diagnostics(db, acc);
}
acc.extend(def.diagnostics(db))
}
ModuleDef::Adt(adt) => {

View File

@ -388,14 +388,13 @@ mod F {
#[test]
fn complex_ignore() {
// FIXME: this should trigger errors for the second case.
check_diagnostics(
r#"
trait T { fn a(); }
struct U {}
impl T for U {
fn a() {
#[allow(non_snake_case)]
#[allow(non_snake_case, non_upper_case_globals)]
trait __BitFlagsOk {
const HiImAlsoBad: u8 = 2;
fn Dirty(&self) -> bool { false }
@ -403,7 +402,9 @@ impl T for U {
trait __BitFlagsBad {
const HiImAlsoBad: u8 = 2;
// ^^^^^^^^^^^ 💡 warn: Constant `HiImAlsoBad` should have UPPER_SNAKE_CASE name, e.g. `HI_IM_ALSO_BAD`
fn Dirty(&self) -> bool { false }
// ^^^^^💡 warn: Function `Dirty` should have snake_case name, e.g. `dirty`
}
}
}
@ -463,14 +464,16 @@ extern {
#[test]
fn incorrect_trait_and_assoc_item_names() {
// FIXME: Traits and functions in traits aren't currently checked by
// r-a, even though rustc will complain about them.
check_diagnostics(
r#"
trait BAD_TRAIT {
// ^^^^^^^^^ 💡 warn: Trait `BAD_TRAIT` should have CamelCase name, e.g. `BadTrait`
const bad_const: u8;
// ^^^^^^^^^ 💡 warn: Constant `bad_const` should have UPPER_SNAKE_CASE name, e.g. `BAD_CONST`
fn BAD_FUNCTION();
// ^^^^^^^^^^^^ 💡 warn: Function `BAD_FUNCTION` should have snake_case name, e.g. `bad_function`
fn BadFunction();
// ^^^^^^^^^^^ 💡 warn: Function `BadFunction` should have snake_case name, e.g. `bad_function`
}
"#,
);

View File

@ -103,7 +103,7 @@ fn invalid_args_range(
#[cfg(test)]
mod tests {
use crate::tests::check_diagnostics;
use crate::tests::{check_diagnostics, check_diagnostics_with_disabled};
#[test]
fn simple_free_fn_zero() {
@ -197,7 +197,7 @@ fn f() {
fn method_unknown_receiver() {
// note: this is incorrect code, so there might be errors on this in the
// future, but we shouldn't emit an argument count diagnostic here
check_diagnostics(
check_diagnostics_with_disabled(
r#"
trait Foo { fn method(&self, arg: usize) {} }
@ -206,6 +206,7 @@ fn f() {
x.method();
}
"#,
std::iter::once("unused_variables".to_string()),
);
}

View File

@ -89,6 +89,16 @@ pub(crate) fn check_diagnostics(ra_fixture: &str) {
check_diagnostics_with_config(config, ra_fixture)
}
#[track_caller]
pub(crate) fn check_diagnostics_with_disabled(
ra_fixture: &str,
disabled: impl Iterator<Item = String>,
) {
let mut config = DiagnosticsConfig::test_sample();
config.disabled.extend(disabled);
check_diagnostics_with_config(config, ra_fixture)
}
#[track_caller]
pub(crate) fn check_diagnostics_with_config(config: DiagnosticsConfig, ra_fixture: &str) {
let (db, files) = RootDatabase::with_many_files(ra_fixture);
@ -175,6 +185,7 @@ fn minicore_smoke_test() {
let mut config = DiagnosticsConfig::test_sample();
// This should be ignored since we conditionally remove code which creates single item use with braces
config.disabled.insert("unused_braces".to_string());
config.disabled.insert("unused_variables".to_string());
check_diagnostics_with_config(config, &source);
}