mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 05:45:12 +00:00
Add diagnostics for unsafe_op_in_unsafe_fn
Turns out it's pretty easy, but I did have to add support for allowed-by-default lints.
This commit is contained in:
parent
94b526fc86
commit
f6eb5be591
5 changed files with 55 additions and 10 deletions
|
@ -11,9 +11,14 @@ use crate::{fix, Diagnostic, DiagnosticCode, DiagnosticsContext};
|
|||
//
|
||||
// This diagnostic is triggered if an operation marked as `unsafe` is used outside of an `unsafe` function or block.
|
||||
pub(crate) fn missing_unsafe(ctx: &DiagnosticsContext<'_>, d: &hir::MissingUnsafe) -> Diagnostic {
|
||||
let code = if d.only_lint {
|
||||
DiagnosticCode::RustcLint("unsafe_op_in_unsafe_fn")
|
||||
} else {
|
||||
DiagnosticCode::RustcHardError("E0133")
|
||||
};
|
||||
Diagnostic::new_with_syntax_node_ptr(
|
||||
ctx,
|
||||
DiagnosticCode::RustcHardError("E0133"),
|
||||
code,
|
||||
"this operation is unsafe and requires an unsafe function or block",
|
||||
d.expr.map(|it| it.into()),
|
||||
)
|
||||
|
@ -562,6 +567,30 @@ fn main() {
|
|||
ed2021::safe();
|
||||
ed2024::not_safe();
|
||||
//^^^^^^^^^^^^^^^^^^💡 error: this operation is unsafe and requires an unsafe function or block
|
||||
}
|
||||
"#,
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unsafe_op_in_unsafe_fn_allowed_by_default() {
|
||||
check_diagnostics(
|
||||
r#"
|
||||
unsafe fn foo(p: *mut i32) {
|
||||
*p = 123;
|
||||
}
|
||||
"#,
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unsafe_op_in_unsafe_fn() {
|
||||
check_diagnostics(
|
||||
r#"
|
||||
#![warn(unsafe_op_in_unsafe_fn)]
|
||||
unsafe fn foo(p: *mut i32) {
|
||||
*p = 123;
|
||||
//^^💡 warn: this operation is unsafe and requires an unsafe function or block
|
||||
}
|
||||
"#,
|
||||
)
|
||||
|
|
|
@ -491,7 +491,7 @@ pub fn semantic_diagnostics(
|
|||
|
||||
// The edition isn't accurate (each diagnostics may have its own edition due to macros),
|
||||
// but it's okay as it's only being used for error recovery.
|
||||
handle_lint_attributes(
|
||||
handle_lints(
|
||||
&ctx.sema,
|
||||
&mut FxHashMap::default(),
|
||||
&mut lints,
|
||||
|
@ -551,7 +551,12 @@ fn build_group_dict(
|
|||
map_with_prefixes.into_iter().map(|(k, v)| (k.strip_prefix(prefix).unwrap(), v)).collect()
|
||||
}
|
||||
|
||||
fn handle_lint_attributes(
|
||||
/// Thd default severity for lints that are not warn by default.
|
||||
// FIXME: Autogenerate this instead of write manually.
|
||||
static LINTS_DEFAULT_SEVERITY: LazyLock<FxHashMap<&str, Severity>> =
|
||||
LazyLock::new(|| FxHashMap::from_iter([("unsafe_op_in_unsafe_fn", Severity::Allow)]));
|
||||
|
||||
fn handle_lints(
|
||||
sema: &Semantics<'_, RootDatabase>,
|
||||
cache: &mut FxHashMap<HirFileId, FxHashMap<SmolStr, SeverityAttr>>,
|
||||
diagnostics: &mut [(InFile<SyntaxNode>, &mut Diagnostic)],
|
||||
|
@ -559,6 +564,14 @@ fn handle_lint_attributes(
|
|||
edition: Edition,
|
||||
) {
|
||||
for (node, diag) in diagnostics {
|
||||
let lint = match diag.code {
|
||||
DiagnosticCode::RustcLint(lint) | DiagnosticCode::Clippy(lint) => lint,
|
||||
_ => panic!("non-lint passed to `handle_lints()`"),
|
||||
};
|
||||
if let Some(&default_severity) = LINTS_DEFAULT_SEVERITY.get(lint) {
|
||||
diag.severity = default_severity;
|
||||
}
|
||||
|
||||
let mut diag_severity = fill_lint_attrs(sema, node, cache, cache_stack, diag, edition);
|
||||
|
||||
if let outline_diag_severity @ Some(_) =
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue