Skip to content

Commit 92a4ec8

Browse files
author
Jonas Schievink
committed
Only display experimental diagnostics when enabled
1 parent f6f4973 commit 92a4ec8

File tree

5 files changed

+22
-11
lines changed

5 files changed

+22
-11
lines changed

crates/ra_ide/src/diagnostics.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ pub enum Severity {
2929
WeakWarning,
3030
}
3131

32-
pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic> {
32+
pub(crate) fn diagnostics(
33+
db: &RootDatabase,
34+
file_id: FileId,
35+
enable_experimental: bool,
36+
) -> Vec<Diagnostic> {
3337
let _p = profile("diagnostics");
3438
let sema = Semantics::new(db);
3539
let parse = db.parse(file_id);
@@ -116,6 +120,9 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
116120
fix: missing_struct_field_fix(&sema, file_id, d),
117121
})
118122
})
123+
// Only collect experimental diagnostics when they're enabled.
124+
.filter(|diag| !diag.is_experimental() || enable_experimental)
125+
// Diagnostics not handled above get no fix and default treatment.
119126
.build(|d| {
120127
res.borrow_mut().push(Diagnostic {
121128
message: d.message(),
@@ -301,7 +308,7 @@ mod tests {
301308
let after = trim_indent(ra_fixture_after);
302309

303310
let (analysis, file_position) = analysis_and_position(ra_fixture_before);
304-
let diagnostic = analysis.diagnostics(file_position.file_id).unwrap().pop().unwrap();
311+
let diagnostic = analysis.diagnostics(file_position.file_id, true).unwrap().pop().unwrap();
305312
let mut fix = diagnostic.fix.unwrap();
306313
let edit = fix.source_change.source_file_edits.pop().unwrap().edit;
307314
let target_file_contents = analysis.file_text(file_position.file_id).unwrap();
@@ -327,7 +334,7 @@ mod tests {
327334
let ra_fixture_after = &trim_indent(ra_fixture_after);
328335
let (analysis, file_pos) = analysis_and_position(ra_fixture_before);
329336
let current_file_id = file_pos.file_id;
330-
let diagnostic = analysis.diagnostics(current_file_id).unwrap().pop().unwrap();
337+
let diagnostic = analysis.diagnostics(current_file_id, true).unwrap().pop().unwrap();
331338
let mut fix = diagnostic.fix.unwrap();
332339
let edit = fix.source_change.source_file_edits.pop().unwrap();
333340
let changed_file_id = edit.file_id;
@@ -348,14 +355,14 @@ mod tests {
348355
let analysis = mock.analysis();
349356
let diagnostics = files
350357
.into_iter()
351-
.flat_map(|file_id| analysis.diagnostics(file_id).unwrap())
358+
.flat_map(|file_id| analysis.diagnostics(file_id, true).unwrap())
352359
.collect::<Vec<_>>();
353360
assert_eq!(diagnostics.len(), 0, "unexpected diagnostics:\n{:#?}", diagnostics);
354361
}
355362

356363
fn check_expect(ra_fixture: &str, expect: Expect) {
357364
let (analysis, file_id) = single_file(ra_fixture);
358-
let diagnostics = analysis.diagnostics(file_id).unwrap();
365+
let diagnostics = analysis.diagnostics(file_id, true).unwrap();
359366
expect.assert_debug_eq(&diagnostics)
360367
}
361368

crates/ra_ide/src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,8 +487,12 @@ impl Analysis {
487487
}
488488

489489
/// Computes the set of diagnostics for the given file.
490-
pub fn diagnostics(&self, file_id: FileId) -> Cancelable<Vec<Diagnostic>> {
491-
self.with_db(|db| diagnostics::diagnostics(db, file_id))
490+
pub fn diagnostics(
491+
&self,
492+
file_id: FileId,
493+
enable_experimental: bool,
494+
) -> Cancelable<Vec<Diagnostic>> {
495+
self.with_db(|db| diagnostics::diagnostics(db, file_id, enable_experimental))
492496
}
493497

494498
/// Returns the edit required to rename reference at the position to the new

crates/rust-analyzer/src/cli/analysis_bench.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub fn analysis_bench(
7070
match &what {
7171
BenchWhat::Highlight { .. } => {
7272
let res = do_work(&mut host, file_id, |analysis| {
73-
analysis.diagnostics(file_id).unwrap();
73+
analysis.diagnostics(file_id, true).unwrap();
7474
analysis.highlight_as_html(file_id, false).unwrap()
7575
});
7676
if verbosity.is_verbose() {

crates/rust-analyzer/src/cli/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub fn diagnostics(
4747
String::from("unknown")
4848
};
4949
println!("processing crate: {}, module: {}", crate_name, _vfs.file_path(file_id));
50-
for diagnostic in analysis.diagnostics(file_id).unwrap() {
50+
for diagnostic in analysis.diagnostics(file_id, true).unwrap() {
5151
if matches!(diagnostic.severity, Severity::Error) {
5252
found_error = true;
5353
}

crates/rust-analyzer/src/handlers.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ fn handle_fixes(
774774
None => {}
775775
};
776776

777-
let diagnostics = snap.analysis.diagnostics(file_id)?;
777+
let diagnostics = snap.analysis.diagnostics(file_id, snap.config.experimental_diagnostics)?;
778778

779779
let fixes_from_diagnostics = diagnostics
780780
.into_iter()
@@ -1040,7 +1040,7 @@ pub(crate) fn publish_diagnostics(
10401040
let line_index = snap.analysis.file_line_index(file_id)?;
10411041
let diagnostics: Vec<Diagnostic> = snap
10421042
.analysis
1043-
.diagnostics(file_id)?
1043+
.diagnostics(file_id, snap.config.experimental_diagnostics)?
10441044
.into_iter()
10451045
.map(|d| Diagnostic {
10461046
range: to_proto::range(&line_index, d.range),

0 commit comments

Comments
 (0)