aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2021-06-13 15:29:25 +0100
committerAleksey Kladov <[email protected]>2021-06-13 15:29:25 +0100
commitf85e383b94376d55bb5ee6be375ef3dc0006590f (patch)
tree03e3c3685a893891c7b9f144597362a5e57c02ca /crates/ide/src
parentfa9ed4e0ce633e51d1411951bf044719e6837457 (diff)
internal: refactor inactive code diagnostics
Diffstat (limited to 'crates/ide/src')
-rw-r--r--crates/ide/src/diagnostics.rs30
-rw-r--r--crates/ide/src/diagnostics/inactive_code.rs113
2 files changed, 126 insertions, 17 deletions
diff --git a/crates/ide/src/diagnostics.rs b/crates/ide/src/diagnostics.rs
index 1a4800832..634e6a043 100644
--- a/crates/ide/src/diagnostics.rs
+++ b/crates/ide/src/diagnostics.rs
@@ -8,6 +8,7 @@ mod unresolved_module;
8mod unresolved_extern_crate; 8mod unresolved_extern_crate;
9mod unresolved_import; 9mod unresolved_import;
10mod unresolved_macro_call; 10mod unresolved_macro_call;
11mod inactive_code;
11mod missing_fields; 12mod missing_fields;
12 13
13mod fixes; 14mod fixes;
@@ -164,22 +165,6 @@ pub(crate) fn diagnostics(
164 .on::<hir::diagnostics::ReplaceFilterMapNextWithFindMap, _>(|d| { 165 .on::<hir::diagnostics::ReplaceFilterMapNextWithFindMap, _>(|d| {
165 res.borrow_mut().push(warning_with_fix(d, &sema, resolve)); 166 res.borrow_mut().push(warning_with_fix(d, &sema, resolve));
166 }) 167 })
167 .on::<hir::diagnostics::InactiveCode, _>(|d| {
168 // If there's inactive code somewhere in a macro, don't propagate to the call-site.
169 if d.display_source().file_id.expansion_info(db).is_some() {
170 return;
171 }
172
173 // Override severity and mark as unused.
174 res.borrow_mut().push(
175 Diagnostic::hint(
176 sema.diagnostics_display_range(d.display_source()).range,
177 d.message(),
178 )
179 .with_unused(true)
180 .with_code(Some(d.code())),
181 );
182 })
183 .on::<UnlinkedFile, _>(|d| { 168 .on::<UnlinkedFile, _>(|d| {
184 // Limit diagnostic to the first few characters in the file. This matches how VS Code 169 // Limit diagnostic to the first few characters in the file. This matches how VS Code
185 // renders it with the full span, but on other editors, and is less invasive. 170 // renders it with the full span, but on other editors, and is less invasive.
@@ -247,6 +232,11 @@ pub(crate) fn diagnostics(
247 AnyDiagnostic::UnresolvedImport(d) => unresolved_import::unresolved_import(&ctx, &d), 232 AnyDiagnostic::UnresolvedImport(d) => unresolved_import::unresolved_import(&ctx, &d),
248 AnyDiagnostic::UnresolvedMacroCall(d) => unresolved_macro_call::unresolved_macro_call(&ctx, &d), 233 AnyDiagnostic::UnresolvedMacroCall(d) => unresolved_macro_call::unresolved_macro_call(&ctx, &d),
249 AnyDiagnostic::MissingFields(d) => missing_fields::missing_fields(&ctx, &d), 234 AnyDiagnostic::MissingFields(d) => missing_fields::missing_fields(&ctx, &d),
235
236 AnyDiagnostic::InactiveCode(d) => match inactive_code::inactive_code(&ctx, &d) {
237 Some(it) => it,
238 None => continue,
239 }
250 }; 240 };
251 if let Some(code) = d.code { 241 if let Some(code) = d.code {
252 if ctx.config.disabled.contains(code.as_str()) { 242 if ctx.config.disabled.contains(code.as_str()) {
@@ -451,7 +441,13 @@ mod tests {
451 expect.assert_debug_eq(&diagnostics) 441 expect.assert_debug_eq(&diagnostics)
452 } 442 }
453 443
444 #[track_caller]
454 pub(crate) fn check_diagnostics(ra_fixture: &str) { 445 pub(crate) fn check_diagnostics(ra_fixture: &str) {
446 check_diagnostics_with_inactive_code(ra_fixture, false)
447 }
448
449 #[track_caller]
450 pub(crate) fn check_diagnostics_with_inactive_code(ra_fixture: &str, with_inactive_code: bool) {
455 let (analysis, file_id) = fixture::file(ra_fixture); 451 let (analysis, file_id) = fixture::file(ra_fixture);
456 let diagnostics = analysis 452 let diagnostics = analysis
457 .diagnostics(&DiagnosticsConfig::default(), AssistResolveStrategy::All, file_id) 453 .diagnostics(&DiagnosticsConfig::default(), AssistResolveStrategy::All, file_id)
@@ -460,7 +456,7 @@ mod tests {
460 let expected = extract_annotations(&*analysis.file_text(file_id).unwrap()); 456 let expected = extract_annotations(&*analysis.file_text(file_id).unwrap());
461 let mut actual = diagnostics 457 let mut actual = diagnostics
462 .into_iter() 458 .into_iter()
463 .filter(|d| d.code != Some(DiagnosticCode("inactive-code"))) 459 .filter(|d| d.code != Some(DiagnosticCode("inactive-code")) || with_inactive_code)
464 .map(|d| (d.range, d.message)) 460 .map(|d| (d.range, d.message))
465 .collect::<Vec<_>>(); 461 .collect::<Vec<_>>();
466 actual.sort_by_key(|(range, _)| range.start()); 462 actual.sort_by_key(|(range, _)| range.start());
diff --git a/crates/ide/src/diagnostics/inactive_code.rs b/crates/ide/src/diagnostics/inactive_code.rs
new file mode 100644
index 000000000..52f97cb4c
--- /dev/null
+++ b/crates/ide/src/diagnostics/inactive_code.rs
@@ -0,0 +1,113 @@
1use cfg::DnfExpr;
2use stdx::format_to;
3
4use crate::diagnostics::{Diagnostic, DiagnosticsContext};
5
6// Diagnostic: inactive-code
7//
8// This diagnostic is shown for code with inactive `#[cfg]` attributes.
9pub(super) fn inactive_code(
10 ctx: &DiagnosticsContext<'_>,
11 d: &hir::InactiveCode,
12) -> Option<Diagnostic> {
13 // If there's inactive code somewhere in a macro, don't propagate to the call-site.
14 if d.node.file_id.expansion_info(ctx.sema.db).is_some() {
15 return None;
16 }
17
18 let inactive = DnfExpr::new(d.cfg.clone()).why_inactive(&d.opts);
19 let mut message = "code is inactive due to #[cfg] directives".to_string();
20
21 if let Some(inactive) = inactive {
22 format_to!(message, ": {}", inactive);
23 }
24
25 let res = Diagnostic::new(
26 "inactive-code",
27 message,
28 ctx.sema.diagnostics_display_range(d.node.clone()).range,
29 )
30 .with_unused(true);
31 Some(res)
32}
33
34#[cfg(test)]
35mod tests {
36 use crate::diagnostics::tests::check_diagnostics_with_inactive_code;
37
38 #[test]
39 fn cfg_diagnostics() {
40 check_diagnostics_with_inactive_code(
41 r#"
42fn f() {
43 // The three g̶e̶n̶d̶e̶r̶s̶ statements:
44
45 #[cfg(a)] fn f() {} // Item statement
46 //^^^^^^^^^^^^^^^^^^^ code is inactive due to #[cfg] directives: a is disabled
47 #[cfg(a)] {} // Expression statement
48 //^^^^^^^^^^^^ code is inactive due to #[cfg] directives: a is disabled
49 #[cfg(a)] let x = 0; // let statement
50 //^^^^^^^^^^^^^^^^^^^^ code is inactive due to #[cfg] directives: a is disabled
51
52 abc(#[cfg(a)] 0);
53 //^^^^^^^^^^^ code is inactive due to #[cfg] directives: a is disabled
54 let x = Struct {
55 #[cfg(a)] f: 0,
56 //^^^^^^^^^^^^^^ code is inactive due to #[cfg] directives: a is disabled
57 };
58 match () {
59 () => (),
60 #[cfg(a)] () => (),
61 //^^^^^^^^^^^^^^^^^^ code is inactive due to #[cfg] directives: a is disabled
62 }
63
64 #[cfg(a)] 0 // Trailing expression of block
65 //^^^^^^^^^^^ code is inactive due to #[cfg] directives: a is disabled
66}
67 "#,
68 true,
69 );
70 }
71
72 #[test]
73 fn inactive_item() {
74 // Additional tests in `cfg` crate. This only tests disabled cfgs.
75
76 check_diagnostics_with_inactive_code(
77 r#"
78 #[cfg(no)] pub fn f() {}
79 //^^^^^^^^^^^^^^^^^^^^^^^^ code is inactive due to #[cfg] directives: no is disabled
80
81 #[cfg(no)] #[cfg(no2)] mod m;
82 //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ code is inactive due to #[cfg] directives: no and no2 are disabled
83
84 #[cfg(all(not(a), b))] enum E {}
85 //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ code is inactive due to #[cfg] directives: b is disabled
86
87 #[cfg(feature = "std")] use std;
88 //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ code is inactive due to #[cfg] directives: feature = "std" is disabled
89"#,
90 true,
91 );
92 }
93
94 /// Tests that `cfg` attributes behind `cfg_attr` is handled properly.
95 #[test]
96 fn inactive_via_cfg_attr() {
97 cov_mark::check!(cfg_attr_active);
98 check_diagnostics_with_inactive_code(
99 r#"
100 #[cfg_attr(not(never), cfg(no))] fn f() {}
101 //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ code is inactive due to #[cfg] directives: no is disabled
102
103 #[cfg_attr(not(never), cfg(not(no)))] fn f() {}
104
105 #[cfg_attr(never, cfg(no))] fn g() {}
106
107 #[cfg_attr(not(never), inline, cfg(no))] fn h() {}
108 //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ code is inactive due to #[cfg] directives: no is disabled
109"#,
110 true,
111 );
112 }
113}