From bb541c38d33de08d98f8bec7c013c1141695e707 Mon Sep 17 00:00:00 2001 From: Lukas Wirth <lukastw97@gmail.com> Date: Sun, 17 Mar 2024 12:03:24 +0100 Subject: [PATCH] fix: Ignore some warnings if they originate from within macro expansions --- .../src/handlers/mutability_errors.rs | 28 +++++++++++-------- .../src/handlers/remove_trailing_return.rs | 19 +++++++++---- .../src/handlers/remove_unnecessary_else.rs | 21 +++++++++----- .../src/handlers/unused_variables.rs | 28 +++++++++++-------- crates/ide-diagnostics/src/lib.rs | 21 ++++++++++---- 5 files changed, 77 insertions(+), 40 deletions(-) diff --git a/crates/ide-diagnostics/src/handlers/mutability_errors.rs b/crates/ide-diagnostics/src/handlers/mutability_errors.rs index 34a0038295..6602aca2fa 100644 --- a/crates/ide-diagnostics/src/handlers/mutability_errors.rs +++ b/crates/ide-diagnostics/src/handlers/mutability_errors.rs @@ -7,7 +7,11 @@ use crate::{fix, Diagnostic, DiagnosticCode, DiagnosticsContext}; // Diagnostic: need-mut // // This diagnostic is triggered on mutating an immutable variable. -pub(crate) fn need_mut(ctx: &DiagnosticsContext<'_>, d: &hir::NeedMut) -> Diagnostic { +pub(crate) fn need_mut(ctx: &DiagnosticsContext<'_>, d: &hir::NeedMut) -> Option<Diagnostic> { + if d.span.file_id.macro_file().is_some() { + // FIXME: Our infra can't handle allow from within macro expansions rn + return None; + } let fixes = (|| { if d.local.is_ref(ctx.sema.db) { // There is no simple way to add `mut` to `ref x` and `ref mut x` @@ -29,17 +33,19 @@ pub(crate) fn need_mut(ctx: &DiagnosticsContext<'_>, d: &hir::NeedMut) -> Diagno use_range, )]) })(); - Diagnostic::new_with_syntax_node_ptr( - ctx, - // FIXME: `E0384` is not the only error that this diagnostic handles - DiagnosticCode::RustcHardError("E0384"), - format!( - "cannot mutate immutable variable `{}`", - d.local.name(ctx.sema.db).display(ctx.sema.db) - ), - d.span, + Some( + Diagnostic::new_with_syntax_node_ptr( + ctx, + // FIXME: `E0384` is not the only error that this diagnostic handles + DiagnosticCode::RustcHardError("E0384"), + format!( + "cannot mutate immutable variable `{}`", + d.local.name(ctx.sema.db).display(ctx.sema.db) + ), + d.span, + ) + .with_fixes(fixes), ) - .with_fixes(fixes) } // Diagnostic: unused-mut diff --git a/crates/ide-diagnostics/src/handlers/remove_trailing_return.rs b/crates/ide-diagnostics/src/handlers/remove_trailing_return.rs index 7a040e46e3..d831878044 100644 --- a/crates/ide-diagnostics/src/handlers/remove_trailing_return.rs +++ b/crates/ide-diagnostics/src/handlers/remove_trailing_return.rs @@ -12,7 +12,12 @@ use crate::{adjusted_display_range, fix, Diagnostic, DiagnosticCode, Diagnostics pub(crate) fn remove_trailing_return( ctx: &DiagnosticsContext<'_>, d: &RemoveTrailingReturn, -) -> Diagnostic { +) -> Option<Diagnostic> { + if d.return_expr.file_id.macro_file().is_some() { + // FIXME: Our infra can't handle allow from within macro expansions rn + return None; + } + let display_range = adjusted_display_range(ctx, d.return_expr, &|return_expr| { return_expr .syntax() @@ -20,12 +25,14 @@ pub(crate) fn remove_trailing_return( .and_then(ast::ExprStmt::cast) .map(|stmt| stmt.syntax().text_range()) }); - Diagnostic::new( - DiagnosticCode::Clippy("needless_return"), - "replace return <expr>; with <expr>", - display_range, + Some( + Diagnostic::new( + DiagnosticCode::Clippy("needless_return"), + "replace return <expr>; with <expr>", + display_range, + ) + .with_fixes(fixes(ctx, d)), ) - .with_fixes(fixes(ctx, d)) } fn fixes(ctx: &DiagnosticsContext<'_>, d: &RemoveTrailingReturn) -> Option<Vec<Assist>> { diff --git a/crates/ide-diagnostics/src/handlers/remove_unnecessary_else.rs b/crates/ide-diagnostics/src/handlers/remove_unnecessary_else.rs index f68e698238..073287d902 100644 --- a/crates/ide-diagnostics/src/handlers/remove_unnecessary_else.rs +++ b/crates/ide-diagnostics/src/handlers/remove_unnecessary_else.rs @@ -21,17 +21,24 @@ use crate::{ pub(crate) fn remove_unnecessary_else( ctx: &DiagnosticsContext<'_>, d: &RemoveUnnecessaryElse, -) -> Diagnostic { +) -> Option<Diagnostic> { + if d.if_expr.file_id.macro_file().is_some() { + // FIXME: Our infra can't handle allow from within macro expansions rn + return None; + } + let display_range = adjusted_display_range(ctx, d.if_expr, &|if_expr| { if_expr.else_token().as_ref().map(SyntaxToken::text_range) }); - Diagnostic::new( - DiagnosticCode::Ra("remove-unnecessary-else", Severity::WeakWarning), - "remove unnecessary else block", - display_range, + Some( + Diagnostic::new( + DiagnosticCode::Ra("remove-unnecessary-else", Severity::WeakWarning), + "remove unnecessary else block", + display_range, + ) + .experimental() + .with_fixes(fixes(ctx, d)), ) - .experimental() - .with_fixes(fixes(ctx, d)) } fn fixes(ctx: &DiagnosticsContext<'_>, d: &RemoveUnnecessaryElse) -> Option<Vec<Assist>> { diff --git a/crates/ide-diagnostics/src/handlers/unused_variables.rs b/crates/ide-diagnostics/src/handlers/unused_variables.rs index a9e1d07d7c..cd251faab9 100644 --- a/crates/ide-diagnostics/src/handlers/unused_variables.rs +++ b/crates/ide-diagnostics/src/handlers/unused_variables.rs @@ -14,18 +14,24 @@ use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext}; pub(crate) fn unused_variables( ctx: &DiagnosticsContext<'_>, d: &hir::UnusedVariable, -) -> Diagnostic { +) -> Option<Diagnostic> { let ast = d.local.primary_source(ctx.sema.db).syntax_ptr(); + if ast.file_id.macro_file().is_some() { + // FIXME: Our infra can't handle allow from within macro expansions rn + return None; + } let diagnostic_range = ctx.sema.diagnostics_display_range(ast); let var_name = d.local.primary_source(ctx.sema.db).syntax().to_string(); - Diagnostic::new_with_syntax_node_ptr( - ctx, - DiagnosticCode::RustcLint("unused_variables"), - "unused variable", - ast, + Some( + Diagnostic::new_with_syntax_node_ptr( + ctx, + DiagnosticCode::RustcLint("unused_variables"), + "unused variable", + ast, + ) + .with_fixes(fixes(&var_name, diagnostic_range, ast.file_id.is_macro())) + .experimental(), ) - .with_fixes(fixes(&var_name, diagnostic_range, ast.file_id.is_macro())) - .experimental() } fn fixes(var_name: &String, diagnostic_range: FileRange, is_in_marco: bool) -> Option<Vec<Assist>> { @@ -47,7 +53,7 @@ fn fixes(var_name: &String, diagnostic_range: FileRange, is_in_marco: bool) -> O #[cfg(test)] mod tests { - use crate::tests::{check_diagnostics, check_fix, check_no_fix}; + use crate::tests::{check_diagnostics, check_fix}; #[test] fn unused_variables_simple() { @@ -193,7 +199,7 @@ fn main() { #[test] fn no_fix_for_marco() { - check_no_fix( + check_diagnostics( r#" macro_rules! my_macro { () => { @@ -202,7 +208,7 @@ macro_rules! my_macro { } fn main() { - $0my_macro!(); + my_macro!(); } "#, ); diff --git a/crates/ide-diagnostics/src/lib.rs b/crates/ide-diagnostics/src/lib.rs index 0df6f0e037..c5e52d104d 100644 --- a/crates/ide-diagnostics/src/lib.rs +++ b/crates/ide-diagnostics/src/lib.rs @@ -330,7 +330,6 @@ pub fn diagnostics( } for diag in diags { - #[rustfmt::skip] let d = match diag { AnyDiagnostic::ExpectedFunction(d) => handlers::expected_function::expected_function(&ctx, &d), AnyDiagnostic::InactiveCode(d) => match handlers::inactive_code::inactive_code(&ctx, &d) { @@ -361,7 +360,10 @@ pub fn diagnostics( AnyDiagnostic::MissingMatchArms(d) => handlers::missing_match_arms::missing_match_arms(&ctx, &d), AnyDiagnostic::MissingUnsafe(d) => handlers::missing_unsafe::missing_unsafe(&ctx, &d), AnyDiagnostic::MovedOutOfRef(d) => handlers::moved_out_of_ref::moved_out_of_ref(&ctx, &d), - AnyDiagnostic::NeedMut(d) => handlers::mutability_errors::need_mut(&ctx, &d), + AnyDiagnostic::NeedMut(d) => match handlers::mutability_errors::need_mut(&ctx, &d) { + Some(it) => it, + None => continue, + }, AnyDiagnostic::NonExhaustiveLet(d) => handlers::non_exhaustive_let::non_exhaustive_let(&ctx, &d), AnyDiagnostic::NoSuchField(d) => handlers::no_such_field::no_such_field(&ctx, &d), AnyDiagnostic::PrivateAssocItem(d) => handlers::private_assoc_item::private_assoc_item(&ctx, &d), @@ -386,11 +388,20 @@ pub fn diagnostics( AnyDiagnostic::UnresolvedModule(d) => handlers::unresolved_module::unresolved_module(&ctx, &d), AnyDiagnostic::UnresolvedProcMacro(d) => handlers::unresolved_proc_macro::unresolved_proc_macro(&ctx, &d, config.proc_macros_enabled, config.proc_attr_macros_enabled), AnyDiagnostic::UnusedMut(d) => handlers::mutability_errors::unused_mut(&ctx, &d), - AnyDiagnostic::UnusedVariable(d) => handlers::unused_variables::unused_variables(&ctx, &d), + AnyDiagnostic::UnusedVariable(d) => match handlers::unused_variables::unused_variables(&ctx, &d) { + Some(it) => it, + None => continue, + }, AnyDiagnostic::BreakOutsideOfLoop(d) => handlers::break_outside_of_loop::break_outside_of_loop(&ctx, &d), AnyDiagnostic::MismatchedTupleStructPatArgCount(d) => handlers::mismatched_arg_count::mismatched_tuple_struct_pat_arg_count(&ctx, &d), - AnyDiagnostic::RemoveTrailingReturn(d) => handlers::remove_trailing_return::remove_trailing_return(&ctx, &d), - AnyDiagnostic::RemoveUnnecessaryElse(d) => handlers::remove_unnecessary_else::remove_unnecessary_else(&ctx, &d), + AnyDiagnostic::RemoveTrailingReturn(d) => match handlers::remove_trailing_return::remove_trailing_return(&ctx, &d) { + Some(it) => it, + None => continue, + }, + AnyDiagnostic::RemoveUnnecessaryElse(d) => match handlers::remove_unnecessary_else::remove_unnecessary_else(&ctx, &d) { + Some(it) => it, + None => continue, + }, }; res.push(d) }