aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src/diagnostics.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide/src/diagnostics.rs')
-rw-r--r--crates/ide/src/diagnostics.rs498
1 files changed, 0 insertions, 498 deletions
diff --git a/crates/ide/src/diagnostics.rs b/crates/ide/src/diagnostics.rs
deleted file mode 100644
index 815a633e5..000000000
--- a/crates/ide/src/diagnostics.rs
+++ /dev/null
@@ -1,498 +0,0 @@
1//! Collects diagnostics & fixits for a single file.
2//!
3//! The tricky bit here is that diagnostics are produced by hir in terms of
4//! macro-expanded files, but we need to present them to the users in terms of
5//! original files. So we need to map the ranges.
6
7mod break_outside_of_loop;
8mod inactive_code;
9mod incorrect_case;
10mod macro_error;
11mod mismatched_arg_count;
12mod missing_fields;
13mod missing_match_arms;
14mod missing_ok_or_some_in_tail_expr;
15mod missing_unsafe;
16mod no_such_field;
17mod remove_this_semicolon;
18mod replace_filter_map_next_with_find_map;
19mod unimplemented_builtin_macro;
20mod unlinked_file;
21mod unresolved_extern_crate;
22mod unresolved_import;
23mod unresolved_macro_call;
24mod unresolved_module;
25mod unresolved_proc_macro;
26
27mod field_shorthand;
28
29use hir::{diagnostics::AnyDiagnostic, Semantics};
30use ide_assists::AssistResolveStrategy;
31use ide_db::{base_db::SourceDatabase, RootDatabase};
32use itertools::Itertools;
33use rustc_hash::FxHashSet;
34use syntax::{
35 ast::{self, AstNode},
36 SyntaxNode, TextRange,
37};
38use text_edit::TextEdit;
39use unlinked_file::UnlinkedFile;
40
41use crate::{Assist, AssistId, AssistKind, FileId, Label, SourceChange};
42
43#[derive(Copy, Clone, Debug, PartialEq)]
44pub struct DiagnosticCode(pub &'static str);
45
46impl DiagnosticCode {
47 pub fn as_str(&self) -> &str {
48 self.0
49 }
50}
51
52#[derive(Debug)]
53pub struct Diagnostic {
54 pub code: DiagnosticCode,
55 pub message: String,
56 pub range: TextRange,
57 pub severity: Severity,
58 pub unused: bool,
59 pub experimental: bool,
60 pub fixes: Option<Vec<Assist>>,
61}
62
63impl Diagnostic {
64 fn new(code: &'static str, message: impl Into<String>, range: TextRange) -> Diagnostic {
65 let message = message.into();
66 Diagnostic {
67 code: DiagnosticCode(code),
68 message,
69 range,
70 severity: Severity::Error,
71 unused: false,
72 experimental: false,
73 fixes: None,
74 }
75 }
76
77 fn experimental(mut self) -> Diagnostic {
78 self.experimental = true;
79 self
80 }
81
82 fn severity(mut self, severity: Severity) -> Diagnostic {
83 self.severity = severity;
84 self
85 }
86
87 fn with_fixes(mut self, fixes: Option<Vec<Assist>>) -> Diagnostic {
88 self.fixes = fixes;
89 self
90 }
91
92 fn with_unused(mut self, unused: bool) -> Diagnostic {
93 self.unused = unused;
94 self
95 }
96}
97
98#[derive(Debug, Copy, Clone)]
99pub enum Severity {
100 Error,
101 WeakWarning,
102}
103
104#[derive(Default, Debug, Clone)]
105pub struct DiagnosticsConfig {
106 pub disable_experimental: bool,
107 pub disabled: FxHashSet<String>,
108}
109
110struct DiagnosticsContext<'a> {
111 config: &'a DiagnosticsConfig,
112 sema: Semantics<'a, RootDatabase>,
113 resolve: &'a AssistResolveStrategy,
114}
115
116pub(crate) fn diagnostics(
117 db: &RootDatabase,
118 config: &DiagnosticsConfig,
119 resolve: &AssistResolveStrategy,
120 file_id: FileId,
121) -> Vec<Diagnostic> {
122 let _p = profile::span("diagnostics");
123 let sema = Semantics::new(db);
124 let parse = db.parse(file_id);
125 let mut res = Vec::new();
126
127 // [#34344] Only take first 128 errors to prevent slowing down editor/ide, the number 128 is chosen arbitrarily.
128 res.extend(
129 parse.errors().iter().take(128).map(|err| {
130 Diagnostic::new("syntax-error", format!("Syntax Error: {}", err), err.range())
131 }),
132 );
133
134 for node in parse.tree().syntax().descendants() {
135 check_unnecessary_braces_in_use_statement(&mut res, file_id, &node);
136 field_shorthand::check(&mut res, file_id, &node);
137 }
138
139 let mut diags = Vec::new();
140 let module = sema.to_module_def(file_id);
141 if let Some(m) = module {
142 m.diagnostics(db, &mut diags)
143 }
144
145 let ctx = DiagnosticsContext { config, sema, resolve };
146 if module.is_none() {
147 let d = UnlinkedFile { file: file_id };
148 let d = unlinked_file::unlinked_file(&ctx, &d);
149 res.push(d)
150 }
151
152 for diag in diags {
153 #[rustfmt::skip]
154 let d = match diag {
155 AnyDiagnostic::BreakOutsideOfLoop(d) => break_outside_of_loop::break_outside_of_loop(&ctx, &d),
156 AnyDiagnostic::IncorrectCase(d) => incorrect_case::incorrect_case(&ctx, &d),
157 AnyDiagnostic::MacroError(d) => macro_error::macro_error(&ctx, &d),
158 AnyDiagnostic::MismatchedArgCount(d) => mismatched_arg_count::mismatched_arg_count(&ctx, &d),
159 AnyDiagnostic::MissingFields(d) => missing_fields::missing_fields(&ctx, &d),
160 AnyDiagnostic::MissingMatchArms(d) => missing_match_arms::missing_match_arms(&ctx, &d),
161 AnyDiagnostic::MissingOkOrSomeInTailExpr(d) => missing_ok_or_some_in_tail_expr::missing_ok_or_some_in_tail_expr(&ctx, &d),
162 AnyDiagnostic::MissingUnsafe(d) => missing_unsafe::missing_unsafe(&ctx, &d),
163 AnyDiagnostic::NoSuchField(d) => no_such_field::no_such_field(&ctx, &d),
164 AnyDiagnostic::RemoveThisSemicolon(d) => remove_this_semicolon::remove_this_semicolon(&ctx, &d),
165 AnyDiagnostic::ReplaceFilterMapNextWithFindMap(d) => replace_filter_map_next_with_find_map::replace_filter_map_next_with_find_map(&ctx, &d),
166 AnyDiagnostic::UnimplementedBuiltinMacro(d) => unimplemented_builtin_macro::unimplemented_builtin_macro(&ctx, &d),
167 AnyDiagnostic::UnresolvedExternCrate(d) => unresolved_extern_crate::unresolved_extern_crate(&ctx, &d),
168 AnyDiagnostic::UnresolvedImport(d) => unresolved_import::unresolved_import(&ctx, &d),
169 AnyDiagnostic::UnresolvedMacroCall(d) => unresolved_macro_call::unresolved_macro_call(&ctx, &d),
170 AnyDiagnostic::UnresolvedModule(d) => unresolved_module::unresolved_module(&ctx, &d),
171 AnyDiagnostic::UnresolvedProcMacro(d) => unresolved_proc_macro::unresolved_proc_macro(&ctx, &d),
172
173 AnyDiagnostic::InactiveCode(d) => match inactive_code::inactive_code(&ctx, &d) {
174 Some(it) => it,
175 None => continue,
176 }
177 };
178 res.push(d)
179 }
180
181 res.retain(|d| {
182 !ctx.config.disabled.contains(d.code.as_str())
183 && !(ctx.config.disable_experimental && d.experimental)
184 });
185
186 res
187}
188
189fn check_unnecessary_braces_in_use_statement(
190 acc: &mut Vec<Diagnostic>,
191 file_id: FileId,
192 node: &SyntaxNode,
193) -> Option<()> {
194 let use_tree_list = ast::UseTreeList::cast(node.clone())?;
195 if let Some((single_use_tree,)) = use_tree_list.use_trees().collect_tuple() {
196 // If there is a comment inside the bracketed `use`,
197 // assume it is a commented out module path and don't show diagnostic.
198 if use_tree_list.has_inner_comment() {
199 return Some(());
200 }
201
202 let use_range = use_tree_list.syntax().text_range();
203 let edit =
204 text_edit_for_remove_unnecessary_braces_with_self_in_use_statement(&single_use_tree)
205 .unwrap_or_else(|| {
206 let to_replace = single_use_tree.syntax().text().to_string();
207 let mut edit_builder = TextEdit::builder();
208 edit_builder.delete(use_range);
209 edit_builder.insert(use_range.start(), to_replace);
210 edit_builder.finish()
211 });
212
213 acc.push(
214 Diagnostic::new(
215 "unnecessary-braces",
216 "Unnecessary braces in use statement".to_string(),
217 use_range,
218 )
219 .severity(Severity::WeakWarning)
220 .with_fixes(Some(vec![fix(
221 "remove_braces",
222 "Remove unnecessary braces",
223 SourceChange::from_text_edit(file_id, edit),
224 use_range,
225 )])),
226 );
227 }
228
229 Some(())
230}
231
232fn text_edit_for_remove_unnecessary_braces_with_self_in_use_statement(
233 single_use_tree: &ast::UseTree,
234) -> Option<TextEdit> {
235 let use_tree_list_node = single_use_tree.syntax().parent()?;
236 if single_use_tree.path()?.segment()?.self_token().is_some() {
237 let start = use_tree_list_node.prev_sibling_or_token()?.text_range().start();
238 let end = use_tree_list_node.text_range().end();
239 return Some(TextEdit::delete(TextRange::new(start, end)));
240 }
241 None
242}
243
244fn fix(id: &'static str, label: &str, source_change: SourceChange, target: TextRange) -> Assist {
245 let mut res = unresolved_fix(id, label, target);
246 res.source_change = Some(source_change);
247 res
248}
249
250fn unresolved_fix(id: &'static str, label: &str, target: TextRange) -> Assist {
251 assert!(!id.contains(' '));
252 Assist {
253 id: AssistId(id, AssistKind::QuickFix),
254 label: Label::new(label),
255 group: None,
256 target,
257 source_change: None,
258 }
259}
260
261#[cfg(test)]
262mod tests {
263 use expect_test::Expect;
264 use ide_assists::AssistResolveStrategy;
265 use stdx::trim_indent;
266 use test_utils::{assert_eq_text, extract_annotations};
267
268 use crate::{fixture, DiagnosticsConfig};
269
270 /// Takes a multi-file input fixture with annotated cursor positions,
271 /// and checks that:
272 /// * a diagnostic is produced
273 /// * the first diagnostic fix trigger range touches the input cursor position
274 /// * that the contents of the file containing the cursor match `after` after the diagnostic fix is applied
275 #[track_caller]
276 pub(crate) fn check_fix(ra_fixture_before: &str, ra_fixture_after: &str) {
277 check_nth_fix(0, ra_fixture_before, ra_fixture_after);
278 }
279 /// Takes a multi-file input fixture with annotated cursor positions,
280 /// and checks that:
281 /// * a diagnostic is produced
282 /// * every diagnostic fixes trigger range touches the input cursor position
283 /// * that the contents of the file containing the cursor match `after` after each diagnostic fix is applied
284 pub(crate) fn check_fixes(ra_fixture_before: &str, ra_fixtures_after: Vec<&str>) {
285 for (i, ra_fixture_after) in ra_fixtures_after.iter().enumerate() {
286 check_nth_fix(i, ra_fixture_before, ra_fixture_after)
287 }
288 }
289
290 #[track_caller]
291 fn check_nth_fix(nth: usize, ra_fixture_before: &str, ra_fixture_after: &str) {
292 let after = trim_indent(ra_fixture_after);
293
294 let (analysis, file_position) = fixture::position(ra_fixture_before);
295 let diagnostic = analysis
296 .diagnostics(
297 &DiagnosticsConfig::default(),
298 AssistResolveStrategy::All,
299 file_position.file_id,
300 )
301 .unwrap()
302 .pop()
303 .expect("no diagnostics");
304 let fix = &diagnostic.fixes.expect("diagnostic misses fixes")[nth];
305 let actual = {
306 let source_change = fix.source_change.as_ref().unwrap();
307 let file_id = *source_change.source_file_edits.keys().next().unwrap();
308 let mut actual = analysis.file_text(file_id).unwrap().to_string();
309
310 for edit in source_change.source_file_edits.values() {
311 edit.apply(&mut actual);
312 }
313 actual
314 };
315
316 assert_eq_text!(&after, &actual);
317 assert!(
318 fix.target.contains_inclusive(file_position.offset),
319 "diagnostic fix range {:?} does not touch cursor position {:?}",
320 fix.target,
321 file_position.offset
322 );
323 }
324
325 /// Checks that there's a diagnostic *without* fix at `$0`.
326 pub(crate) fn check_no_fix(ra_fixture: &str) {
327 let (analysis, file_position) = fixture::position(ra_fixture);
328 let diagnostic = analysis
329 .diagnostics(
330 &DiagnosticsConfig::default(),
331 AssistResolveStrategy::All,
332 file_position.file_id,
333 )
334 .unwrap()
335 .pop()
336 .unwrap();
337 assert!(diagnostic.fixes.is_none(), "got a fix when none was expected: {:?}", diagnostic);
338 }
339
340 pub(crate) fn check_expect(ra_fixture: &str, expect: Expect) {
341 let (analysis, file_id) = fixture::file(ra_fixture);
342 let diagnostics = analysis
343 .diagnostics(&DiagnosticsConfig::default(), AssistResolveStrategy::All, file_id)
344 .unwrap();
345 expect.assert_debug_eq(&diagnostics)
346 }
347
348 #[track_caller]
349 pub(crate) fn check_diagnostics(ra_fixture: &str) {
350 let mut config = DiagnosticsConfig::default();
351 config.disabled.insert("inactive-code".to_string());
352 check_diagnostics_with_config(config, ra_fixture)
353 }
354
355 #[track_caller]
356 pub(crate) fn check_diagnostics_with_config(config: DiagnosticsConfig, ra_fixture: &str) {
357 let (analysis, files) = fixture::files(ra_fixture);
358 for file_id in files {
359 let diagnostics =
360 analysis.diagnostics(&config, AssistResolveStrategy::All, file_id).unwrap();
361
362 let expected = extract_annotations(&*analysis.file_text(file_id).unwrap());
363 let mut actual =
364 diagnostics.into_iter().map(|d| (d.range, d.message)).collect::<Vec<_>>();
365 actual.sort_by_key(|(range, _)| range.start());
366 assert_eq!(expected, actual);
367 }
368 }
369
370 #[test]
371 fn test_check_unnecessary_braces_in_use_statement() {
372 check_diagnostics(
373 r#"
374use a;
375use a::{c, d::e};
376
377mod a {
378 mod c {}
379 mod d {
380 mod e {}
381 }
382}
383"#,
384 );
385 check_diagnostics(
386 r#"
387use a;
388use a::{
389 c,
390 // d::e
391};
392
393mod a {
394 mod c {}
395 mod d {
396 mod e {}
397 }
398}
399"#,
400 );
401 check_fix(
402 r"
403 mod b {}
404 use {$0b};
405 ",
406 r"
407 mod b {}
408 use b;
409 ",
410 );
411 check_fix(
412 r"
413 mod b {}
414 use {b$0};
415 ",
416 r"
417 mod b {}
418 use b;
419 ",
420 );
421 check_fix(
422 r"
423 mod a { mod c {} }
424 use a::{c$0};
425 ",
426 r"
427 mod a { mod c {} }
428 use a::c;
429 ",
430 );
431 check_fix(
432 r"
433 mod a {}
434 use a::{self$0};
435 ",
436 r"
437 mod a {}
438 use a;
439 ",
440 );
441 check_fix(
442 r"
443 mod a { mod c {} mod d { mod e {} } }
444 use a::{c, d::{e$0}};
445 ",
446 r"
447 mod a { mod c {} mod d { mod e {} } }
448 use a::{c, d::e};
449 ",
450 );
451 }
452
453 #[test]
454 fn test_disabled_diagnostics() {
455 let mut config = DiagnosticsConfig::default();
456 config.disabled.insert("unresolved-module".into());
457
458 let (analysis, file_id) = fixture::file(r#"mod foo;"#);
459
460 let diagnostics =
461 analysis.diagnostics(&config, AssistResolveStrategy::All, file_id).unwrap();
462 assert!(diagnostics.is_empty());
463
464 let diagnostics = analysis
465 .diagnostics(&DiagnosticsConfig::default(), AssistResolveStrategy::All, file_id)
466 .unwrap();
467 assert!(!diagnostics.is_empty());
468 }
469
470 #[test]
471 fn import_extern_crate_clash_with_inner_item() {
472 // This is more of a resolver test, but doesn't really work with the hir_def testsuite.
473
474 check_diagnostics(
475 r#"
476//- /lib.rs crate:lib deps:jwt
477mod permissions;
478
479use permissions::jwt;
480
481fn f() {
482 fn inner() {}
483 jwt::Claims {}; // should resolve to the local one with 0 fields, and not get a diagnostic
484}
485
486//- /permissions.rs
487pub mod jwt {
488 pub struct Claims {}
489}
490
491//- /jwt/lib.rs crate:jwt
492pub struct Claims {
493 field: u8,
494}
495 "#,
496 );
497 }
498}