Skip to content

Commit 6d2d632

Browse files
committed
incorrect case diagnostics for trait assoc functions and consts
1 parent 5fe3b75 commit 6d2d632

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

crates/hir/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,17 @@ impl Module {
563563
for diag in db.trait_data_with_diagnostics(t.id).1.iter() {
564564
emit_def_diagnostic(db, acc, diag);
565565
}
566+
567+
for item in t.items(db) {
568+
let def: DefWithBody = match item {
569+
AssocItem::Function(it) => it.into(),
570+
AssocItem::Const(it) => it.into(),
571+
AssocItem::TypeAlias(_) => continue,
572+
};
573+
574+
def.diagnostics(db, acc);
575+
}
576+
566577
acc.extend(def.diagnostics(db))
567578
}
568579
ModuleDef::Adt(adt) => {

crates/ide-diagnostics/src/handlers/incorrect_case.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -388,22 +388,23 @@ mod F {
388388

389389
#[test]
390390
fn complex_ignore() {
391-
// FIXME: this should trigger errors for the second case.
392391
check_diagnostics(
393392
r#"
394393
trait T { fn a(); }
395394
struct U {}
396395
impl T for U {
397396
fn a() {
398-
#[allow(non_snake_case)]
397+
#[allow(non_snake_case, non_upper_case_globals)]
399398
trait __BitFlagsOk {
400399
const HiImAlsoBad: u8 = 2;
401400
fn Dirty(&self) -> bool { false }
402401
}
403402
404403
trait __BitFlagsBad {
405404
const HiImAlsoBad: u8 = 2;
405+
// ^^^^^^^^^^^ 💡 warn: Constant `HiImAlsoBad` should have UPPER_SNAKE_CASE name, e.g. `HI_IM_ALSO_BAD`
406406
fn Dirty(&self) -> bool { false }
407+
// ^^^^^💡 warn: Function `Dirty` should have snake_case name, e.g. `dirty`
407408
}
408409
}
409410
}
@@ -463,14 +464,16 @@ extern {
463464

464465
#[test]
465466
fn incorrect_trait_and_assoc_item_names() {
466-
// FIXME: Traits and functions in traits aren't currently checked by
467-
// r-a, even though rustc will complain about them.
468467
check_diagnostics(
469468
r#"
470469
trait BAD_TRAIT {
471470
// ^^^^^^^^^ 💡 warn: Trait `BAD_TRAIT` should have CamelCase name, e.g. `BadTrait`
471+
const bad_const: u8;
472+
// ^^^^^^^^^ 💡 warn: Constant `bad_const` should have UPPER_SNAKE_CASE name, e.g. `BAD_CONST`
472473
fn BAD_FUNCTION();
474+
// ^^^^^^^^^^^^ 💡 warn: Function `BAD_FUNCTION` should have snake_case name, e.g. `bad_function`
473475
fn BadFunction();
476+
// ^^^^^^^^^^^ 💡 warn: Function `BadFunction` should have snake_case name, e.g. `bad_function`
474477
}
475478
"#,
476479
);

crates/ide-diagnostics/src/handlers/mismatched_arg_count.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ fn invalid_args_range(
103103

104104
#[cfg(test)]
105105
mod tests {
106-
use crate::tests::check_diagnostics;
106+
use crate::tests::{check_diagnostics, check_diagnostics_with_disabled};
107107

108108
#[test]
109109
fn simple_free_fn_zero() {
@@ -197,7 +197,7 @@ fn f() {
197197
fn method_unknown_receiver() {
198198
// note: this is incorrect code, so there might be errors on this in the
199199
// future, but we shouldn't emit an argument count diagnostic here
200-
check_diagnostics(
200+
check_diagnostics_with_disabled(
201201
r#"
202202
trait Foo { fn method(&self, arg: usize) {} }
203203
@@ -206,6 +206,7 @@ fn f() {
206206
x.method();
207207
}
208208
"#,
209+
std::iter::once("unused_variables".to_string()),
209210
);
210211
}
211212

crates/ide-diagnostics/src/tests.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,16 @@ pub(crate) fn check_diagnostics(ra_fixture: &str) {
8989
check_diagnostics_with_config(config, ra_fixture)
9090
}
9191

92+
#[track_caller]
93+
pub(crate) fn check_diagnostics_with_disabled(
94+
ra_fixture: &str,
95+
disabled: impl Iterator<Item = String>,
96+
) {
97+
let mut config = DiagnosticsConfig::test_sample();
98+
config.disabled.extend(disabled);
99+
check_diagnostics_with_config(config, ra_fixture)
100+
}
101+
92102
#[track_caller]
93103
pub(crate) fn check_diagnostics_with_config(config: DiagnosticsConfig, ra_fixture: &str) {
94104
let (db, files) = RootDatabase::with_many_files(ra_fixture);
@@ -175,6 +185,7 @@ fn minicore_smoke_test() {
175185
let mut config = DiagnosticsConfig::test_sample();
176186
// This should be ignored since we conditionally remove code which creates single item use with braces
177187
config.disabled.insert("unused_braces".to_string());
188+
config.disabled.insert("unused_variables".to_string());
178189
check_diagnostics_with_config(config, &source);
179190
}
180191

0 commit comments

Comments
 (0)