Skip to main content

ide_diagnostics/
lib.rs

1//! Diagnostics rendering and fixits.
2//!
3//! Most of the diagnostics originate from the dark depth of the compiler, and
4//! are originally expressed in term of IR. When we emit the diagnostic, we are
5//! usually not in the position to decide how to best "render" it in terms of
6//! user-authored source code. We are especially not in the position to offer
7//! fixits, as the compiler completely lacks the infrastructure to edit the
8//! source code.
9//!
10//! Instead, we "bubble up" raw, structured diagnostics until the `hir` crate,
11//! where we "cook" them so that each diagnostic is formulated in terms of `hir`
12//! types. Well, at least that's the aspiration, the "cooking" is somewhat
13//! ad-hoc at the moment. Anyways, we get a bunch of ide-friendly diagnostic
14//! structs from hir, and we want to render them to unified serializable
15//! representation (span, level, message) here. If we can, we also provide
16//! fixits. By the way, that's why we want to keep diagnostics structured
17//! internally -- so that we have all the info to make fixes.
18//!
19//! We have one "handler" module per diagnostic code. Such a module contains
20//! rendering, optional fixes and tests. It's OK if some low-level compiler
21//! functionality ends up being tested via a diagnostic.
22//!
23//! There are also a couple of ad-hoc diagnostics implemented directly here, we
24//! don't yet have a great pattern for how to do them properly.
25
26#![cfg_attr(feature = "in-rust-tree", feature(rustc_private))]
27
28#[cfg(feature = "in-rust-tree")]
29extern crate rustc_driver as _;
30
31mod handlers {
32    pub(crate) mod array_pattern_without_fixed_length;
33    pub(crate) mod await_outside_of_async;
34    pub(crate) mod bad_rtn;
35    pub(crate) mod break_outside_of_loop;
36    pub(crate) mod cannot_be_dereferenced;
37    pub(crate) mod cannot_implicitly_deref_trait_object;
38    pub(crate) mod cannot_index_into;
39    pub(crate) mod duplicate_field;
40    pub(crate) mod elided_lifetimes_in_path;
41    pub(crate) mod expected_array_or_slice_pat;
42    pub(crate) mod expected_function;
43    pub(crate) mod explicit_drop_method_use;
44    pub(crate) mod fru_in_destructuring_assignment;
45    pub(crate) mod functional_record_update_on_non_struct;
46    pub(crate) mod generic_args_prohibited;
47    pub(crate) mod generic_default_refers_to_self;
48    pub(crate) mod inactive_code;
49    pub(crate) mod incoherent_impl;
50    pub(crate) mod incorrect_case;
51    pub(crate) mod incorrect_generics_len;
52    pub(crate) mod incorrect_generics_order;
53    pub(crate) mod infer_vars_not_allowed;
54    pub(crate) mod invalid_cast;
55    pub(crate) mod invalid_derive_target;
56    pub(crate) mod invalid_lhs_of_assignment;
57    pub(crate) mod invalid_range_pat_type;
58    pub(crate) mod macro_error;
59    pub(crate) mod malformed_derive;
60    pub(crate) mod method_call_illegal_sized_bound;
61    pub(crate) mod mismatched_arg_count;
62    pub(crate) mod mismatched_array_pat_len;
63    pub(crate) mod missing_fields;
64    pub(crate) mod missing_lifetime;
65    pub(crate) mod missing_match_arms;
66    pub(crate) mod missing_unsafe;
67    pub(crate) mod moved_out_of_ref;
68    pub(crate) mod mutability_errors;
69    pub(crate) mod mutable_ref;
70    pub(crate) mod no_such_field;
71    pub(crate) mod non_exhaustive_let;
72    pub(crate) mod non_exhaustive_record_expr;
73    pub(crate) mod non_exhaustive_record_pat;
74    pub(crate) mod parenthesized_generic_args_without_fn_trait;
75    pub(crate) mod pattern_arg_in_extern_fn;
76    pub(crate) mod private_assoc_item;
77    pub(crate) mod private_field;
78    pub(crate) mod remove_trailing_return;
79    pub(crate) mod remove_unnecessary_else;
80    pub(crate) mod replace_filter_map_next_with_find_map;
81    pub(crate) mod trait_impl_incorrect_safety;
82    pub(crate) mod trait_impl_missing_assoc_item;
83    pub(crate) mod trait_impl_orphan;
84    pub(crate) mod trait_impl_redundant_assoc_item;
85    pub(crate) mod type_mismatch;
86    pub(crate) mod type_must_be_known;
87    pub(crate) mod typed_hole;
88    pub(crate) mod undeclared_label;
89    pub(crate) mod unimplemented_builtin_macro;
90    pub(crate) mod unimplemented_trait;
91    pub(crate) mod union_expr_must_have_exactly_one_field;
92    pub(crate) mod unreachable_label;
93    pub(crate) mod unresolved_assoc_item;
94    pub(crate) mod unresolved_extern_crate;
95    pub(crate) mod unresolved_field;
96    pub(crate) mod unresolved_ident;
97    pub(crate) mod unresolved_import;
98    pub(crate) mod unresolved_macro_call;
99    pub(crate) mod unresolved_method;
100    pub(crate) mod unresolved_module;
101    pub(crate) mod unused_must_use;
102    pub(crate) mod unused_variables;
103
104    // The handlers below are unusual, the implement the diagnostics as well.
105    pub(crate) mod field_shorthand;
106    pub(crate) mod json_is_not_rust;
107    pub(crate) mod unlinked_file;
108    pub(crate) mod useless_braces;
109}
110
111#[cfg(test)]
112mod tests;
113
114use std::sync::LazyLock;
115
116use hir::{
117    Crate, DisplayTarget, InFile, MacroCallIdExt, Semantics, db::ExpandDatabase,
118    diagnostics::AnyDiagnostic,
119};
120use ide_db::{
121    FileId, FileRange, FxHashMap, FxHashSet, RootDatabase, Severity, SnippetCap,
122    assists::{Assist, AssistId, AssistResolveStrategy, ExprFillDefaultMode},
123    base_db::{ReleaseChannel, all_crates, toolchain_channel},
124    generated::lints::{CLIPPY_LINT_GROUPS, DEFAULT_LINT_GROUPS, DEFAULT_LINTS, Lint, LintGroup},
125    imports::insert_use::InsertUseConfig,
126    label::Label,
127    rename::RenameConfig,
128    source_change::SourceChange,
129};
130use syntax::{
131    AstPtr, Edition, SmolStr, SyntaxNode, SyntaxNodePtr, TextRange,
132    ast::{self, AstNode},
133};
134
135#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
136pub enum DiagnosticCode {
137    RustcHardError(&'static str),
138    SyntaxError,
139    RustcLint(&'static str),
140    Clippy(&'static str),
141    Ra(&'static str, Severity),
142}
143
144impl DiagnosticCode {
145    pub fn url(&self) -> String {
146        match self {
147            DiagnosticCode::RustcHardError(e) => {
148                format!("https://doc.rust-lang.org/stable/error_codes/{e}.html")
149            }
150            DiagnosticCode::SyntaxError => {
151                String::from("https://doc.rust-lang.org/stable/reference/")
152            }
153            DiagnosticCode::RustcLint(e) => {
154                format!("https://doc.rust-lang.org/rustc/?search={e}")
155            }
156            DiagnosticCode::Clippy(e) => {
157                format!("https://rust-lang.github.io/rust-clippy/master/#/{e}")
158            }
159            DiagnosticCode::Ra(e, _) => {
160                format!("https://rust-analyzer.github.io/book/diagnostics.html#{e}")
161            }
162        }
163    }
164
165    pub fn as_str(&self) -> &'static str {
166        match self {
167            DiagnosticCode::RustcHardError(r)
168            | DiagnosticCode::RustcLint(r)
169            | DiagnosticCode::Clippy(r)
170            | DiagnosticCode::Ra(r, _) => r,
171            DiagnosticCode::SyntaxError => "syntax-error",
172        }
173    }
174}
175
176#[derive(Debug)]
177pub struct Diagnostic {
178    pub code: DiagnosticCode,
179    pub message: String,
180    pub range: FileRange,
181    pub severity: Severity,
182    pub unused: bool,
183    pub experimental: bool,
184    pub fixes: Option<Vec<Assist>>,
185    // The node that will be affected by `#[allow]` and similar attributes.
186    pub main_node: Option<InFile<SyntaxNodePtr>>,
187}
188
189impl Diagnostic {
190    fn new(
191        code: DiagnosticCode,
192        message: impl Into<String>,
193        range: impl Into<FileRange>,
194    ) -> Diagnostic {
195        let message = message.into();
196        Diagnostic {
197            code,
198            message,
199            range: range.into(),
200            severity: match code {
201                DiagnosticCode::RustcHardError(_) | DiagnosticCode::SyntaxError => Severity::Error,
202                // FIXME: Rustc lints are not always warning, but the ones that are currently implemented are all warnings.
203                DiagnosticCode::RustcLint(_) => Severity::Warning,
204                // FIXME: We can make this configurable, and if the user uses `cargo clippy` on flycheck, we can
205                // make it normal warning.
206                DiagnosticCode::Clippy(_) => Severity::WeakWarning,
207                DiagnosticCode::Ra(_, s) => s,
208            },
209            unused: false,
210            experimental: true,
211            fixes: None,
212            main_node: None,
213        }
214    }
215
216    fn new_with_syntax_node_ptr(
217        ctx: &DiagnosticsContext<'_, '_>,
218        code: DiagnosticCode,
219        message: impl Into<String>,
220        node: InFile<SyntaxNodePtr>,
221    ) -> Diagnostic {
222        Diagnostic::new(code, message, ctx.sema.diagnostics_display_range(node))
223            .with_main_node(node)
224    }
225
226    fn stable(mut self) -> Diagnostic {
227        self.experimental = false;
228        self
229    }
230
231    fn with_main_node(mut self, main_node: InFile<SyntaxNodePtr>) -> Diagnostic {
232        self.main_node = Some(main_node);
233        self
234    }
235
236    fn with_fixes(mut self, fixes: Option<Vec<Assist>>) -> Diagnostic {
237        self.fixes = fixes;
238        self
239    }
240
241    fn with_unused(mut self, unused: bool) -> Diagnostic {
242        self.unused = unused;
243        self
244    }
245
246    fn main_node(&self, sema: &Semantics<'_, RootDatabase>) -> Option<InFile<SyntaxNode>> {
247        self.main_node.map(|ptr| ptr.with_value(sema.to_node_syntax(ptr))).or_else(|| {
248            let token = sema
249                .parse_guess_edition(self.range.file_id)
250                .syntax()
251                .token_at_offset(self.range.range.start())
252                .right_biased()?;
253            sema.descend_into_macros(token).into_iter().find_map(|token| {
254                let node = sema.ancestors_with_macros(token.parent().unwrap()).find(|node| {
255                    let original_range = sema.original_range(node);
256                    original_range.file_id.file_id(sema.db) == self.range.file_id
257                        && original_range.range.contains_range(self.range.range)
258                })?;
259                let file = sema.hir_file_for(&node);
260                Some(InFile::new(file, node))
261            })
262        })
263    }
264}
265
266#[derive(Debug, Clone)]
267pub struct DiagnosticsConfig {
268    /// Whether native diagnostics are enabled.
269    pub enabled: bool,
270    pub proc_macros_enabled: bool,
271    pub proc_attr_macros_enabled: bool,
272    pub disable_experimental: bool,
273    pub disabled: FxHashSet<String>,
274    pub expr_fill_default: ExprFillDefaultMode,
275    pub style_lints: bool,
276    // FIXME: We may want to include a whole `AssistConfig` here
277    pub snippet_cap: Option<SnippetCap>,
278    pub insert_use: InsertUseConfig,
279    pub prefer_no_std: bool,
280    pub prefer_prelude: bool,
281    pub prefer_absolute: bool,
282    pub term_search_fuel: u64,
283    pub term_search_borrowck: bool,
284    pub show_rename_conflicts: bool,
285}
286
287impl DiagnosticsConfig {
288    pub fn test_sample() -> Self {
289        use hir::PrefixKind;
290        use ide_db::imports::insert_use::ImportGranularity;
291
292        Self {
293            enabled: true,
294            proc_macros_enabled: Default::default(),
295            proc_attr_macros_enabled: Default::default(),
296            disable_experimental: Default::default(),
297            disabled: Default::default(),
298            expr_fill_default: Default::default(),
299            style_lints: true,
300            snippet_cap: SnippetCap::new(true),
301            insert_use: InsertUseConfig {
302                granularity: ImportGranularity::Item,
303                enforce_granularity: false,
304                prefix_kind: PrefixKind::Plain,
305                group: false,
306                skip_glob_imports: false,
307            },
308            prefer_no_std: false,
309            prefer_prelude: true,
310            prefer_absolute: false,
311            term_search_fuel: 400,
312            term_search_borrowck: true,
313            show_rename_conflicts: true,
314        }
315    }
316
317    pub fn rename_config(&self) -> RenameConfig {
318        RenameConfig { show_conflicts: self.show_rename_conflicts }
319    }
320}
321
322struct DiagnosticsContext<'a, 'db> {
323    config: &'a DiagnosticsConfig,
324    sema: Semantics<'db, RootDatabase>,
325    resolve: &'a AssistResolveStrategy,
326    edition: Edition,
327    display_target: DisplayTarget,
328    is_nightly: bool,
329}
330
331impl<'db> DiagnosticsContext<'_, 'db> {
332    fn db(&self) -> &'db RootDatabase {
333        self.sema.db
334    }
335}
336
337/// Request parser level diagnostics for the given [`FileId`].
338pub fn syntax_diagnostics(
339    db: &RootDatabase,
340    config: &DiagnosticsConfig,
341    file_id: FileId,
342) -> Vec<Diagnostic> {
343    let _p = tracing::info_span!("syntax_diagnostics").entered();
344
345    if config.disabled.contains("syntax-error") {
346        return Vec::new();
347    }
348
349    let sema = Semantics::new(db);
350    let editioned_file_id = sema.attach_first_edition(file_id);
351
352    let (file_id, _) = editioned_file_id.unpack(db);
353
354    // [#3434] Only take first 128 errors to prevent slowing down editor/ide, the number 128 is chosen arbitrarily.
355    editioned_file_id
356        .parse_errors(db)
357        .into_iter()
358        .flatten()
359        .take(128)
360        .map(|err| {
361            Diagnostic::new(
362                DiagnosticCode::SyntaxError,
363                format!("Syntax Error: {err}"),
364                FileRange { file_id, range: err.range() },
365            )
366        })
367        .collect()
368}
369
370/// Request semantic diagnostics for the given [`FileId`]. The produced diagnostics may point to other files
371/// due to macros.
372pub fn semantic_diagnostics(
373    db: &RootDatabase,
374    config: &DiagnosticsConfig,
375    resolve: &AssistResolveStrategy,
376    file_id: FileId,
377) -> Vec<Diagnostic> {
378    let _p = tracing::info_span!("semantic_diagnostics").entered();
379    let sema = Semantics::new(db);
380    let editioned_file_id = sema.attach_first_edition(file_id);
381
382    let (file_id, edition) = editioned_file_id.unpack(db);
383    let mut res = Vec::new();
384
385    let parse = sema.parse(editioned_file_id);
386
387    // FIXME: This iterates the entire file which is a rather expensive operation.
388    // We should implement these differently in some form?
389    // Salsa caching + incremental re-parse would be better here
390    for node in parse.syntax().descendants() {
391        handlers::useless_braces::useless_braces(db, &mut res, editioned_file_id, &node);
392        handlers::field_shorthand::field_shorthand(db, &mut res, editioned_file_id, &node);
393        handlers::json_is_not_rust::json_in_items(
394            &sema,
395            &mut res,
396            editioned_file_id,
397            &node,
398            config,
399            edition,
400        );
401    }
402
403    let module = sema.file_to_module_def(file_id);
404
405    let is_nightly = matches!(
406        module.and_then(|m| toolchain_channel(db, m.krate(db).into())),
407        Some(ReleaseChannel::Nightly) | None
408    );
409
410    let krate = match module {
411        Some(module) => module.krate(db),
412        None => {
413            match all_crates(db).last() {
414                Some(last) => (*last).into(),
415                // short-circuit, return an empty vec of diagnostics
416                None => return vec![],
417            }
418        }
419    };
420    let display_target = krate.to_display_target(db);
421    let ctx = DiagnosticsContext { config, sema, resolve, edition, is_nightly, display_target };
422
423    let mut diags = Vec::new();
424    match module {
425        // A bunch of parse errors in a file indicate some bigger structural parse changes in the
426        // file, so we skip semantic diagnostics so we can show these faster.
427        Some(m) => {
428            if editioned_file_id.parse_errors(db).is_none_or(|es| es.len() < 16) {
429                m.diagnostics(db, &mut diags, config.style_lints);
430            }
431        }
432        None => {
433            handlers::unlinked_file::unlinked_file(&ctx, &mut res, editioned_file_id.file_id(db))
434        }
435    }
436
437    for diag in diags {
438        let d = match diag {
439            AnyDiagnostic::AwaitOutsideOfAsync(d) => handlers::await_outside_of_async::await_outside_of_async(&ctx, &d),
440            AnyDiagnostic::CannotBeDereferenced(d) => handlers::cannot_be_dereferenced::cannot_be_dereferenced(&ctx, &d),
441            AnyDiagnostic::CannotImplicitlyDerefTraitObject(d) => handlers::cannot_implicitly_deref_trait_object::cannot_implicitly_deref_trait_object(&ctx, &d),
442            AnyDiagnostic::CannotIndexInto(d) => handlers::cannot_index_into::cannot_index_into(&ctx, &d),
443            AnyDiagnostic::CastToUnsized(d) => handlers::invalid_cast::cast_to_unsized(&ctx, &d),
444            AnyDiagnostic::InferVarsNotAllowed(d) => handlers::infer_vars_not_allowed::infer_vars_not_allowed(&ctx, &d),
445            AnyDiagnostic::ArrayPatternWithoutFixedLength(d) => {
446                handlers::array_pattern_without_fixed_length::array_pattern_without_fixed_length(
447                    &ctx, &d,
448                )
449            }
450            AnyDiagnostic::ExpectedArrayOrSlicePat(d) => handlers::expected_array_or_slice_pat::expected_array_or_slice_pat(&ctx, &d),
451            AnyDiagnostic::ExpectedFunction(d) => handlers::expected_function::expected_function(&ctx, &d),
452            AnyDiagnostic::FunctionalRecordUpdateOnNonStruct(d) => handlers::functional_record_update_on_non_struct::functional_record_update_on_non_struct(&ctx, &d),
453            AnyDiagnostic::InactiveCode(d) => match handlers::inactive_code::inactive_code(&ctx, &d) {
454                Some(it) => it,
455                None => continue,
456            }
457            AnyDiagnostic::IncoherentImpl(d) => handlers::incoherent_impl::incoherent_impl(&ctx, &d),
458            AnyDiagnostic::IncorrectCase(d) => handlers::incorrect_case::incorrect_case(&ctx, &d),
459            AnyDiagnostic::InvalidCast(d) => handlers::invalid_cast::invalid_cast(&ctx, &d),
460            AnyDiagnostic::InvalidDeriveTarget(d) => handlers::invalid_derive_target::invalid_derive_target(&ctx, &d),
461            AnyDiagnostic::MacroDefError(d) => handlers::macro_error::macro_def_error(&ctx, &d),
462            AnyDiagnostic::MacroError(d) => handlers::macro_error::macro_error(&ctx, &d),
463            AnyDiagnostic::MacroExpansionParseError(d) => {
464                // FIXME: Point to the correct error span here, not just the macro-call name
465                res.extend(d.errors.iter().take(16).map(|err| {
466                        Diagnostic::new(
467                            DiagnosticCode::SyntaxError,
468                            format!("Syntax Error in Expansion: {err}"),
469                            ctx.sema.diagnostics_display_range_for_range(d.range),
470                        )
471                }));
472                continue;
473            },
474            AnyDiagnostic::MalformedDerive(d) => handlers::malformed_derive::malformed_derive(&ctx, &d),
475            AnyDiagnostic::MethodCallIllegalSizedBound(d) => handlers::method_call_illegal_sized_bound::method_call_illegal_sized_bound(&ctx, &d),
476            AnyDiagnostic::MismatchedArgCount(d) => handlers::mismatched_arg_count::mismatched_arg_count(&ctx, &d),
477            AnyDiagnostic::MismatchedArrayPatLen(d) => handlers::mismatched_array_pat_len::mismatched_array_pat_len(&ctx, &d),
478            AnyDiagnostic::MissingFields(d) => handlers::missing_fields::missing_fields(&ctx, &d),
479            AnyDiagnostic::MissingMatchArms(d) => handlers::missing_match_arms::missing_match_arms(&ctx, &d),
480            AnyDiagnostic::MissingUnsafe(d) => handlers::missing_unsafe::missing_unsafe(&ctx, &d),
481            AnyDiagnostic::MovedOutOfRef(d) => handlers::moved_out_of_ref::moved_out_of_ref(&ctx, &d),
482            AnyDiagnostic::MutableRefBinding(d) => handlers::mutable_ref::mutable_ref_binding(&ctx, &d),
483            AnyDiagnostic::NeedMut(d) => match handlers::mutability_errors::need_mut(&ctx, &d) {
484                Some(it) => it,
485                None => continue,
486            },
487            AnyDiagnostic::NonExhaustiveLet(d) => handlers::non_exhaustive_let::non_exhaustive_let(&ctx, &d),
488            AnyDiagnostic::NonExhaustiveRecordExpr(d) => {
489                handlers::non_exhaustive_record_expr::non_exhaustive_record_expr(&ctx, &d)
490            }
491            AnyDiagnostic::NonExhaustiveRecordPat(d) => {
492                handlers::non_exhaustive_record_pat::non_exhaustive_record_pat(&ctx, &d)
493            }
494            AnyDiagnostic::NoSuchField(d) => handlers::no_such_field::no_such_field(&ctx, &d),
495            AnyDiagnostic::DuplicateField(d) => handlers::duplicate_field::duplicate_field(&ctx, &d),
496            AnyDiagnostic::PrivateAssocItem(d) => handlers::private_assoc_item::private_assoc_item(&ctx, &d),
497            AnyDiagnostic::PrivateField(d) => handlers::private_field::private_field(&ctx, &d),
498            AnyDiagnostic::ReplaceFilterMapNextWithFindMap(d) => handlers::replace_filter_map_next_with_find_map::replace_filter_map_next_with_find_map(&ctx, &d),
499            AnyDiagnostic::TraitImplIncorrectSafety(d) => handlers::trait_impl_incorrect_safety::trait_impl_incorrect_safety(&ctx, &d),
500            AnyDiagnostic::TraitImplMissingAssocItems(d) => handlers::trait_impl_missing_assoc_item::trait_impl_missing_assoc_item(&ctx, &d),
501            AnyDiagnostic::TraitImplRedundantAssocItems(d) => handlers::trait_impl_redundant_assoc_item::trait_impl_redundant_assoc_item(&ctx, &d),
502            AnyDiagnostic::TraitImplOrphan(d) => handlers::trait_impl_orphan::trait_impl_orphan(&ctx, &d),
503            AnyDiagnostic::TypedHole(d) => handlers::typed_hole::typed_hole(&ctx, &d),
504            AnyDiagnostic::TypeMismatch(d) => match handlers::type_mismatch::type_mismatch(&ctx, &d) {
505                Some(diag) => diag,
506                None => continue,
507            },
508            AnyDiagnostic::UndeclaredLabel(d) => handlers::undeclared_label::undeclared_label(&ctx, &d),
509            AnyDiagnostic::UnimplementedBuiltinMacro(d) => handlers::unimplemented_builtin_macro::unimplemented_builtin_macro(&ctx, &d),
510            AnyDiagnostic::UnreachableLabel(d) => handlers::unreachable_label::unreachable_label(&ctx, &d),
511            AnyDiagnostic::UnresolvedAssocItem(d) => handlers::unresolved_assoc_item::unresolved_assoc_item(&ctx, &d),
512            AnyDiagnostic::UnresolvedExternCrate(d) => handlers::unresolved_extern_crate::unresolved_extern_crate(&ctx, &d),
513            AnyDiagnostic::UnresolvedField(d) => handlers::unresolved_field::unresolved_field(&ctx, &d),
514            AnyDiagnostic::UnresolvedIdent(d) => handlers::unresolved_ident::unresolved_ident(&ctx, &d),
515            AnyDiagnostic::UnresolvedImport(d) => handlers::unresolved_import::unresolved_import(&ctx, &d),
516            AnyDiagnostic::UnresolvedMacroCall(d) => handlers::unresolved_macro_call::unresolved_macro_call(&ctx, &d),
517            AnyDiagnostic::UnresolvedMethodCall(d) => handlers::unresolved_method::unresolved_method(&ctx, &d),
518            AnyDiagnostic::UnresolvedModule(d) => handlers::unresolved_module::unresolved_module(&ctx, &d),
519            AnyDiagnostic::UnusedMustUse(d) => handlers::unused_must_use::unused_must_use(&ctx, &d),
520            AnyDiagnostic::UnusedMut(d) => match handlers::mutability_errors::unused_mut(&ctx, &d) {
521                Some(it) => it,
522                None => continue,
523            },
524            AnyDiagnostic::UnusedVariable(d) => match handlers::unused_variables::unused_variables(&ctx, &d) {
525                Some(it) => it,
526                None => continue,
527            },
528            AnyDiagnostic::BreakOutsideOfLoop(d) => handlers::break_outside_of_loop::break_outside_of_loop(&ctx, &d),
529            AnyDiagnostic::MismatchedTupleStructPatArgCount(d) => handlers::mismatched_arg_count::mismatched_tuple_struct_pat_arg_count(&ctx, &d),
530            AnyDiagnostic::RemoveTrailingReturn(d) => match handlers::remove_trailing_return::remove_trailing_return(&ctx, &d) {
531                Some(it) => it,
532                None => continue,
533            },
534            AnyDiagnostic::RemoveUnnecessaryElse(d) => match handlers::remove_unnecessary_else::remove_unnecessary_else(&ctx, &d) {
535                Some(it) => it,
536                None => continue,
537            },
538            AnyDiagnostic::GenericArgsProhibited(d) => handlers::generic_args_prohibited::generic_args_prohibited(&ctx, &d),
539            AnyDiagnostic::ParenthesizedGenericArgsWithoutFnTrait(d) => handlers::parenthesized_generic_args_without_fn_trait::parenthesized_generic_args_without_fn_trait(&ctx, &d),
540            AnyDiagnostic::BadRtn(d) => handlers::bad_rtn::bad_rtn(&ctx, &d),
541            AnyDiagnostic::IncorrectGenericsLen(d) => handlers::incorrect_generics_len::incorrect_generics_len(&ctx, &d),
542            AnyDiagnostic::IncorrectGenericsOrder(d) => handlers::incorrect_generics_order::incorrect_generics_order(&ctx, &d),
543            AnyDiagnostic::MissingLifetime(d) => handlers::missing_lifetime::missing_lifetime(&ctx, &d),
544            AnyDiagnostic::ElidedLifetimesInPath(d) => handlers::elided_lifetimes_in_path::elided_lifetimes_in_path(&ctx, &d),
545            AnyDiagnostic::GenericDefaultRefersToSelf(d) => handlers::generic_default_refers_to_self::generic_default_refers_to_self(&ctx, &d),
546            AnyDiagnostic::InvalidLhsOfAssignment(d) => handlers::invalid_lhs_of_assignment::invalid_lhs_of_assignment(&ctx, &d),
547            AnyDiagnostic::InvalidRangePatType(d) => handlers::invalid_range_pat_type::invalid_range_pat_type(&ctx, &d),
548            AnyDiagnostic::TypeMustBeKnown(d) => handlers::type_must_be_known::type_must_be_known(&ctx, &d),
549            AnyDiagnostic::PatternArgInExternFn(d) => handlers::pattern_arg_in_extern_fn::pattern_arg_in_extern_fn(&ctx, &d),
550            AnyDiagnostic::UnionExprMustHaveExactlyOneField(d) => handlers::union_expr_must_have_exactly_one_field::union_expr_must_have_exactly_one_field(&ctx, &d),
551            AnyDiagnostic::UnimplementedTrait(d) => handlers::unimplemented_trait::unimplemented_trait(&ctx, &d),
552            AnyDiagnostic::FruInDestructuringAssignment(d) => handlers::fru_in_destructuring_assignment::fru_in_destructuring_assignment(&ctx, &d),
553            AnyDiagnostic::ExplicitDropMethodUse(d) => handlers::explicit_drop_method_use::explicit_drop_method_use(&ctx, &d),
554        };
555        res.push(d)
556    }
557
558    res.retain(|d| {
559        !(ctx.config.disabled.contains(d.code.as_str())
560            || ctx.config.disable_experimental && d.experimental)
561    });
562
563    let mut lints = res
564        .iter_mut()
565        .filter(|it| matches!(it.code, DiagnosticCode::Clippy(_) | DiagnosticCode::RustcLint(_)))
566        .filter_map(|it| Some((it.main_node(&ctx.sema)?, it)))
567        .collect::<Vec<_>>();
568
569    // The edition isn't accurate (each diagnostics may have its own edition due to macros),
570    // but it's okay as it's only being used for error recovery.
571    handle_lints(&ctx.sema, file_id, krate, &mut lints, editioned_file_id.edition(db));
572
573    res.retain(|d| d.severity != Severity::Allow);
574
575    res.retain_mut(|diag| {
576        if let Some(node) = diag
577            .main_node
578            .map(|ptr| ptr.map(|node| node.to_node(&ctx.sema.parse_or_expand(ptr.file_id))))
579        {
580            handle_diag_from_macros(&ctx.sema, diag, &node)
581        } else {
582            true
583        }
584    });
585
586    res
587}
588
589/// Request both syntax and semantic diagnostics for the given [`FileId`].
590pub fn full_diagnostics(
591    db: &RootDatabase,
592    config: &DiagnosticsConfig,
593    resolve: &AssistResolveStrategy,
594    file_id: FileId,
595) -> Vec<Diagnostic> {
596    let mut res = syntax_diagnostics(db, config, file_id);
597    let sema = semantic_diagnostics(db, config, resolve, file_id);
598    res.extend(sema);
599    res
600}
601
602/// Returns whether to keep this diagnostic (or remove it).
603fn handle_diag_from_macros(
604    sema: &Semantics<'_, RootDatabase>,
605    diag: &mut Diagnostic,
606    node: &InFile<SyntaxNode>,
607) -> bool {
608    let Some(macro_file) = node.file_id.macro_file() else { return true };
609    let span_map = sema.db.expansion_span_map(macro_file);
610    let mut spans = span_map.spans_for_range(node.text_range());
611    if spans.any(|span| {
612        span.ctx.outer_expn(sema.db).is_some_and(|expansion| {
613            let macro_call = expansion.loc(sema.db);
614            // We don't want to show diagnostics for non-local macros at all, but proc macros authors
615            // seem to rely on being able to emit non-warning-free code, so we don't want to show warnings
616            // for them even when the proc macro comes from the same workspace (in rustc that's not a
617            // problem because it doesn't have the concept of workspaces, and proc macros always reside
618            // in a different crate).
619            !Crate::from(macro_call.def.krate).origin(sema.db).is_local()
620                || !macro_call.def.kind.is_declarative()
621        })
622    }) {
623        // Disable suggestions for external macros, they'll change library code and it's just bad.
624        diag.fixes = None;
625
626        // All Clippy lints report in macros, see https://github.com/rust-lang/rust-clippy/blob/903293b199364/declare_clippy_lint/src/lib.rs#L172.
627        if let DiagnosticCode::RustcLint(lint) = diag.code
628            && !LINTS_TO_REPORT_IN_EXTERNAL_MACROS.contains(lint)
629        {
630            return false;
631        };
632    }
633    true
634}
635
636struct BuiltLint {
637    lint: &'static Lint,
638    groups: Vec<&'static str>,
639}
640
641static RUSTC_LINTS: LazyLock<FxHashMap<&str, BuiltLint>> =
642    LazyLock::new(|| build_lints_map(DEFAULT_LINTS, DEFAULT_LINT_GROUPS, ""));
643
644static CLIPPY_LINTS: LazyLock<FxHashMap<&str, BuiltLint>> = LazyLock::new(|| {
645    build_lints_map(ide_db::generated::lints::CLIPPY_LINTS, CLIPPY_LINT_GROUPS, "clippy::")
646});
647
648// FIXME: Autogenerate this instead of enumerating by hand.
649static LINTS_TO_REPORT_IN_EXTERNAL_MACROS: LazyLock<FxHashSet<&str>> =
650    LazyLock::new(|| FxHashSet::from_iter([]));
651
652fn build_lints_map(
653    lints: &'static [Lint],
654    lint_group: &'static [LintGroup],
655    prefix: &'static str,
656) -> FxHashMap<&'static str, BuiltLint> {
657    let mut map_with_prefixes: FxHashMap<_, _> = lints
658        .iter()
659        .map(|lint| (lint.label, BuiltLint { lint, groups: vec![lint.label, "__RA_EVERY_LINT"] }))
660        .collect();
661    for g in lint_group {
662        let mut add_children = |label: &'static str| {
663            for child in g.children {
664                map_with_prefixes.get_mut(child).unwrap().groups.push(label);
665            }
666        };
667        add_children(g.lint.label);
668
669        if g.lint.label == "nonstandard_style" {
670            // Also add `bad_style`, which for some reason isn't listed in the groups.
671            add_children("bad_style");
672        }
673    }
674    map_with_prefixes.into_iter().map(|(k, v)| (k.strip_prefix(prefix).unwrap(), v)).collect()
675}
676
677fn handle_lints(
678    sema: &Semantics<'_, RootDatabase>,
679    file_id: FileId,
680    krate: hir::Crate,
681    diagnostics: &mut [(InFile<SyntaxNode>, &mut Diagnostic)],
682    edition: Edition,
683) {
684    for (node, diag) in diagnostics {
685        let lint = match diag.code {
686            DiagnosticCode::RustcLint(lint) => RUSTC_LINTS[lint].lint,
687            DiagnosticCode::Clippy(lint) => CLIPPY_LINTS[lint].lint,
688            _ => panic!("non-lint passed to `handle_lints()`"),
689        };
690        let default_severity = default_lint_severity(lint, edition);
691        if !(default_severity == Severity::Allow && diag.severity == Severity::WeakWarning) {
692            diag.severity = default_severity;
693        }
694
695        let mut diag_severity =
696            lint_severity_at(sema, file_id, krate, node, &lint_groups(&diag.code, edition));
697
698        if let outline_diag_severity @ Some(_) =
699            find_outline_mod_lint_severity(sema, file_id, krate, node, diag, edition)
700        {
701            diag_severity = outline_diag_severity;
702        }
703
704        if let Some(diag_severity) = diag_severity {
705            diag.severity = diag_severity;
706        }
707    }
708}
709
710fn default_lint_severity(lint: &Lint, edition: Edition) -> Severity {
711    if lint.deny_since.is_some_and(|e| edition >= e) {
712        Severity::Error
713    } else if lint.warn_since.is_some_and(|e| edition >= e) {
714        Severity::Warning
715    } else {
716        lint.default_severity
717    }
718}
719
720fn find_outline_mod_lint_severity(
721    sema: &Semantics<'_, RootDatabase>,
722    file_id: FileId,
723    krate: hir::Crate,
724    node: &InFile<SyntaxNode>,
725    diag: &Diagnostic,
726    edition: Edition,
727) -> Option<Severity> {
728    let mod_node = node.value.ancestors().find_map(ast::Module::cast)?;
729    if mod_node.item_list().is_some() {
730        // Inline modules will be handled by `fill_lint_attrs()`.
731        return None;
732    }
733
734    let mod_def = sema.to_module_def(&mod_node)?;
735    let module_source_file = sema.module_definition_node(mod_def);
736    let lint_groups = lint_groups(&diag.code, edition);
737    lint_attrs(
738        sema,
739        file_id,
740        krate,
741        ast::AnyHasAttrs::cast(module_source_file.value).expect("SourceFile always has attrs"),
742    )
743    .find_map(|(lint, severity)| lint_groups.contains(&lint).then_some(severity))
744}
745
746fn lint_severity_at(
747    sema: &Semantics<'_, RootDatabase>,
748    file_id: FileId,
749    krate: hir::Crate,
750    node: &InFile<SyntaxNode>,
751    lint_groups: &LintGroups,
752) -> Option<Severity> {
753    node.value
754        .ancestors()
755        .filter_map(ast::AnyHasAttrs::cast)
756        .find_map(|ancestor| {
757            lint_attrs(sema, file_id, krate, ancestor)
758                .find_map(|(lint, severity)| lint_groups.contains(&lint).then_some(severity))
759        })
760        .or_else(|| {
761            lint_severity_at(
762                sema,
763                file_id,
764                krate,
765                &sema.find_parent_file(node.file_id)?,
766                lint_groups,
767            )
768        })
769}
770
771// FIXME: Switch this to analysis' `expand_cfg_attr`.
772fn lint_attrs(
773    sema: &Semantics<'_, RootDatabase>,
774    file_id: FileId,
775    krate: hir::Crate,
776    ancestor: ast::AnyHasAttrs,
777) -> impl Iterator<Item = (SmolStr, Severity)> {
778    sema.lint_attrs(file_id, krate, ancestor).rev().map(|(lint_attr, lint)| {
779        let severity = match lint_attr {
780            hir::LintAttr::Allow | hir::LintAttr::Expect => Severity::Allow,
781            hir::LintAttr::Warn => Severity::Warning,
782            hir::LintAttr::Deny | hir::LintAttr::Forbid => Severity::Error,
783        };
784        (lint, severity)
785    })
786}
787
788#[derive(Debug)]
789struct LintGroups {
790    groups: &'static [&'static str],
791    inside_warnings: bool,
792}
793
794impl LintGroups {
795    fn contains(&self, group: &str) -> bool {
796        self.groups.contains(&group) || (self.inside_warnings && group == "warnings")
797    }
798}
799
800fn lint_groups(lint: &DiagnosticCode, edition: Edition) -> LintGroups {
801    let (groups, inside_warnings) = match lint {
802        DiagnosticCode::RustcLint(name) => {
803            let lint = &RUSTC_LINTS[name];
804            let inside_warnings = default_lint_severity(lint.lint, edition) == Severity::Warning;
805            (&lint.groups, inside_warnings)
806        }
807        DiagnosticCode::Clippy(name) => {
808            let lint = &CLIPPY_LINTS[name];
809            let inside_warnings = default_lint_severity(lint.lint, edition) == Severity::Warning;
810            (&lint.groups, inside_warnings)
811        }
812        _ => panic!("non-lint passed to `handle_lints()`"),
813    };
814    LintGroups { groups, inside_warnings }
815}
816
817fn fix(id: &'static str, label: &str, source_change: SourceChange, target: TextRange) -> Assist {
818    let mut res = unresolved_fix(id, label, target);
819    res.source_change = Some(source_change);
820    res
821}
822
823fn unresolved_fix(id: &'static str, label: &str, target: TextRange) -> Assist {
824    assert!(!id.contains(' '));
825    Assist {
826        id: AssistId::quick_fix(id),
827        label: Label::new(label.to_owned()),
828        group: None,
829        target,
830        source_change: None,
831        command: None,
832    }
833}
834
835fn adjusted_display_range<N: AstNode>(
836    ctx: &DiagnosticsContext<'_, '_>,
837    diag_ptr: InFile<AstPtr<N>>,
838    adj: &dyn Fn(N) -> Option<TextRange>,
839) -> FileRange {
840    let source_file = ctx.sema.parse_or_expand(diag_ptr.file_id);
841    let node = diag_ptr.value.to_node(&source_file);
842    let hir::FileRange { file_id, range } = diag_ptr
843        .with_value(adj(node).unwrap_or_else(|| diag_ptr.value.text_range()))
844        .original_node_file_range_rooted(ctx.sema.db);
845    ide_db::FileRange { file_id: file_id.file_id(ctx.sema.db), range }
846}