Skip to content

Commit a54e481

Browse files
author
Jonas Schievink
committed
Simplify diagnostic construction, add unused field
1 parent f925735 commit a54e481

File tree

3 files changed

+55
-64
lines changed

3 files changed

+55
-64
lines changed

crates/ide/src/diagnostics.rs

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ pub struct Diagnostic {
3131
pub range: TextRange,
3232
pub severity: Severity,
3333
pub fix: Option<Fix>,
34+
pub unused: bool,
35+
}
36+
37+
impl Diagnostic {
38+
fn error(range: TextRange, message: String) -> Self {
39+
Self { message, range, severity: Severity::Error, fix: None, unused: false }
40+
}
41+
42+
fn hint(range: TextRange, message: String) -> Self {
43+
Self { message, range, severity: Severity::WeakWarning, fix: None, unused: false }
44+
}
45+
46+
fn with_fix(self, fix: Option<Fix>) -> Self {
47+
Self { fix, ..self }
48+
}
3449
}
3550

3651
#[derive(Debug)]
@@ -71,13 +86,13 @@ pub(crate) fn diagnostics(
7186
let mut res = Vec::new();
7287

7388
// [#34344] Only take first 128 errors to prevent slowing down editor/ide, the number 128 is chosen arbitrarily.
74-
res.extend(parse.errors().iter().take(128).map(|err| Diagnostic {
75-
// name: None,
76-
range: err.range(),
77-
message: format!("Syntax Error: {}", err),
78-
severity: Severity::Error,
79-
fix: None,
80-
}));
89+
res.extend(
90+
parse
91+
.errors()
92+
.iter()
93+
.take(128)
94+
.map(|err| Diagnostic::error(err.range(), format!("Syntax Error: {}", err))),
95+
);
8196

8297
for node in parse.tree().syntax().descendants() {
8398
check_unnecessary_braces_in_use_statement(&mut res, file_id, &node);
@@ -108,13 +123,8 @@ pub(crate) fn diagnostics(
108123
let mut sink = sink_builder
109124
// Diagnostics not handled above get no fix and default treatment.
110125
.build(|d| {
111-
res.borrow_mut().push(Diagnostic {
112-
// name: Some(d.name().into()),
113-
message: d.message(),
114-
range: sema.diagnostics_display_range(d).range,
115-
severity: Severity::Error,
116-
fix: None,
117-
})
126+
res.borrow_mut()
127+
.push(Diagnostic::error(sema.diagnostics_display_range(d).range, d.message()));
118128
});
119129

120130
if let Some(m) = sema.to_module_def(file_id) {
@@ -125,22 +135,11 @@ pub(crate) fn diagnostics(
125135
}
126136

127137
fn diagnostic_with_fix<D: DiagnosticWithFix>(d: &D, sema: &Semantics<RootDatabase>) -> Diagnostic {
128-
Diagnostic {
129-
// name: Some(d.name().into()),
130-
range: sema.diagnostics_display_range(d).range,
131-
message: d.message(),
132-
severity: Severity::Error,
133-
fix: d.fix(&sema),
134-
}
138+
Diagnostic::error(sema.diagnostics_display_range(d).range, d.message()).with_fix(d.fix(&sema))
135139
}
136140

137141
fn warning_with_fix<D: DiagnosticWithFix>(d: &D, sema: &Semantics<RootDatabase>) -> Diagnostic {
138-
Diagnostic {
139-
range: sema.diagnostics_display_range(d).range,
140-
message: d.message(),
141-
severity: Severity::WeakWarning,
142-
fix: d.fix(&sema),
143-
}
142+
Diagnostic::hint(sema.diagnostics_display_range(d).range, d.message()).with_fix(d.fix(&sema))
144143
}
145144

146145
fn check_unnecessary_braces_in_use_statement(
@@ -161,17 +160,14 @@ fn check_unnecessary_braces_in_use_statement(
161160
edit_builder.finish()
162161
});
163162

164-
acc.push(Diagnostic {
165-
// name: None,
166-
range: use_range,
167-
message: "Unnecessary braces in use statement".to_string(),
168-
severity: Severity::WeakWarning,
169-
fix: Some(Fix::new(
170-
"Remove unnecessary braces",
171-
SourceFileEdit { file_id, edit }.into(),
172-
use_range,
173-
)),
174-
});
163+
acc.push(
164+
Diagnostic::hint(use_range, "Unnecessary braces in use statement".to_string())
165+
.with_fix(Some(Fix::new(
166+
"Remove unnecessary braces",
167+
SourceFileEdit { file_id, edit }.into(),
168+
use_range,
169+
))),
170+
);
175171
}
176172

177173
Some(())
@@ -578,6 +574,7 @@ fn test_fn() {
578574
fix_trigger_range: 0..8,
579575
},
580576
),
577+
unused: false,
581578
},
582579
]
583580
"#]],

crates/ide/src/diagnostics/field_shorthand.rs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use ide_db::source_change::SourceFileEdit;
66
use syntax::{ast, match_ast, AstNode, SyntaxNode};
77
use text_edit::TextEdit;
88

9-
use crate::{Diagnostic, Fix, Severity};
9+
use crate::{Diagnostic, Fix};
1010

1111
pub(super) fn check(acc: &mut Vec<Diagnostic>, file_id: FileId, node: &SyntaxNode) {
1212
match_ast! {
@@ -46,17 +46,15 @@ fn check_expr_field_shorthand(
4646
let edit = edit_builder.finish();
4747

4848
let field_range = record_field.syntax().text_range();
49-
acc.push(Diagnostic {
50-
// name: None,
51-
range: field_range,
52-
message: "Shorthand struct initialization".to_string(),
53-
severity: Severity::WeakWarning,
54-
fix: Some(Fix::new(
55-
"Use struct shorthand initialization",
56-
SourceFileEdit { file_id, edit }.into(),
57-
field_range,
58-
)),
59-
});
49+
acc.push(
50+
Diagnostic::hint(field_range, "Shorthand struct initialization".to_string()).with_fix(
51+
Some(Fix::new(
52+
"Use struct shorthand initialization",
53+
SourceFileEdit { file_id, edit }.into(),
54+
field_range,
55+
)),
56+
),
57+
);
6058
}
6159
}
6260

@@ -88,17 +86,13 @@ fn check_pat_field_shorthand(
8886
let edit = edit_builder.finish();
8987

9088
let field_range = record_pat_field.syntax().text_range();
91-
acc.push(Diagnostic {
92-
// name: None,
93-
range: field_range,
94-
message: "Shorthand struct pattern".to_string(),
95-
severity: Severity::WeakWarning,
96-
fix: Some(Fix::new(
89+
acc.push(Diagnostic::hint(field_range, "Shorthand struct pattern".to_string()).with_fix(
90+
Some(Fix::new(
9791
"Use struct field shorthand",
9892
SourceFileEdit { file_id, edit }.into(),
9993
field_range,
10094
)),
101-
});
95+
));
10296
}
10397
}
10498

crates/rust-analyzer/src/handlers.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ use lsp_server::ErrorCode;
1616
use lsp_types::{
1717
CallHierarchyIncomingCall, CallHierarchyIncomingCallsParams, CallHierarchyItem,
1818
CallHierarchyOutgoingCall, CallHierarchyOutgoingCallsParams, CallHierarchyPrepareParams,
19-
CodeActionKind, CodeLens, Command, CompletionItem, Diagnostic, DocumentFormattingParams,
20-
DocumentHighlight, DocumentSymbol, FoldingRange, FoldingRangeParams, HoverContents, Location,
21-
Position, PrepareRenameResponse, Range, RenameParams, SemanticTokensDeltaParams,
22-
SemanticTokensFullDeltaResult, SemanticTokensParams, SemanticTokensRangeParams,
23-
SemanticTokensRangeResult, SemanticTokensResult, SymbolInformation, SymbolTag,
24-
TextDocumentIdentifier, Url, WorkspaceEdit,
19+
CodeActionKind, CodeLens, Command, CompletionItem, Diagnostic, DiagnosticTag,
20+
DocumentFormattingParams, DocumentHighlight, DocumentSymbol, FoldingRange, FoldingRangeParams,
21+
HoverContents, Location, Position, PrepareRenameResponse, Range, RenameParams,
22+
SemanticTokensDeltaParams, SemanticTokensFullDeltaResult, SemanticTokensParams,
23+
SemanticTokensRangeParams, SemanticTokensRangeResult, SemanticTokensResult, SymbolInformation,
24+
SymbolTag, TextDocumentIdentifier, Url, WorkspaceEdit,
2525
};
2626
use project_model::TargetKind;
2727
use serde::{Deserialize, Serialize};
@@ -1124,7 +1124,7 @@ pub(crate) fn publish_diagnostics(
11241124
source: Some("rust-analyzer".to_string()),
11251125
message: d.message,
11261126
related_information: None,
1127-
tags: None,
1127+
tags: if d.unused { Some(vec![DiagnosticTag::Unnecessary]) } else { None },
11281128
})
11291129
.collect();
11301130
Ok(diagnostics)

0 commit comments

Comments
 (0)