Skip to content

Commit b6d1887

Browse files
committed
Auto merge of rust-lang#16861 - Veykril:macro-diag-exceptions, r=Veykril
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.
2 parents c1122c9 + bb541c3 commit b6d1887

File tree

5 files changed

+77
-40
lines changed

5 files changed

+77
-40
lines changed

crates/ide-diagnostics/src/handlers/mutability_errors.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ use crate::{fix, Diagnostic, DiagnosticCode, DiagnosticsContext};
77
// Diagnostic: need-mut
88
//
99
// This diagnostic is triggered on mutating an immutable variable.
10-
pub(crate) fn need_mut(ctx: &DiagnosticsContext<'_>, d: &hir::NeedMut) -> Diagnostic {
10+
pub(crate) fn need_mut(ctx: &DiagnosticsContext<'_>, d: &hir::NeedMut) -> Option<Diagnostic> {
11+
if d.span.file_id.macro_file().is_some() {
12+
// FIXME: Our infra can't handle allow from within macro expansions rn
13+
return None;
14+
}
1115
let fixes = (|| {
1216
if d.local.is_ref(ctx.sema.db) {
1317
// 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
2933
use_range,
3034
)])
3135
})();
32-
Diagnostic::new_with_syntax_node_ptr(
33-
ctx,
34-
// FIXME: `E0384` is not the only error that this diagnostic handles
35-
DiagnosticCode::RustcHardError("E0384"),
36-
format!(
37-
"cannot mutate immutable variable `{}`",
38-
d.local.name(ctx.sema.db).display(ctx.sema.db)
39-
),
40-
d.span,
36+
Some(
37+
Diagnostic::new_with_syntax_node_ptr(
38+
ctx,
39+
// FIXME: `E0384` is not the only error that this diagnostic handles
40+
DiagnosticCode::RustcHardError("E0384"),
41+
format!(
42+
"cannot mutate immutable variable `{}`",
43+
d.local.name(ctx.sema.db).display(ctx.sema.db)
44+
),
45+
d.span,
46+
)
47+
.with_fixes(fixes),
4148
)
42-
.with_fixes(fixes)
4349
}
4450

4551
// Diagnostic: unused-mut

crates/ide-diagnostics/src/handlers/remove_trailing_return.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,27 @@ use crate::{adjusted_display_range, fix, Diagnostic, DiagnosticCode, Diagnostics
1212
pub(crate) fn remove_trailing_return(
1313
ctx: &DiagnosticsContext<'_>,
1414
d: &RemoveTrailingReturn,
15-
) -> Diagnostic {
15+
) -> Option<Diagnostic> {
16+
if d.return_expr.file_id.macro_file().is_some() {
17+
// FIXME: Our infra can't handle allow from within macro expansions rn
18+
return None;
19+
}
20+
1621
let display_range = adjusted_display_range(ctx, d.return_expr, &|return_expr| {
1722
return_expr
1823
.syntax()
1924
.parent()
2025
.and_then(ast::ExprStmt::cast)
2126
.map(|stmt| stmt.syntax().text_range())
2227
});
23-
Diagnostic::new(
24-
DiagnosticCode::Clippy("needless_return"),
25-
"replace return <expr>; with <expr>",
26-
display_range,
28+
Some(
29+
Diagnostic::new(
30+
DiagnosticCode::Clippy("needless_return"),
31+
"replace return <expr>; with <expr>",
32+
display_range,
33+
)
34+
.with_fixes(fixes(ctx, d)),
2735
)
28-
.with_fixes(fixes(ctx, d))
2936
}
3037

3138
fn fixes(ctx: &DiagnosticsContext<'_>, d: &RemoveTrailingReturn) -> Option<Vec<Assist>> {

crates/ide-diagnostics/src/handlers/remove_unnecessary_else.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,24 @@ use crate::{
2121
pub(crate) fn remove_unnecessary_else(
2222
ctx: &DiagnosticsContext<'_>,
2323
d: &RemoveUnnecessaryElse,
24-
) -> Diagnostic {
24+
) -> Option<Diagnostic> {
25+
if d.if_expr.file_id.macro_file().is_some() {
26+
// FIXME: Our infra can't handle allow from within macro expansions rn
27+
return None;
28+
}
29+
2530
let display_range = adjusted_display_range(ctx, d.if_expr, &|if_expr| {
2631
if_expr.else_token().as_ref().map(SyntaxToken::text_range)
2732
});
28-
Diagnostic::new(
29-
DiagnosticCode::Ra("remove-unnecessary-else", Severity::WeakWarning),
30-
"remove unnecessary else block",
31-
display_range,
33+
Some(
34+
Diagnostic::new(
35+
DiagnosticCode::Ra("remove-unnecessary-else", Severity::WeakWarning),
36+
"remove unnecessary else block",
37+
display_range,
38+
)
39+
.experimental()
40+
.with_fixes(fixes(ctx, d)),
3241
)
33-
.experimental()
34-
.with_fixes(fixes(ctx, d))
3542
}
3643

3744
fn fixes(ctx: &DiagnosticsContext<'_>, d: &RemoveUnnecessaryElse) -> Option<Vec<Assist>> {

crates/ide-diagnostics/src/handlers/unused_variables.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,24 @@ use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};
1414
pub(crate) fn unused_variables(
1515
ctx: &DiagnosticsContext<'_>,
1616
d: &hir::UnusedVariable,
17-
) -> Diagnostic {
17+
) -> Option<Diagnostic> {
1818
let ast = d.local.primary_source(ctx.sema.db).syntax_ptr();
19+
if ast.file_id.macro_file().is_some() {
20+
// FIXME: Our infra can't handle allow from within macro expansions rn
21+
return None;
22+
}
1923
let diagnostic_range = ctx.sema.diagnostics_display_range(ast);
2024
let var_name = d.local.primary_source(ctx.sema.db).syntax().to_string();
21-
Diagnostic::new_with_syntax_node_ptr(
22-
ctx,
23-
DiagnosticCode::RustcLint("unused_variables"),
24-
"unused variable",
25-
ast,
25+
Some(
26+
Diagnostic::new_with_syntax_node_ptr(
27+
ctx,
28+
DiagnosticCode::RustcLint("unused_variables"),
29+
"unused variable",
30+
ast,
31+
)
32+
.with_fixes(fixes(&var_name, diagnostic_range, ast.file_id.is_macro()))
33+
.experimental(),
2634
)
27-
.with_fixes(fixes(&var_name, diagnostic_range, ast.file_id.is_macro()))
28-
.experimental()
2935
}
3036

3137
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
4753

4854
#[cfg(test)]
4955
mod tests {
50-
use crate::tests::{check_diagnostics, check_fix, check_no_fix};
56+
use crate::tests::{check_diagnostics, check_fix};
5157

5258
#[test]
5359
fn unused_variables_simple() {
@@ -193,7 +199,7 @@ fn main() {
193199

194200
#[test]
195201
fn no_fix_for_marco() {
196-
check_no_fix(
202+
check_diagnostics(
197203
r#"
198204
macro_rules! my_macro {
199205
() => {
@@ -202,7 +208,7 @@ macro_rules! my_macro {
202208
}
203209
204210
fn main() {
205-
$0my_macro!();
211+
my_macro!();
206212
}
207213
"#,
208214
);

crates/ide-diagnostics/src/lib.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,6 @@ pub fn diagnostics(
330330
}
331331

332332
for diag in diags {
333-
#[rustfmt::skip]
334333
let d = match diag {
335334
AnyDiagnostic::ExpectedFunction(d) => handlers::expected_function::expected_function(&ctx, &d),
336335
AnyDiagnostic::InactiveCode(d) => match handlers::inactive_code::inactive_code(&ctx, &d) {
@@ -361,7 +360,10 @@ pub fn diagnostics(
361360
AnyDiagnostic::MissingMatchArms(d) => handlers::missing_match_arms::missing_match_arms(&ctx, &d),
362361
AnyDiagnostic::MissingUnsafe(d) => handlers::missing_unsafe::missing_unsafe(&ctx, &d),
363362
AnyDiagnostic::MovedOutOfRef(d) => handlers::moved_out_of_ref::moved_out_of_ref(&ctx, &d),
364-
AnyDiagnostic::NeedMut(d) => handlers::mutability_errors::need_mut(&ctx, &d),
363+
AnyDiagnostic::NeedMut(d) => match handlers::mutability_errors::need_mut(&ctx, &d) {
364+
Some(it) => it,
365+
None => continue,
366+
},
365367
AnyDiagnostic::NonExhaustiveLet(d) => handlers::non_exhaustive_let::non_exhaustive_let(&ctx, &d),
366368
AnyDiagnostic::NoSuchField(d) => handlers::no_such_field::no_such_field(&ctx, &d),
367369
AnyDiagnostic::PrivateAssocItem(d) => handlers::private_assoc_item::private_assoc_item(&ctx, &d),
@@ -386,11 +388,20 @@ pub fn diagnostics(
386388
AnyDiagnostic::UnresolvedModule(d) => handlers::unresolved_module::unresolved_module(&ctx, &d),
387389
AnyDiagnostic::UnresolvedProcMacro(d) => handlers::unresolved_proc_macro::unresolved_proc_macro(&ctx, &d, config.proc_macros_enabled, config.proc_attr_macros_enabled),
388390
AnyDiagnostic::UnusedMut(d) => handlers::mutability_errors::unused_mut(&ctx, &d),
389-
AnyDiagnostic::UnusedVariable(d) => handlers::unused_variables::unused_variables(&ctx, &d),
391+
AnyDiagnostic::UnusedVariable(d) => match handlers::unused_variables::unused_variables(&ctx, &d) {
392+
Some(it) => it,
393+
None => continue,
394+
},
390395
AnyDiagnostic::BreakOutsideOfLoop(d) => handlers::break_outside_of_loop::break_outside_of_loop(&ctx, &d),
391396
AnyDiagnostic::MismatchedTupleStructPatArgCount(d) => handlers::mismatched_arg_count::mismatched_tuple_struct_pat_arg_count(&ctx, &d),
392-
AnyDiagnostic::RemoveTrailingReturn(d) => handlers::remove_trailing_return::remove_trailing_return(&ctx, &d),
393-
AnyDiagnostic::RemoveUnnecessaryElse(d) => handlers::remove_unnecessary_else::remove_unnecessary_else(&ctx, &d),
397+
AnyDiagnostic::RemoveTrailingReturn(d) => match handlers::remove_trailing_return::remove_trailing_return(&ctx, &d) {
398+
Some(it) => it,
399+
None => continue,
400+
},
401+
AnyDiagnostic::RemoveUnnecessaryElse(d) => match handlers::remove_unnecessary_else::remove_unnecessary_else(&ctx, &d) {
402+
Some(it) => it,
403+
None => continue,
404+
},
394405
};
395406
res.push(d)
396407
}

0 commit comments

Comments
 (0)