Skip to content

Commit 45272ef

Browse files
committed
Auto merge of rust-lang#14990 - HKalbasi:diagnostic-map, r=HKalbasi
Map our diagnostics to rustc and clippy's ones And control their severity by lint attributes `#[allow]`, `#[deny]` and ... . It doesn't work with proc macros and I would like to fix that before merge but I don't know how to do it.
2 parents daba334 + e55a1f1 commit 45272ef

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+628
-251
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/hir-ty/src/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ mod unsafe_check;
55
mod decl_check;
66

77
pub use crate::diagnostics::{
8-
decl_check::{incorrect_case, IncorrectCase},
8+
decl_check::{incorrect_case, CaseType, IncorrectCase},
99
expr::{
1010
record_literal_missing_fields, record_pattern_missing_fields, BodyValidationDiagnostic,
1111
},

crates/hir-ty/src/diagnostics/decl_check.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ pub fn incorrect_case(
5757

5858
#[derive(Debug)]
5959
pub enum CaseType {
60-
// `some_var`
60+
/// `some_var`
6161
LowerSnakeCase,
62-
// `SOME_CONST`
62+
/// `SOME_CONST`
6363
UpperSnakeCase,
64-
// `SomeStruct`
64+
/// `SomeStruct`
6565
UpperCamelCase,
6666
}
6767

crates/hir/src/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//!
44
//! This probably isn't the best way to do this -- ideally, diagnostics should
55
//! be expressed in terms of hir types themselves.
6-
pub use hir_ty::diagnostics::{IncoherentImpl, IncorrectCase};
6+
pub use hir_ty::diagnostics::{CaseType, IncoherentImpl, IncorrectCase};
77

88
use base_db::CrateId;
99
use cfg::{CfgExpr, CfgOptions};

crates/hir/src/lib.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ use crate::db::{DefDatabase, HirDatabase};
8989
pub use crate::{
9090
attrs::{HasAttrs, Namespace},
9191
diagnostics::{
92-
AnyDiagnostic, BreakOutsideOfLoop, ExpectedFunction, InactiveCode, IncoherentImpl,
93-
IncorrectCase, InvalidDeriveTarget, MacroDefError, MacroError, MacroExpansionParseError,
94-
MalformedDerive, MismatchedArgCount, MissingFields, MissingMatchArms, MissingUnsafe,
95-
MovedOutOfRef, NeedMut, NoSuchField, PrivateAssocItem, PrivateField,
96-
ReplaceFilterMapNextWithFindMap, TypeMismatch, TypedHole, UndeclaredLabel,
92+
AnyDiagnostic, BreakOutsideOfLoop, CaseType, ExpectedFunction, InactiveCode,
93+
IncoherentImpl, IncorrectCase, InvalidDeriveTarget, MacroDefError, MacroError,
94+
MacroExpansionParseError, MalformedDerive, MismatchedArgCount, MissingFields,
95+
MissingMatchArms, MissingUnsafe, MovedOutOfRef, NeedMut, NoSuchField, PrivateAssocItem,
96+
PrivateField, ReplaceFilterMapNextWithFindMap, TypeMismatch, TypedHole, UndeclaredLabel,
9797
UnimplementedBuiltinMacro, UnreachableLabel, UnresolvedExternCrate, UnresolvedField,
9898
UnresolvedImport, UnresolvedMacroCall, UnresolvedMethodCall, UnresolvedModule,
9999
UnresolvedProcMacro, UnusedMut,

crates/ide-diagnostics/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ cov-mark = "2.0.0-pre.1"
1616
either = "1.7.0"
1717
itertools = "0.10.5"
1818
serde_json = "1.0.86"
19+
once_cell = "1.17.0"
1920

2021
# local deps
2122
profile.workspace = true

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{Diagnostic, DiagnosticsContext};
1+
use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};
22

33
// Diagnostic: break-outside-of-loop
44
//
@@ -13,10 +13,11 @@ pub(crate) fn break_outside_of_loop(
1313
let construct = if d.is_break { "break" } else { "continue" };
1414
format!("{construct} outside of loop")
1515
};
16-
Diagnostic::new(
17-
"break-outside-of-loop",
16+
Diagnostic::new_with_syntax_node_ptr(
17+
ctx,
18+
DiagnosticCode::RustcHardError("E0268"),
1819
message,
19-
ctx.sema.diagnostics_display_range(d.expr.clone().map(|it| it.into())).range,
20+
d.expr.clone().map(|it| it.into()),
2021
)
2122
}
2223

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use hir::HirDisplay;
22

3-
use crate::{Diagnostic, DiagnosticsContext};
3+
use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};
44

55
// Diagnostic: expected-function
66
//
@@ -9,10 +9,11 @@ pub(crate) fn expected_function(
99
ctx: &DiagnosticsContext<'_>,
1010
d: &hir::ExpectedFunction,
1111
) -> Diagnostic {
12-
Diagnostic::new(
13-
"expected-function",
12+
Diagnostic::new_with_syntax_node_ptr(
13+
ctx,
14+
DiagnosticCode::RustcHardError("E0618"),
1415
format!("expected function, found {}", d.found.display(ctx.sema.db)),
15-
ctx.sema.diagnostics_display_range(d.call.clone().map(|it| it.into())).range,
16+
d.call.clone().map(|it| it.into()),
1617
)
1718
.experimental()
1819
}

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

+23-17
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::{fix, Diagnostic, Severity};
8+
use crate::{fix, Diagnostic, DiagnosticCode};
99

1010
pub(crate) fn field_shorthand(acc: &mut Vec<Diagnostic>, file_id: FileId, node: &SyntaxNode) {
1111
match_ast! {
@@ -46,14 +46,17 @@ fn check_expr_field_shorthand(
4646

4747
let field_range = record_field.syntax().text_range();
4848
acc.push(
49-
Diagnostic::new("use-field-shorthand", "Shorthand struct initialization", field_range)
50-
.severity(Severity::WeakWarning)
51-
.with_fixes(Some(vec![fix(
52-
"use_expr_field_shorthand",
53-
"Use struct shorthand initialization",
54-
SourceChange::from_text_edit(file_id, edit),
55-
field_range,
56-
)])),
49+
Diagnostic::new(
50+
DiagnosticCode::Clippy("redundant_field_names"),
51+
"Shorthand struct initialization",
52+
field_range,
53+
)
54+
.with_fixes(Some(vec![fix(
55+
"use_expr_field_shorthand",
56+
"Use struct shorthand initialization",
57+
SourceChange::from_text_edit(file_id, edit),
58+
field_range,
59+
)])),
5760
);
5861
}
5962
}
@@ -87,14 +90,17 @@ fn check_pat_field_shorthand(
8790

8891
let field_range = record_pat_field.syntax().text_range();
8992
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-
)])),
93+
Diagnostic::new(
94+
DiagnosticCode::Clippy("redundant_field_names"),
95+
"Shorthand struct pattern",
96+
field_range,
97+
)
98+
.with_fixes(Some(vec![fix(
99+
"use_pat_field_shorthand",
100+
"Use struct field shorthand",
101+
SourceChange::from_text_edit(file_id, edit),
102+
field_range,
103+
)])),
98104
);
99105
}
100106
}

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use cfg::DnfExpr;
22
use stdx::format_to;
33

4-
use crate::{Diagnostic, DiagnosticsContext, Severity};
4+
use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext, Severity};
55

66
// Diagnostic: inactive-code
77
//
@@ -27,13 +27,12 @@ pub(crate) fn inactive_code(
2727
format_to!(message, ": {}", inactive);
2828
}
2929
}
30-
30+
// FIXME: This shouldn't be a diagnostic
3131
let res = Diagnostic::new(
32-
"inactive-code",
32+
DiagnosticCode::Ra("inactive-code", Severity::WeakWarning),
3333
message,
3434
ctx.sema.diagnostics_display_range(d.node.clone()).range,
3535
)
36-
.severity(Severity::WeakWarning)
3736
.with_unused(true);
3837
Some(res)
3938
}

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
use hir::InFile;
22

3-
use crate::{Diagnostic, DiagnosticsContext, Severity};
3+
use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};
44

55
// Diagnostic: incoherent-impl
66
//
77
// This diagnostic is triggered if the targe type of an impl is from a foreign crate.
88
pub(crate) fn incoherent_impl(ctx: &DiagnosticsContext<'_>, d: &hir::IncoherentImpl) -> Diagnostic {
9-
Diagnostic::new(
10-
"incoherent-impl",
9+
Diagnostic::new_with_syntax_node_ptr(
10+
ctx,
11+
DiagnosticCode::RustcHardError("E0210"),
1112
format!("cannot define inherent `impl` for foreign type"),
12-
ctx.sema.diagnostics_display_range(InFile::new(d.file_id, d.impl_.clone().into())).range,
13+
InFile::new(d.file_id, d.impl_.clone().into()),
1314
)
14-
.severity(Severity::Error)
1515
}
1616

1717
#[cfg(test)]

0 commit comments

Comments
 (0)