9257: internal: diagnostic code is mandatory r=matklad a=matklad

bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2021-06-13 19:17:57 +00:00 committed by GitHub
commit f0618a8f06
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 79 deletions

View file

@ -51,28 +51,26 @@ impl DiagnosticCode {
#[derive(Debug)] #[derive(Debug)]
pub struct Diagnostic { pub struct Diagnostic {
// pub name: Option<String>, pub code: DiagnosticCode,
pub message: String, pub message: String,
pub range: TextRange, pub range: TextRange,
pub severity: Severity, pub severity: Severity,
pub fixes: Option<Vec<Assist>>,
pub unused: bool, pub unused: bool,
pub code: Option<DiagnosticCode>,
pub experimental: bool, pub experimental: bool,
pub fixes: Option<Vec<Assist>>,
} }
impl Diagnostic { impl Diagnostic {
fn new(code: &'static str, message: impl Into<String>, range: TextRange) -> Diagnostic { fn new(code: &'static str, message: impl Into<String>, range: TextRange) -> Diagnostic {
let message = message.into(); let message = message.into();
let code = Some(DiagnosticCode(code)); Diagnostic {
Self { code: DiagnosticCode(code),
message, message,
range, range,
severity: Severity::Error, severity: Severity::Error,
fixes: None,
unused: false, unused: false,
code,
experimental: false, experimental: false,
fixes: None,
} }
} }
@ -86,36 +84,14 @@ impl Diagnostic {
self self
} }
fn error(range: TextRange, message: String) -> Self { fn with_fixes(mut self, fixes: Option<Vec<Assist>>) -> Diagnostic {
Self { self.fixes = fixes;
message, self
range,
severity: Severity::Error,
fixes: None,
unused: false,
code: None,
experimental: false,
}
} }
fn hint(range: TextRange, message: String) -> Self { fn with_unused(mut self, unused: bool) -> Diagnostic {
Self { self.unused = unused;
message, self
range,
severity: Severity::WeakWarning,
fixes: None,
unused: false,
code: None,
experimental: false,
}
}
fn with_fixes(self, fixes: Option<Vec<Assist>>) -> Self {
Self { fixes, ..self }
}
fn with_unused(self, unused: bool) -> Self {
Self { unused, ..self }
} }
} }
@ -150,11 +126,9 @@ pub(crate) fn diagnostics(
// [#34344] Only take first 128 errors to prevent slowing down editor/ide, the number 128 is chosen arbitrarily. // [#34344] Only take first 128 errors to prevent slowing down editor/ide, the number 128 is chosen arbitrarily.
res.extend( res.extend(
parse parse.errors().iter().take(128).map(|err| {
.errors() Diagnostic::new("syntax-error", format!("Syntax Error: {}", err), err.range())
.iter() }),
.take(128)
.map(|err| Diagnostic::error(err.range(), format!("Syntax Error: {}", err))),
); );
for node in parse.tree().syntax().descendants() { for node in parse.tree().syntax().descendants() {
@ -205,15 +179,8 @@ pub(crate) fn diagnostics(
} }
res.retain(|d| { res.retain(|d| {
if let Some(code) = d.code { !ctx.config.disabled.contains(d.code.as_str())
if ctx.config.disabled.contains(code.as_str()) { && !(ctx.config.disable_experimental && d.experimental)
return false;
}
}
if ctx.config.disable_experimental && d.experimental {
return false;
}
true
}); });
res res
@ -244,7 +211,12 @@ fn check_unnecessary_braces_in_use_statement(
}); });
acc.push( acc.push(
Diagnostic::hint(use_range, "Unnecessary braces in use statement".to_string()) Diagnostic::new(
"unnecessary-braces",
"Unnecessary braces in use statement".to_string(),
use_range,
)
.severity(Severity::WeakWarning)
.with_fixes(Some(vec![fix( .with_fixes(Some(vec![fix(
"remove_braces", "remove_braces",
"Remove unnecessary braces", "Remove unnecessary braces",

View file

@ -5,7 +5,7 @@ use ide_db::{base_db::FileId, source_change::SourceChange};
use syntax::{ast, match_ast, AstNode, SyntaxNode}; use syntax::{ast, match_ast, AstNode, SyntaxNode};
use text_edit::TextEdit; use text_edit::TextEdit;
use crate::{diagnostics::fix, Diagnostic}; use crate::{diagnostics::fix, Diagnostic, Severity};
pub(super) fn check(acc: &mut Vec<Diagnostic>, file_id: FileId, node: &SyntaxNode) { pub(super) fn check(acc: &mut Vec<Diagnostic>, file_id: FileId, node: &SyntaxNode) {
match_ast! { match_ast! {
@ -46,7 +46,8 @@ fn check_expr_field_shorthand(
let field_range = record_field.syntax().text_range(); let field_range = record_field.syntax().text_range();
acc.push( acc.push(
Diagnostic::hint(field_range, "Shorthand struct initialization".to_string()) Diagnostic::new("use-field-shorthand", "Shorthand struct initialization", field_range)
.severity(Severity::WeakWarning)
.with_fixes(Some(vec![fix( .with_fixes(Some(vec![fix(
"use_expr_field_shorthand", "use_expr_field_shorthand",
"Use struct shorthand initialization", "Use struct shorthand initialization",
@ -85,14 +86,16 @@ fn check_pat_field_shorthand(
let edit = edit_builder.finish(); let edit = edit_builder.finish();
let field_range = record_pat_field.syntax().text_range(); let field_range = record_pat_field.syntax().text_range();
acc.push(Diagnostic::hint(field_range, "Shorthand struct pattern".to_string()).with_fixes( acc.push(
Some(vec![fix( Diagnostic::new("use-field-shorthand", "Shorthand struct pattern", field_range)
.severity(Severity::WeakWarning)
.with_fixes(Some(vec![fix(
"use_pat_field_shorthand", "use_pat_field_shorthand",
"Use struct field shorthand", "Use struct field shorthand",
SourceChange::from_text_edit(file_id, edit), SourceChange::from_text_edit(file_id, edit),
field_range, field_range,
)]), )])),
)); );
} }
} }

View file

@ -65,9 +65,14 @@ mod baz {}
expect![[r#" expect![[r#"
[ [
Diagnostic { Diagnostic {
code: DiagnosticCode(
"unresolved-module",
),
message: "unresolved module", message: "unresolved module",
range: 0..8, range: 0..8,
severity: Error, severity: Error,
unused: false,
experimental: false,
fixes: Some( fixes: Some(
[ [
Assist { Assist {
@ -98,13 +103,6 @@ mod baz {}
}, },
], ],
), ),
unused: false,
code: Some(
DiagnosticCode(
"unresolved-module",
),
),
experimental: false,
}, },
] ]
"#]], "#]],

View file

@ -1229,14 +1229,13 @@ pub(crate) fn publish_diagnostics(
.map(|d| Diagnostic { .map(|d| Diagnostic {
range: to_proto::range(&line_index, d.range), range: to_proto::range(&line_index, d.range),
severity: Some(to_proto::diagnostic_severity(d.severity)), severity: Some(to_proto::diagnostic_severity(d.severity)),
code: d.code.map(|d| d.as_str().to_owned()).map(NumberOrString::String), code: Some(NumberOrString::String(d.code.as_str().to_string())),
code_description: d.code.and_then(|code| { code_description: Some(lsp_types::CodeDescription {
lsp_types::Url::parse(&format!( href: lsp_types::Url::parse(&format!(
"https://rust-analyzer.github.io/manual.html#{}", "https://rust-analyzer.github.io/manual.html#{}",
code.as_str() d.code.as_str()
)) ))
.ok() .unwrap(),
.map(|href| lsp_types::CodeDescription { href })
}), }),
source: Some("rust-analyzer".to_string()), source: Some("rust-analyzer".to_string()),
message: d.message, message: d.message,