Skip to content

Commit 138d1a9

Browse files
Jonas Schievinkbnjjj
authored andcommitted
Add a (hint) diagnostic for unconfigured items
1 parent f324f76 commit 138d1a9

File tree

6 files changed

+86
-3
lines changed

6 files changed

+86
-3
lines changed

crates/hir/src/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! FIXME: write short doc here
2-
pub use hir_def::diagnostics::UnresolvedModule;
2+
pub use hir_def::diagnostics::{UnconfiguredCode, UnresolvedModule};
33
pub use hir_expand::diagnostics::{Diagnostic, DiagnosticSink, DiagnosticSinkBuilder};
44
pub use hir_ty::diagnostics::{
55
IncorrectCase, MismatchedArgCount, MissingFields, MissingMatchArms, MissingOkInTailExpr,

crates/hir_def/src/diagnostics.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,28 @@ impl Diagnostic for UnresolvedImport {
8686
true
8787
}
8888
}
89+
90+
// Diagnostic: unconfigured-code
91+
//
92+
// This diagnostic is shown for code with inactive `#[cfg]` attributes.
93+
#[derive(Debug)]
94+
pub struct UnconfiguredCode {
95+
pub file: HirFileId,
96+
pub node: SyntaxNodePtr,
97+
}
98+
99+
impl Diagnostic for UnconfiguredCode {
100+
fn code(&self) -> DiagnosticCode {
101+
DiagnosticCode("unconfigured-code")
102+
}
103+
fn message(&self) -> String {
104+
// FIXME: say *why* it is configured out
105+
"configured out".to_string()
106+
}
107+
fn display_source(&self) -> InFile<SyntaxNodePtr> {
108+
InFile::new(self.file, self.node.clone())
109+
}
110+
fn as_any(&self) -> &(dyn Any + Send + 'static) {
111+
self
112+
}
113+
}

crates/hir_def/src/item_tree.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,24 @@ impl ModItem {
672672
pub fn downcast<N: ItemTreeNode>(self) -> Option<FileItemTreeId<N>> {
673673
N::id_from_mod_item(self)
674674
}
675+
676+
pub fn ast_id(&self, tree: &ItemTree) -> FileAstId<ast::Item> {
677+
match self {
678+
ModItem::Import(it) => tree[it.index].ast_id().upcast(),
679+
ModItem::ExternCrate(it) => tree[it.index].ast_id().upcast(),
680+
ModItem::Function(it) => tree[it.index].ast_id().upcast(),
681+
ModItem::Struct(it) => tree[it.index].ast_id().upcast(),
682+
ModItem::Union(it) => tree[it.index].ast_id().upcast(),
683+
ModItem::Enum(it) => tree[it.index].ast_id().upcast(),
684+
ModItem::Const(it) => tree[it.index].ast_id().upcast(),
685+
ModItem::Static(it) => tree[it.index].ast_id().upcast(),
686+
ModItem::Trait(it) => tree[it.index].ast_id().upcast(),
687+
ModItem::Impl(it) => tree[it.index].ast_id().upcast(),
688+
ModItem::TypeAlias(it) => tree[it.index].ast_id().upcast(),
689+
ModItem::Mod(it) => tree[it.index].ast_id().upcast(),
690+
ModItem::MacroCall(it) => tree[it.index].ast_id().upcast(),
691+
}
692+
}
675693
}
676694

677695
#[derive(Debug, Copy, Clone, Eq, PartialEq)]

crates/hir_def/src/nameres.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ mod diagnostics {
286286
use hir_expand::diagnostics::DiagnosticSink;
287287
use hir_expand::hygiene::Hygiene;
288288
use hir_expand::InFile;
289-
use syntax::{ast, AstPtr};
289+
use syntax::{ast, AstPtr, SyntaxNodePtr};
290290

291291
use crate::path::ModPath;
292292
use crate::{db::DefDatabase, diagnostics::*, nameres::LocalModuleId, AstId};
@@ -298,6 +298,8 @@ mod diagnostics {
298298
UnresolvedExternCrate { ast: AstId<ast::ExternCrate> },
299299

300300
UnresolvedImport { ast: AstId<ast::Use>, index: usize },
301+
302+
UnconfiguredCode { ast: InFile<SyntaxNodePtr> },
301303
}
302304

303305
#[derive(Debug, PartialEq, Eq)]
@@ -336,6 +338,13 @@ mod diagnostics {
336338
Self { in_module: container, kind: DiagnosticKind::UnresolvedImport { ast, index } }
337339
}
338340

341+
pub(super) fn unconfigured_code(
342+
container: LocalModuleId,
343+
ast: InFile<SyntaxNodePtr>,
344+
) -> Self {
345+
Self { in_module: container, kind: DiagnosticKind::UnconfiguredCode { ast } }
346+
}
347+
339348
pub(super) fn add_to(
340349
&self,
341350
db: &dyn DefDatabase,
@@ -385,6 +394,10 @@ mod diagnostics {
385394
sink.push(UnresolvedImport { file: ast.file_id, node: AstPtr::new(&tree) });
386395
}
387396
}
397+
398+
DiagnosticKind::UnconfiguredCode { ast } => {
399+
sink.push(UnconfiguredCode { file: ast.file_id, node: ast.value.clone() });
400+
}
388401
}
389402
}
390403
}

crates/hir_def/src/nameres/collector.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,7 @@ impl ModCollector<'_, '_> {
913913
for &item in items {
914914
let attrs = self.item_tree.attrs(item.into());
915915
if !self.is_cfg_enabled(attrs) {
916+
self.emit_unconfigured_diagnostic(item);
916917
continue;
917918
}
918919
let module =
@@ -1323,6 +1324,18 @@ impl ModCollector<'_, '_> {
13231324
fn is_cfg_enabled(&self, attrs: &Attrs) -> bool {
13241325
attrs.is_cfg_enabled(self.def_collector.cfg_options)
13251326
}
1327+
1328+
fn emit_unconfigured_diagnostic(&mut self, item: ModItem) {
1329+
let ast_id = item.ast_id(self.item_tree);
1330+
let id_map = self.def_collector.db.ast_id_map(self.file_id);
1331+
let syntax_ptr = id_map.get(ast_id).syntax_node_ptr();
1332+
1333+
let ast_node = InFile::new(self.file_id, syntax_ptr);
1334+
self.def_collector
1335+
.def_map
1336+
.diagnostics
1337+
.push(DefDiagnostic::unconfigured_code(self.module_id, ast_node));
1338+
}
13261339
}
13271340

13281341
fn is_macro_rules(path: &ModPath) -> bool {

crates/ide/src/diagnostics.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ mod field_shorthand;
1010
use std::cell::RefCell;
1111

1212
use base_db::SourceDatabase;
13-
use hir::{diagnostics::DiagnosticSinkBuilder, Semantics};
13+
use hir::{
14+
diagnostics::{Diagnostic as _, DiagnosticSinkBuilder},
15+
Semantics,
16+
};
1417
use ide_db::RootDatabase;
1518
use itertools::Itertools;
1619
use rustc_hash::FxHashSet;
@@ -46,6 +49,10 @@ impl Diagnostic {
4649
fn with_fix(self, fix: Option<Fix>) -> Self {
4750
Self { fix, ..self }
4851
}
52+
53+
fn with_unused(self, unused: bool) -> Self {
54+
Self { unused, ..self }
55+
}
4956
}
5057

5158
#[derive(Debug)]
@@ -115,6 +122,13 @@ pub(crate) fn diagnostics(
115122
.on::<hir::diagnostics::IncorrectCase, _>(|d| {
116123
res.borrow_mut().push(warning_with_fix(d, &sema));
117124
})
125+
.on::<hir::diagnostics::UnconfiguredCode, _>(|d| {
126+
// Override severity and mark as unused.
127+
res.borrow_mut().push(
128+
Diagnostic::hint(sema.diagnostics_display_range(d).range, d.message())
129+
.with_unused(true),
130+
);
131+
})
118132
// Only collect experimental diagnostics when they're enabled.
119133
.filter(|diag| !(diag.is_experimental() && config.disable_experimental))
120134
.filter(|diag| !config.disabled.contains(diag.code().as_str()));

0 commit comments

Comments
 (0)