Skip to content

Commit 604479c

Browse files
committed
internal: Use improved adjusted_display_range for all diagnostics
1 parent 9a832c4 commit 604479c

7 files changed

+27
-54
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use syntax::{
66
AstNode, AstPtr,
77
};
88

9-
use crate::{adjusted_display_range_new, Diagnostic, DiagnosticCode, DiagnosticsContext};
9+
use crate::{adjusted_display_range, Diagnostic, DiagnosticCode, DiagnosticsContext};
1010

1111
// Diagnostic: mismatched-tuple-struct-pat-arg-count
1212
//
@@ -50,7 +50,7 @@ fn invalid_args_range(
5050
expected: usize,
5151
found: usize,
5252
) -> FileRange {
53-
adjusted_display_range_new(ctx, source, &|expr| {
53+
adjusted_display_range(ctx, source, &|expr| {
5454
let (text_range, r_paren_token, expected_arg) = match expr {
5555
Either::Left(ast::Expr::CallExpr(call)) => {
5656
let arg_list = call.arg_list()?;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub(crate) fn trait_impl_incorrect_safety(
1919
},
2020
adjusted_display_range::<ast::Impl>(
2121
ctx,
22-
InFile { file_id: d.file_id, value: d.impl_.syntax_node_ptr() },
22+
InFile { file_id: d.file_id, value: d.impl_ },
2323
&|impl_| {
2424
if d.should_be_safe {
2525
Some(match (impl_.unsafe_token(), impl_.impl_token()) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub(crate) fn trait_impl_missing_assoc_item(
2525
format!("not all trait items implemented, missing: {missing}"),
2626
adjusted_display_range::<ast::Impl>(
2727
ctx,
28-
InFile { file_id: d.file_id, value: d.impl_.syntax_node_ptr() },
28+
InFile { file_id: d.file_id, value: d.impl_ },
2929
&|impl_| impl_.trait_().map(|t| t.syntax().text_range()),
3030
),
3131
)

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

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use either::Either;
12
use hir::{db::ExpandDatabase, ClosureStyle, HirDisplay, HirFileIdExt, InFile, Type};
23
use ide_db::{famous_defs::FamousDefs, source_change::SourceChange};
34
use syntax::{
@@ -13,33 +14,24 @@ use crate::{adjusted_display_range, fix, Assist, Diagnostic, DiagnosticCode, Dia
1314
// This diagnostic is triggered when the type of an expression or pattern does not match
1415
// the expected type.
1516
pub(crate) fn type_mismatch(ctx: &DiagnosticsContext<'_>, d: &hir::TypeMismatch) -> Diagnostic {
16-
let display_range = match &d.expr_or_pat.value {
17-
expr if ast::Expr::can_cast(expr.kind()) => adjusted_display_range::<ast::Expr>(
18-
ctx,
19-
InFile { file_id: d.expr_or_pat.file_id, value: expr.syntax_node_ptr() },
20-
&|expr| {
21-
let salient_token_range = match expr {
22-
ast::Expr::IfExpr(it) => it.if_token()?.text_range(),
23-
ast::Expr::LoopExpr(it) => it.loop_token()?.text_range(),
24-
ast::Expr::ForExpr(it) => it.for_token()?.text_range(),
25-
ast::Expr::WhileExpr(it) => it.while_token()?.text_range(),
26-
ast::Expr::BlockExpr(it) => it.stmt_list()?.r_curly_token()?.text_range(),
27-
ast::Expr::MatchExpr(it) => it.match_token()?.text_range(),
28-
ast::Expr::MethodCallExpr(it) => it.name_ref()?.ident_token()?.text_range(),
29-
ast::Expr::FieldExpr(it) => it.name_ref()?.ident_token()?.text_range(),
30-
ast::Expr::AwaitExpr(it) => it.await_token()?.text_range(),
31-
_ => return None,
32-
};
33-
34-
cov_mark::hit!(type_mismatch_range_adjustment);
35-
Some(salient_token_range)
36-
},
37-
),
38-
pat => ctx.sema.diagnostics_display_range(InFile {
39-
file_id: d.expr_or_pat.file_id,
40-
value: pat.syntax_node_ptr(),
41-
}),
42-
};
17+
let display_range = adjusted_display_range(ctx, d.expr_or_pat, &|node| {
18+
let Either::Left(expr) = node else { return None };
19+
let salient_token_range = match expr {
20+
ast::Expr::IfExpr(it) => it.if_token()?.text_range(),
21+
ast::Expr::LoopExpr(it) => it.loop_token()?.text_range(),
22+
ast::Expr::ForExpr(it) => it.for_token()?.text_range(),
23+
ast::Expr::WhileExpr(it) => it.while_token()?.text_range(),
24+
ast::Expr::BlockExpr(it) => it.stmt_list()?.r_curly_token()?.text_range(),
25+
ast::Expr::MatchExpr(it) => it.match_token()?.text_range(),
26+
ast::Expr::MethodCallExpr(it) => it.name_ref()?.ident_token()?.text_range(),
27+
ast::Expr::FieldExpr(it) => it.name_ref()?.ident_token()?.text_range(),
28+
ast::Expr::AwaitExpr(it) => it.await_token()?.text_range(),
29+
_ => return None,
30+
};
31+
32+
cov_mark::hit!(type_mismatch_range_adjustment);
33+
Some(salient_token_range)
34+
});
4335
let mut diag = Diagnostic::new(
4436
DiagnosticCode::RustcHardError("E0308"),
4537
format!(

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use ide_db::{
88
use syntax::{ast, AstNode, AstPtr};
99
use text_edit::TextEdit;
1010

11-
use crate::{adjusted_display_range_new, Diagnostic, DiagnosticCode, DiagnosticsContext};
11+
use crate::{adjusted_display_range, Diagnostic, DiagnosticCode, DiagnosticsContext};
1212

1313
// Diagnostic: unresolved-field
1414
//
@@ -29,7 +29,7 @@ pub(crate) fn unresolved_field(
2929
d.name.display(ctx.sema.db),
3030
d.receiver.display(ctx.sema.db)
3131
),
32-
adjusted_display_range_new(ctx, d.expr, &|expr| {
32+
adjusted_display_range(ctx, d.expr, &|expr| {
3333
Some(
3434
match expr {
3535
ast::Expr::MethodCallExpr(it) => it.name_ref(),

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use syntax::{
1111
};
1212
use text_edit::TextEdit;
1313

14-
use crate::{adjusted_display_range_new, Diagnostic, DiagnosticCode, DiagnosticsContext};
14+
use crate::{adjusted_display_range, Diagnostic, DiagnosticCode, DiagnosticsContext};
1515

1616
// Diagnostic: unresolved-method
1717
//
@@ -34,7 +34,7 @@ pub(crate) fn unresolved_method(
3434
d.name.display(ctx.sema.db),
3535
d.receiver.display(ctx.sema.db)
3636
),
37-
adjusted_display_range_new(ctx, d.expr, &|expr| {
37+
adjusted_display_range(ctx, d.expr, &|expr| {
3838
Some(
3939
match expr {
4040
ast::Expr::MethodCallExpr(it) => it.name_ref(),

crates/ide-diagnostics/src/lib.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ use ide_db::{
8989
use once_cell::sync::Lazy;
9090
use stdx::never;
9191
use syntax::{
92-
algo::find_node_at_range,
9392
ast::{self, AstNode},
9493
AstPtr, SyntaxNode, SyntaxNodePtr, TextRange,
9594
};
@@ -571,24 +570,6 @@ fn unresolved_fix(id: &'static str, label: &str, target: TextRange) -> Assist {
571570
}
572571

573572
fn adjusted_display_range<N: AstNode>(
574-
ctx: &DiagnosticsContext<'_>,
575-
diag_ptr: InFile<SyntaxNodePtr>,
576-
adj: &dyn Fn(N) -> Option<TextRange>,
577-
) -> FileRange {
578-
let FileRange { file_id, range } = ctx.sema.diagnostics_display_range(diag_ptr);
579-
580-
let source_file = ctx.sema.db.parse(file_id);
581-
FileRange {
582-
file_id,
583-
range: find_node_at_range::<N>(&source_file.syntax_node(), range)
584-
.filter(|it| it.syntax().text_range() == range)
585-
.and_then(adj)
586-
.unwrap_or(range),
587-
}
588-
}
589-
590-
// FIXME Replace the one above with this one?
591-
fn adjusted_display_range_new<N: AstNode>(
592573
ctx: &DiagnosticsContext<'_>,
593574
diag_ptr: InFile<AstPtr<N>>,
594575
adj: &dyn Fn(N) -> Option<TextRange>,

0 commit comments

Comments
 (0)