1
Fork 0

Add deprecated_safe lint

It warns about usages of `std::env::{set_var, remove_var}` with an
automatic fix wrapping the call in an `unsafe` block.
This commit is contained in:
Tobias Bucher 2024-05-28 15:16:25 +02:00
parent d7680e3556
commit 44f9f8bc33
10 changed files with 172 additions and 11 deletions

View file

@ -9,7 +9,7 @@ use rustc_middle::thir::visit::Visitor;
use rustc_middle::thir::*;
use rustc_middle::ty::print::with_no_trimmed_paths;
use rustc_middle::ty::{self, ParamEnv, Ty, TyCtxt};
use rustc_session::lint::builtin::{UNSAFE_OP_IN_UNSAFE_FN, UNUSED_UNSAFE};
use rustc_session::lint::builtin::{DEPRECATED_SAFE, UNSAFE_OP_IN_UNSAFE_FN, UNUSED_UNSAFE};
use rustc_session::lint::Level;
use rustc_span::def_id::{DefId, LocalDefId};
use rustc_span::symbol::Symbol;
@ -115,7 +115,22 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
// caller is from an edition before 2024.
UnsafeOpKind::CallToUnsafeFunction(Some(id))
if !span.at_least_rust_2024()
&& self.tcx.has_attr(id, sym::rustc_deprecated_safe_2024) => {}
&& self.tcx.has_attr(id, sym::rustc_deprecated_safe_2024) =>
{
self.tcx.emit_node_span_lint(
DEPRECATED_SAFE,
self.hir_context,
span,
CallToDeprecatedSafeFnRequiresUnsafe {
span,
function: with_no_trimmed_paths!(self.tcx.def_path_str(id)),
sub: CallToDeprecatedSafeFnRequiresUnsafeSub {
left: span.shrink_to_lo(),
right: span.shrink_to_hi(),
},
},
)
}
_ => kind.emit_requires_unsafe_err(
self.tcx,
span,

View file

@ -20,6 +20,25 @@ pub struct UnconditionalRecursion {
pub call_sites: Vec<Span>,
}
#[derive(LintDiagnostic)]
#[diag(mir_build_call_to_deprecated_safe_fn_requires_unsafe)]
pub struct CallToDeprecatedSafeFnRequiresUnsafe {
#[label]
pub span: Span,
pub function: String,
#[subdiagnostic]
pub sub: CallToDeprecatedSafeFnRequiresUnsafeSub,
}
#[derive(Subdiagnostic)]
#[multipart_suggestion(mir_build_suggestion, applicability = "machine-applicable")]
pub struct CallToDeprecatedSafeFnRequiresUnsafeSub {
#[suggestion_part(code = "unsafe {{ ")]
pub left: Span,
#[suggestion_part(code = " }}")]
pub right: Span,
}
#[derive(LintDiagnostic)]
#[diag(mir_build_unsafe_op_in_unsafe_fn_call_to_unsafe_fn_requires_unsafe, code = E0133)]
#[note]