From 92a4ec80a0ce1dd834578f53ea3fd018530ec0e6 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Fri, 24 Jul 2020 17:39:44 +0200 Subject: Only display experimental diagnostics when enabled --- crates/ra_ide/src/diagnostics.rs | 17 ++++++++++++----- crates/ra_ide/src/lib.rs | 8 ++++++-- crates/rust-analyzer/src/cli/analysis_bench.rs | 2 +- crates/rust-analyzer/src/cli/diagnostics.rs | 2 +- crates/rust-analyzer/src/handlers.rs | 4 ++-- 5 files changed, 22 insertions(+), 11 deletions(-) (limited to 'crates') diff --git a/crates/ra_ide/src/diagnostics.rs b/crates/ra_ide/src/diagnostics.rs index 8e715faa4..897177d05 100644 --- a/crates/ra_ide/src/diagnostics.rs +++ b/crates/ra_ide/src/diagnostics.rs @@ -29,7 +29,11 @@ pub enum Severity { WeakWarning, } -pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec { +pub(crate) fn diagnostics( + db: &RootDatabase, + file_id: FileId, + enable_experimental: bool, +) -> Vec { let _p = profile("diagnostics"); let sema = Semantics::new(db); let parse = db.parse(file_id); @@ -116,6 +120,9 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec fix: missing_struct_field_fix(&sema, file_id, d), }) }) + // Only collect experimental diagnostics when they're enabled. + .filter(|diag| !diag.is_experimental() || enable_experimental) + // Diagnostics not handled above get no fix and default treatment. .build(|d| { res.borrow_mut().push(Diagnostic { message: d.message(), @@ -301,7 +308,7 @@ mod tests { let after = trim_indent(ra_fixture_after); let (analysis, file_position) = analysis_and_position(ra_fixture_before); - let diagnostic = analysis.diagnostics(file_position.file_id).unwrap().pop().unwrap(); + let diagnostic = analysis.diagnostics(file_position.file_id, true).unwrap().pop().unwrap(); let mut fix = diagnostic.fix.unwrap(); let edit = fix.source_change.source_file_edits.pop().unwrap().edit; let target_file_contents = analysis.file_text(file_position.file_id).unwrap(); @@ -327,7 +334,7 @@ mod tests { let ra_fixture_after = &trim_indent(ra_fixture_after); let (analysis, file_pos) = analysis_and_position(ra_fixture_before); let current_file_id = file_pos.file_id; - let diagnostic = analysis.diagnostics(current_file_id).unwrap().pop().unwrap(); + let diagnostic = analysis.diagnostics(current_file_id, true).unwrap().pop().unwrap(); let mut fix = diagnostic.fix.unwrap(); let edit = fix.source_change.source_file_edits.pop().unwrap(); let changed_file_id = edit.file_id; @@ -348,14 +355,14 @@ mod tests { let analysis = mock.analysis(); let diagnostics = files .into_iter() - .flat_map(|file_id| analysis.diagnostics(file_id).unwrap()) + .flat_map(|file_id| analysis.diagnostics(file_id, true).unwrap()) .collect::>(); assert_eq!(diagnostics.len(), 0, "unexpected diagnostics:\n{:#?}", diagnostics); } fn check_expect(ra_fixture: &str, expect: Expect) { let (analysis, file_id) = single_file(ra_fixture); - let diagnostics = analysis.diagnostics(file_id).unwrap(); + let diagnostics = analysis.diagnostics(file_id, true).unwrap(); expect.assert_debug_eq(&diagnostics) } diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs index 7356e947b..4c4d9f6fa 100644 --- a/crates/ra_ide/src/lib.rs +++ b/crates/ra_ide/src/lib.rs @@ -487,8 +487,12 @@ impl Analysis { } /// Computes the set of diagnostics for the given file. - pub fn diagnostics(&self, file_id: FileId) -> Cancelable> { - self.with_db(|db| diagnostics::diagnostics(db, file_id)) + pub fn diagnostics( + &self, + file_id: FileId, + enable_experimental: bool, + ) -> Cancelable> { + self.with_db(|db| diagnostics::diagnostics(db, file_id, enable_experimental)) } /// Returns the edit required to rename reference at the position to the new diff --git a/crates/rust-analyzer/src/cli/analysis_bench.rs b/crates/rust-analyzer/src/cli/analysis_bench.rs index 9299879b7..076184ad6 100644 --- a/crates/rust-analyzer/src/cli/analysis_bench.rs +++ b/crates/rust-analyzer/src/cli/analysis_bench.rs @@ -70,7 +70,7 @@ pub fn analysis_bench( match &what { BenchWhat::Highlight { .. } => { let res = do_work(&mut host, file_id, |analysis| { - analysis.diagnostics(file_id).unwrap(); + analysis.diagnostics(file_id, true).unwrap(); analysis.highlight_as_html(file_id, false).unwrap() }); if verbosity.is_verbose() { diff --git a/crates/rust-analyzer/src/cli/diagnostics.rs b/crates/rust-analyzer/src/cli/diagnostics.rs index 6f3c1c1f9..4ac8c8772 100644 --- a/crates/rust-analyzer/src/cli/diagnostics.rs +++ b/crates/rust-analyzer/src/cli/diagnostics.rs @@ -47,7 +47,7 @@ pub fn diagnostics( String::from("unknown") }; println!("processing crate: {}, module: {}", crate_name, _vfs.file_path(file_id)); - for diagnostic in analysis.diagnostics(file_id).unwrap() { + for diagnostic in analysis.diagnostics(file_id, true).unwrap() { if matches!(diagnostic.severity, Severity::Error) { found_error = true; } diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index cad92c444..cd309ed74 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -774,7 +774,7 @@ fn handle_fixes( None => {} }; - let diagnostics = snap.analysis.diagnostics(file_id)?; + let diagnostics = snap.analysis.diagnostics(file_id, snap.config.experimental_diagnostics)?; let fixes_from_diagnostics = diagnostics .into_iter() @@ -1040,7 +1040,7 @@ pub(crate) fn publish_diagnostics( let line_index = snap.analysis.file_line_index(file_id)?; let diagnostics: Vec = snap .analysis - .diagnostics(file_id)? + .diagnostics(file_id, snap.config.experimental_diagnostics)? .into_iter() .map(|d| Diagnostic { range: to_proto::range(&line_index, d.range), -- cgit v1.2.3