Skip to content

Commit

Permalink
Auto merge of #16861 - Veykril:macro-diag-exceptions, r=Veykril
Browse files Browse the repository at this point in the history
fix: Ignore some warnings if they originate from within macro expansions

These tend to be annoying noise as we can't handle `allow`s for them properly for the time being.
  • Loading branch information
bors committed Mar 17, 2024
2 parents c1122c9 + bb541c3 commit b6d1887
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 40 deletions.
28 changes: 17 additions & 11 deletions crates/ide-diagnostics/src/handlers/mutability_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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
Expand Down
19 changes: 13 additions & 6 deletions crates/ide-diagnostics/src/handlers/remove_trailing_return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,27 @@ 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()
.parent()
.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>> {
Expand Down
21 changes: 14 additions & 7 deletions crates/ide-diagnostics/src/handlers/remove_unnecessary_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>> {
Expand Down
28 changes: 17 additions & 11 deletions crates/ide-diagnostics/src/handlers/unused_variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>> {
Expand All @@ -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() {
Expand Down Expand Up @@ -193,7 +199,7 @@ fn main() {

#[test]
fn no_fix_for_marco() {
check_no_fix(
check_diagnostics(
r#"
macro_rules! my_macro {
() => {
Expand All @@ -202,7 +208,7 @@ macro_rules! my_macro {
}
fn main() {
$0my_macro!();
my_macro!();
}
"#,
);
Expand Down
21 changes: 16 additions & 5 deletions crates/ide-diagnostics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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),
Expand All @@ -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)
}
Expand Down

0 comments on commit b6d1887

Please sign in to comment.