Make disabled diagnostics an argument of corresponding function

This commit is contained in:
Igor Aleksanov 2020-08-18 13:32:29 +03:00
parent c26c911ec1
commit b56cfcca10
7 changed files with 43 additions and 39 deletions

View file

@ -4,7 +4,7 @@
//! macro-expanded files, but we need to present them to the users in terms of
//! original files. So we need to map the ranges.
use std::cell::RefCell;
use std::{cell::RefCell, collections::HashSet};
use base_db::SourceDatabase;
use hir::{diagnostics::DiagnosticSinkBuilder, Semantics};
@ -16,7 +16,7 @@ use syntax::{
};
use text_edit::TextEdit;
use crate::{AnalysisConfig, Diagnostic, FileId, Fix, SourceFileEdit};
use crate::{Diagnostic, FileId, Fix, SourceFileEdit};
mod diagnostics_with_fix;
use diagnostics_with_fix::DiagnosticWithFix;
@ -31,7 +31,7 @@ pub(crate) fn diagnostics(
db: &RootDatabase,
file_id: FileId,
enable_experimental: bool,
analysis_config: &AnalysisConfig,
disabled_diagnostics: Option<HashSet<String>>,
) -> Vec<Diagnostic> {
let _p = profile::span("diagnostics");
let sema = Semantics::new(db);
@ -68,10 +68,9 @@ pub(crate) fn diagnostics(
// Only collect experimental diagnostics when they're enabled.
.filter(|diag| !diag.is_experimental() || enable_experimental);
if !analysis_config.disabled_diagnostics.is_empty() {
if let Some(disabled_diagnostics) = disabled_diagnostics {
// Do not collect disabled diagnostics.
sink_builder =
sink_builder.filter(|diag| !analysis_config.disabled_diagnostics.contains(diag.name()));
sink_builder = sink_builder.filter(move |diag| !disabled_diagnostics.contains(diag.name()));
}
// Finalize the `DiagnosticSink` building process.
@ -192,10 +191,7 @@ mod tests {
use stdx::trim_indent;
use test_utils::assert_eq_text;
use crate::{
mock_analysis::{analysis_and_position, single_file, MockAnalysis},
AnalysisConfig,
};
use crate::mock_analysis::{analysis_and_position, single_file, MockAnalysis};
use expect::{expect, Expect};
/// Takes a multi-file input fixture with annotated cursor positions,
@ -207,7 +203,8 @@ 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, true).unwrap().pop().unwrap();
let diagnostic =
analysis.diagnostics(file_position.file_id, true, None).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();
@ -233,7 +230,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, true).unwrap().pop().unwrap();
let diagnostic = analysis.diagnostics(current_file_id, true, None).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;
@ -254,7 +251,7 @@ mod tests {
let analysis = mock.analysis();
let diagnostics = files
.into_iter()
.flat_map(|file_id| analysis.diagnostics(file_id, true).unwrap())
.flat_map(|file_id| analysis.diagnostics(file_id, true, None).unwrap())
.collect::<Vec<_>>();
assert_eq!(diagnostics.len(), 0, "unexpected diagnostics:\n{:#?}", diagnostics);
}
@ -267,13 +264,14 @@ mod tests {
let mock = MockAnalysis::with_files(ra_fixture);
let files = mock.files().map(|(it, _)| it).collect::<Vec<_>>();
let mut analysis = mock.analysis();
analysis.set_config(AnalysisConfig { disabled_diagnostics: disabled_diagnostics.clone() });
let analysis = mock.analysis();
let diagnostics = files
.clone()
.into_iter()
.flat_map(|file_id| analysis.diagnostics(file_id, true).unwrap())
.flat_map(|file_id| {
analysis.diagnostics(file_id, true, Some(disabled_diagnostics.clone())).unwrap()
})
.collect::<Vec<_>>();
// First, we have to check that diagnostic is not emitted when it's added to the disabled diagnostics list.
@ -288,11 +286,9 @@ mod tests {
// This is required for tests to not become outdated if e.g. diagnostics name changes:
// without this additional run the test will pass simply because a diagnostic with an old name
// will no longer exist.
analysis.set_config(AnalysisConfig { disabled_diagnostics: Default::default() });
let diagnostics = files
.into_iter()
.flat_map(|file_id| analysis.diagnostics(file_id, true).unwrap())
.flat_map(|file_id| analysis.diagnostics(file_id, true, None).unwrap())
.collect::<Vec<_>>();
assert!(
@ -306,7 +302,7 @@ mod tests {
fn check_expect(ra_fixture: &str, expect: Expect) {
let (analysis, file_id) = single_file(ra_fixture);
let diagnostics = analysis.diagnostics(file_id, true).unwrap();
let diagnostics = analysis.diagnostics(file_id, true, None).unwrap();
expect.assert_debug_eq(&diagnostics)
}