mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 21:43:37 +00:00
fix: Ignore some warnings if they originate from within macro expansions
This commit is contained in:
parent
e8182a5bb3
commit
bb541c38d3
5 changed files with 77 additions and 40 deletions
|
@ -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
|
||||
|
|
|
@ -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>> {
|
||||
|
|
|
@ -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>> {
|
||||
|
|
|
@ -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!();
|
||||
}
|
||||
"#,
|
||||
);
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue