Skip to content

Commit

Permalink
Auto merge of #14990 - HKalbasi:diagnostic-map, r=HKalbasi
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Jul 3, 2023
2 parents daba334 + e55a1f1 commit 45272ef
Show file tree
Hide file tree
Showing 44 changed files with 628 additions and 251 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/hir-ty/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod unsafe_check;
mod decl_check;

pub use crate::diagnostics::{
decl_check::{incorrect_case, IncorrectCase},
decl_check::{incorrect_case, CaseType, IncorrectCase},
expr::{
record_literal_missing_fields, record_pattern_missing_fields, BodyValidationDiagnostic,
},
Expand Down
6 changes: 3 additions & 3 deletions crates/hir-ty/src/diagnostics/decl_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ pub fn incorrect_case(

#[derive(Debug)]
pub enum CaseType {
// `some_var`
/// `some_var`
LowerSnakeCase,
// `SOME_CONST`
/// `SOME_CONST`
UpperSnakeCase,
// `SomeStruct`
/// `SomeStruct`
UpperCamelCase,
}

Expand Down
2 changes: 1 addition & 1 deletion crates/hir/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//!
//! This probably isn't the best way to do this -- ideally, diagnostics should
//! be expressed in terms of hir types themselves.
pub use hir_ty::diagnostics::{IncoherentImpl, IncorrectCase};
pub use hir_ty::diagnostics::{CaseType, IncoherentImpl, IncorrectCase};

use base_db::CrateId;
use cfg::{CfgExpr, CfgOptions};
Expand Down
10 changes: 5 additions & 5 deletions crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ use crate::db::{DefDatabase, HirDatabase};
pub use crate::{
attrs::{HasAttrs, Namespace},
diagnostics::{
AnyDiagnostic, BreakOutsideOfLoop, ExpectedFunction, InactiveCode, IncoherentImpl,
IncorrectCase, InvalidDeriveTarget, MacroDefError, MacroError, MacroExpansionParseError,
MalformedDerive, MismatchedArgCount, MissingFields, MissingMatchArms, MissingUnsafe,
MovedOutOfRef, NeedMut, NoSuchField, PrivateAssocItem, PrivateField,
ReplaceFilterMapNextWithFindMap, TypeMismatch, TypedHole, UndeclaredLabel,
AnyDiagnostic, BreakOutsideOfLoop, CaseType, ExpectedFunction, InactiveCode,
IncoherentImpl, IncorrectCase, InvalidDeriveTarget, MacroDefError, MacroError,
MacroExpansionParseError, MalformedDerive, MismatchedArgCount, MissingFields,
MissingMatchArms, MissingUnsafe, MovedOutOfRef, NeedMut, NoSuchField, PrivateAssocItem,
PrivateField, ReplaceFilterMapNextWithFindMap, TypeMismatch, TypedHole, UndeclaredLabel,
UnimplementedBuiltinMacro, UnreachableLabel, UnresolvedExternCrate, UnresolvedField,
UnresolvedImport, UnresolvedMacroCall, UnresolvedMethodCall, UnresolvedModule,
UnresolvedProcMacro, UnusedMut,
Expand Down
1 change: 1 addition & 0 deletions crates/ide-diagnostics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ cov-mark = "2.0.0-pre.1"
either = "1.7.0"
itertools = "0.10.5"
serde_json = "1.0.86"
once_cell = "1.17.0"

# local deps
profile.workspace = true
Expand Down
9 changes: 5 additions & 4 deletions crates/ide-diagnostics/src/handlers/break_outside_of_loop.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Diagnostic, DiagnosticsContext};
use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};

// Diagnostic: break-outside-of-loop
//
Expand All @@ -13,10 +13,11 @@ pub(crate) fn break_outside_of_loop(
let construct = if d.is_break { "break" } else { "continue" };
format!("{construct} outside of loop")
};
Diagnostic::new(
"break-outside-of-loop",
Diagnostic::new_with_syntax_node_ptr(
ctx,
DiagnosticCode::RustcHardError("E0268"),
message,
ctx.sema.diagnostics_display_range(d.expr.clone().map(|it| it.into())).range,
d.expr.clone().map(|it| it.into()),
)
}

Expand Down
9 changes: 5 additions & 4 deletions crates/ide-diagnostics/src/handlers/expected_function.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use hir::HirDisplay;

use crate::{Diagnostic, DiagnosticsContext};
use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};

// Diagnostic: expected-function
//
Expand All @@ -9,10 +9,11 @@ pub(crate) fn expected_function(
ctx: &DiagnosticsContext<'_>,
d: &hir::ExpectedFunction,
) -> Diagnostic {
Diagnostic::new(
"expected-function",
Diagnostic::new_with_syntax_node_ptr(
ctx,
DiagnosticCode::RustcHardError("E0618"),
format!("expected function, found {}", d.found.display(ctx.sema.db)),
ctx.sema.diagnostics_display_range(d.call.clone().map(|it| it.into())).range,
d.call.clone().map(|it| it.into()),
)
.experimental()
}
Expand Down
40 changes: 23 additions & 17 deletions crates/ide-diagnostics/src/handlers/field_shorthand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use ide_db::{base_db::FileId, source_change::SourceChange};
use syntax::{ast, match_ast, AstNode, SyntaxNode};
use text_edit::TextEdit;

use crate::{fix, Diagnostic, Severity};
use crate::{fix, Diagnostic, DiagnosticCode};

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

let field_range = record_field.syntax().text_range();
acc.push(
Diagnostic::new("use-field-shorthand", "Shorthand struct initialization", field_range)
.severity(Severity::WeakWarning)
.with_fixes(Some(vec![fix(
"use_expr_field_shorthand",
"Use struct shorthand initialization",
SourceChange::from_text_edit(file_id, edit),
field_range,
)])),
Diagnostic::new(
DiagnosticCode::Clippy("redundant_field_names"),
"Shorthand struct initialization",
field_range,
)
.with_fixes(Some(vec![fix(
"use_expr_field_shorthand",
"Use struct shorthand initialization",
SourceChange::from_text_edit(file_id, edit),
field_range,
)])),
);
}
}
Expand Down Expand Up @@ -87,14 +90,17 @@ fn check_pat_field_shorthand(

let field_range = record_pat_field.syntax().text_range();
acc.push(
Diagnostic::new("use-field-shorthand", "Shorthand struct pattern", field_range)
.severity(Severity::WeakWarning)
.with_fixes(Some(vec![fix(
"use_pat_field_shorthand",
"Use struct field shorthand",
SourceChange::from_text_edit(file_id, edit),
field_range,
)])),
Diagnostic::new(
DiagnosticCode::Clippy("redundant_field_names"),
"Shorthand struct pattern",
field_range,
)
.with_fixes(Some(vec![fix(
"use_pat_field_shorthand",
"Use struct field shorthand",
SourceChange::from_text_edit(file_id, edit),
field_range,
)])),
);
}
}
Expand Down
7 changes: 3 additions & 4 deletions crates/ide-diagnostics/src/handlers/inactive_code.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cfg::DnfExpr;
use stdx::format_to;

use crate::{Diagnostic, DiagnosticsContext, Severity};
use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext, Severity};

// Diagnostic: inactive-code
//
Expand All @@ -27,13 +27,12 @@ pub(crate) fn inactive_code(
format_to!(message, ": {}", inactive);
}
}

// FIXME: This shouldn't be a diagnostic
let res = Diagnostic::new(
"inactive-code",
DiagnosticCode::Ra("inactive-code", Severity::WeakWarning),
message,
ctx.sema.diagnostics_display_range(d.node.clone()).range,
)
.severity(Severity::WeakWarning)
.with_unused(true);
Some(res)
}
Expand Down
10 changes: 5 additions & 5 deletions crates/ide-diagnostics/src/handlers/incoherent_impl.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use hir::InFile;

use crate::{Diagnostic, DiagnosticsContext, Severity};
use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};

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

#[cfg(test)]
Expand Down

0 comments on commit 45272ef

Please sign in to comment.