Skip to content

Commit d5a4f31

Browse files
committed
implement lint groups
1 parent 6496758 commit d5a4f31

File tree

4 files changed

+56
-14
lines changed

4 files changed

+56
-14
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ide-diagnostics/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ cov-mark = "2.0.0-pre.1"
1616
either = "1.7.0"
1717
itertools = "0.10.5"
1818
serde_json = "1.0.86"
19+
once_cell = "1.17.0"
1920

2021
# local deps
2122
profile.workspace = true

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ pub static SomeStatic: u8 = 10;
495495
check_diagnostics(
496496
r#"
497497
#[deny(non_snake_case)]
498-
fn NonSnakeCaseName(some_var: u8) -> u8{
498+
fn NonSnakeCaseName(some_var: u8) -> u8 {
499499
//^^^^^^^^^^^^^^^^ 💡 error: Function `NonSnakeCaseName` should have snake_case name, e.g. `non_snake_case_name`
500500
// cov_flags generated output from elsewhere in this file
501501
extern "C" {
@@ -508,18 +508,16 @@ fn NonSnakeCaseName(some_var: u8) -> u8{
508508
OtherVar
509509
}
510510
511-
// FIXME: lint groups are not implemented
512-
513511
#[deny(nonstandard_style)]
514512
mod CheckNonstandardStyle {
515513
fn HiImABadFnName() {}
516-
//^^^^^^^^^^^^^^ 💡 warn: Function `HiImABadFnName` should have snake_case name, e.g. `hi_im_abad_fn_name`
514+
//^^^^^^^^^^^^^^ 💡 error: Function `HiImABadFnName` should have snake_case name, e.g. `hi_im_abad_fn_name`
517515
}
518516
519-
#[deny(bad_style)]
517+
#[deny(warnings)]
520518
mod CheckBadStyle {
521519
struct fooo;
522-
//^^^^ 💡 warn: Structure `fooo` should have CamelCase name, e.g. `Fooo`
520+
//^^^^ 💡 error: Structure `fooo` should have CamelCase name, e.g. `Fooo`
523521
}
524522
525523
mod F {

crates/ide-diagnostics/src/lib.rs

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,20 @@ mod handlers {
6767
#[cfg(test)]
6868
mod tests;
6969

70+
use std::collections::HashMap;
71+
7072
use hir::{diagnostics::AnyDiagnostic, InFile, Semantics};
7173
use ide_db::{
7274
assists::{Assist, AssistId, AssistKind, AssistResolveStrategy},
7375
base_db::{FileId, FileRange, SourceDatabase},
76+
generated::lints::{LintGroup, CLIPPY_LINT_GROUPS, DEFAULT_LINT_GROUPS},
7477
imports::insert_use::InsertUseConfig,
7578
label::Label,
7679
source_change::SourceChange,
7780
syntax_helpers::node_ext::parse_tt_as_comma_sep_paths,
7881
FxHashMap, FxHashSet, RootDatabase,
7982
};
83+
use once_cell::sync::Lazy;
8084
use stdx::never;
8185
use syntax::{
8286
algo::find_node_at_range,
@@ -370,7 +374,7 @@ pub fn diagnostics(
370374
let mut rustc_stack: FxHashMap<String, Vec<Severity>> = FxHashMap::default();
371375
let mut clippy_stack: FxHashMap<String, Vec<Severity>> = FxHashMap::default();
372376

373-
fun_name(
377+
handle_lint_attributes(
374378
&ctx.sema,
375379
parse.syntax(),
376380
&mut rustc_stack,
@@ -387,7 +391,30 @@ pub fn diagnostics(
387391
res
388392
}
389393

390-
fn fun_name(
394+
static RUSTC_LINT_GROUPS_DICT: Lazy<HashMap<&str, Vec<&str>>> =
395+
Lazy::new(|| build_group_dict(DEFAULT_LINT_GROUPS, &["warnings"]));
396+
397+
static CLIPPY_LINT_GROUPS_DICT: Lazy<HashMap<&str, Vec<&str>>> =
398+
Lazy::new(|| build_group_dict(CLIPPY_LINT_GROUPS, &[]));
399+
400+
fn build_group_dict(
401+
lint_group: &'static [LintGroup],
402+
all_groups: &'static [&'static str],
403+
) -> HashMap<&'static str, Vec<&'static str>> {
404+
let mut r: HashMap<&str, Vec<&str>> = HashMap::new();
405+
for g in lint_group {
406+
for child in g.children {
407+
r.entry(child).or_default().push(g.lint.label);
408+
}
409+
}
410+
for (lint, groups) in r.iter_mut() {
411+
groups.push(lint);
412+
groups.extend_from_slice(all_groups);
413+
}
414+
r
415+
}
416+
417+
fn handle_lint_attributes(
391418
sema: &Semantics<'_, RootDatabase>,
392419
root: &SyntaxNode,
393420
rustc_stack: &mut FxHashMap<String, Vec<Severity>>,
@@ -406,18 +433,33 @@ fn fun_name(
406433
if let Some(x) =
407434
diagnostics_of_range.get_mut(&InFile { file_id, value: node.clone() })
408435
{
409-
let (name, stack) = match x.code {
410-
DiagnosticOrigin::RustcLint(name) => (name, &mut *rustc_stack),
411-
DiagnosticOrigin::Clippy(name) => (name, &mut *clippy_stack),
436+
const EMPTY_LINTS: &[&str] = &[];
437+
let (names, stack) = match x.code {
438+
DiagnosticOrigin::RustcLint(name) => (
439+
RUSTC_LINT_GROUPS_DICT.get(name).map_or(EMPTY_LINTS, |x| &**x),
440+
&mut *rustc_stack,
441+
),
442+
DiagnosticOrigin::Clippy(name) => (
443+
CLIPPY_LINT_GROUPS_DICT.get(name).map_or(EMPTY_LINTS, |x| &**x),
444+
&mut *clippy_stack,
445+
),
412446
_ => continue,
413447
};
414-
if let Some(s) = stack.get(name).and_then(|x| x.last()) {
415-
x.severity = *s;
448+
for &name in names {
449+
if let Some(s) = stack.get(name).and_then(|x| x.last()) {
450+
x.severity = *s;
451+
}
416452
}
417453
}
418454
if let Some(mc) = ast::MacroCall::cast(node) {
419455
if let Some(me) = sema.expand(&mc) {
420-
fun_name(sema, &me, rustc_stack, clippy_stack, diagnostics_of_range);
456+
handle_lint_attributes(
457+
sema,
458+
&me,
459+
rustc_stack,
460+
clippy_stack,
461+
diagnostics_of_range,
462+
);
421463
}
422464
}
423465
}

0 commit comments

Comments
 (0)