Skip to content

Commit f0618a8

Browse files
bors[bot]matklad
andauthored
Merge #9257
9257: internal: diagnostic code is mandatory r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 8c5c0ef + 500c909 commit f0618a8

File tree

4 files changed

+51
-79
lines changed

4 files changed

+51
-79
lines changed

crates/ide/src/diagnostics.rs

Lines changed: 28 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -51,28 +51,26 @@ impl DiagnosticCode {
5151

5252
#[derive(Debug)]
5353
pub struct Diagnostic {
54-
// pub name: Option<String>,
54+
pub code: DiagnosticCode,
5555
pub message: String,
5656
pub range: TextRange,
5757
pub severity: Severity,
58-
pub fixes: Option<Vec<Assist>>,
5958
pub unused: bool,
60-
pub code: Option<DiagnosticCode>,
6159
pub experimental: bool,
60+
pub fixes: Option<Vec<Assist>>,
6261
}
6362

6463
impl Diagnostic {
6564
fn new(code: &'static str, message: impl Into<String>, range: TextRange) -> Diagnostic {
6665
let message = message.into();
67-
let code = Some(DiagnosticCode(code));
68-
Self {
66+
Diagnostic {
67+
code: DiagnosticCode(code),
6968
message,
7069
range,
7170
severity: Severity::Error,
72-
fixes: None,
7371
unused: false,
74-
code,
7572
experimental: false,
73+
fixes: None,
7674
}
7775
}
7876

@@ -86,36 +84,14 @@ impl Diagnostic {
8684
self
8785
}
8886

89-
fn error(range: TextRange, message: String) -> Self {
90-
Self {
91-
message,
92-
range,
93-
severity: Severity::Error,
94-
fixes: None,
95-
unused: false,
96-
code: None,
97-
experimental: false,
98-
}
99-
}
100-
101-
fn hint(range: TextRange, message: String) -> Self {
102-
Self {
103-
message,
104-
range,
105-
severity: Severity::WeakWarning,
106-
fixes: None,
107-
unused: false,
108-
code: None,
109-
experimental: false,
110-
}
111-
}
112-
113-
fn with_fixes(self, fixes: Option<Vec<Assist>>) -> Self {
114-
Self { fixes, ..self }
87+
fn with_fixes(mut self, fixes: Option<Vec<Assist>>) -> Diagnostic {
88+
self.fixes = fixes;
89+
self
11590
}
11691

117-
fn with_unused(self, unused: bool) -> Self {
118-
Self { unused, ..self }
92+
fn with_unused(mut self, unused: bool) -> Diagnostic {
93+
self.unused = unused;
94+
self
11995
}
12096
}
12197

@@ -150,11 +126,9 @@ pub(crate) fn diagnostics(
150126

151127
// [#34344] Only take first 128 errors to prevent slowing down editor/ide, the number 128 is chosen arbitrarily.
152128
res.extend(
153-
parse
154-
.errors()
155-
.iter()
156-
.take(128)
157-
.map(|err| Diagnostic::error(err.range(), format!("Syntax Error: {}", err))),
129+
parse.errors().iter().take(128).map(|err| {
130+
Diagnostic::new("syntax-error", format!("Syntax Error: {}", err), err.range())
131+
}),
158132
);
159133

160134
for node in parse.tree().syntax().descendants() {
@@ -205,15 +179,8 @@ pub(crate) fn diagnostics(
205179
}
206180

207181
res.retain(|d| {
208-
if let Some(code) = d.code {
209-
if ctx.config.disabled.contains(code.as_str()) {
210-
return false;
211-
}
212-
}
213-
if ctx.config.disable_experimental && d.experimental {
214-
return false;
215-
}
216-
true
182+
!ctx.config.disabled.contains(d.code.as_str())
183+
&& !(ctx.config.disable_experimental && d.experimental)
217184
});
218185

219186
res
@@ -244,13 +211,18 @@ fn check_unnecessary_braces_in_use_statement(
244211
});
245212

246213
acc.push(
247-
Diagnostic::hint(use_range, "Unnecessary braces in use statement".to_string())
248-
.with_fixes(Some(vec![fix(
249-
"remove_braces",
250-
"Remove unnecessary braces",
251-
SourceChange::from_text_edit(file_id, edit),
252-
use_range,
253-
)])),
214+
Diagnostic::new(
215+
"unnecessary-braces",
216+
"Unnecessary braces in use statement".to_string(),
217+
use_range,
218+
)
219+
.severity(Severity::WeakWarning)
220+
.with_fixes(Some(vec![fix(
221+
"remove_braces",
222+
"Remove unnecessary braces",
223+
SourceChange::from_text_edit(file_id, edit),
224+
use_range,
225+
)])),
254226
);
255227
}
256228

crates/ide/src/diagnostics/field_shorthand.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use ide_db::{base_db::FileId, source_change::SourceChange};
55
use syntax::{ast, match_ast, AstNode, SyntaxNode};
66
use text_edit::TextEdit;
77

8-
use crate::{diagnostics::fix, Diagnostic};
8+
use crate::{diagnostics::fix, Diagnostic, Severity};
99

1010
pub(super) fn check(acc: &mut Vec<Diagnostic>, file_id: FileId, node: &SyntaxNode) {
1111
match_ast! {
@@ -46,7 +46,8 @@ fn check_expr_field_shorthand(
4646

4747
let field_range = record_field.syntax().text_range();
4848
acc.push(
49-
Diagnostic::hint(field_range, "Shorthand struct initialization".to_string())
49+
Diagnostic::new("use-field-shorthand", "Shorthand struct initialization", field_range)
50+
.severity(Severity::WeakWarning)
5051
.with_fixes(Some(vec![fix(
5152
"use_expr_field_shorthand",
5253
"Use struct shorthand initialization",
@@ -85,14 +86,16 @@ fn check_pat_field_shorthand(
8586
let edit = edit_builder.finish();
8687

8788
let field_range = record_pat_field.syntax().text_range();
88-
acc.push(Diagnostic::hint(field_range, "Shorthand struct pattern".to_string()).with_fixes(
89-
Some(vec![fix(
90-
"use_pat_field_shorthand",
91-
"Use struct field shorthand",
92-
SourceChange::from_text_edit(file_id, edit),
93-
field_range,
94-
)]),
95-
));
89+
acc.push(
90+
Diagnostic::new("use-field-shorthand", "Shorthand struct pattern", field_range)
91+
.severity(Severity::WeakWarning)
92+
.with_fixes(Some(vec![fix(
93+
"use_pat_field_shorthand",
94+
"Use struct field shorthand",
95+
SourceChange::from_text_edit(file_id, edit),
96+
field_range,
97+
)])),
98+
);
9699
}
97100
}
98101

crates/ide/src/diagnostics/unresolved_module.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,14 @@ mod baz {}
6565
expect![[r#"
6666
[
6767
Diagnostic {
68+
code: DiagnosticCode(
69+
"unresolved-module",
70+
),
6871
message: "unresolved module",
6972
range: 0..8,
7073
severity: Error,
74+
unused: false,
75+
experimental: false,
7176
fixes: Some(
7277
[
7378
Assist {
@@ -98,13 +103,6 @@ mod baz {}
98103
},
99104
],
100105
),
101-
unused: false,
102-
code: Some(
103-
DiagnosticCode(
104-
"unresolved-module",
105-
),
106-
),
107-
experimental: false,
108106
},
109107
]
110108
"#]],

crates/rust-analyzer/src/handlers.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,14 +1229,13 @@ pub(crate) fn publish_diagnostics(
12291229
.map(|d| Diagnostic {
12301230
range: to_proto::range(&line_index, d.range),
12311231
severity: Some(to_proto::diagnostic_severity(d.severity)),
1232-
code: d.code.map(|d| d.as_str().to_owned()).map(NumberOrString::String),
1233-
code_description: d.code.and_then(|code| {
1234-
lsp_types::Url::parse(&format!(
1232+
code: Some(NumberOrString::String(d.code.as_str().to_string())),
1233+
code_description: Some(lsp_types::CodeDescription {
1234+
href: lsp_types::Url::parse(&format!(
12351235
"https://rust-analyzer.github.io/manual.html#{}",
1236-
code.as_str()
1236+
d.code.as_str()
12371237
))
1238-
.ok()
1239-
.map(|href| lsp_types::CodeDescription { href })
1238+
.unwrap(),
12401239
}),
12411240
source: Some("rust-analyzer".to_string()),
12421241
message: d.message,

0 commit comments

Comments
 (0)