fix: Ignore some warnings if they originate from within macro expansions

This commit is contained in:
Lukas Wirth 2024-03-17 12:03:24 +01:00
parent e8182a5bb3
commit bb541c38d3
5 changed files with 77 additions and 40 deletions

View file

@ -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

View file

@ -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>> {

View file

@ -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>> {

View file

@ -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!();
}
"#,
);