1 use clippy_utils::diagnostics::span_lint;
2 use rustc_ast::ast::{Item, ItemKind, UseTree, UseTreeKind};
3 use rustc_lint::{EarlyContext, EarlyLintPass};
4 use rustc_session::{declare_lint_pass, declare_tool_lint};
5 use rustc_span::source_map::Span;
6 use rustc_span::symbol::Ident;
10 /// Checks for imports that remove "unsafe" from an item's
13 /// ### Why is this bad?
14 /// Renaming makes it less clear which traits and
15 /// structures are unsafe.
19 /// use std::cell::{UnsafeCell as TotallySafeCell};
21 /// extern crate crossbeam;
22 /// use crossbeam::{spawn_unsafe as spawn};
24 #[clippy::version = "pre 1.29.0"]
25 pub UNSAFE_REMOVED_FROM_NAME,
27 "`unsafe` removed from API names on import"
30 declare_lint_pass!(UnsafeNameRemoval => [UNSAFE_REMOVED_FROM_NAME]);
32 impl EarlyLintPass for UnsafeNameRemoval {
33 fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
34 if let ItemKind::Use(ref use_tree) = item.kind {
35 check_use_tree(use_tree, cx, item.span);
40 fn check_use_tree(use_tree: &UseTree, cx: &EarlyContext<'_>, span: Span) {
42 UseTreeKind::Simple(Some(new_name), ..) => {
43 let old_name = use_tree
47 .expect("use paths cannot be empty")
49 unsafe_to_safe_check(old_name, new_name, cx, span);
51 UseTreeKind::Simple(None, ..) | UseTreeKind::Glob => {},
52 UseTreeKind::Nested(ref nested_use_tree) => {
53 for &(ref use_tree, _) in nested_use_tree {
54 check_use_tree(use_tree, cx, span);
60 fn unsafe_to_safe_check(old_name: Ident, new_name: Ident, cx: &EarlyContext<'_>, span: Span) {
61 let old_str = old_name.name.as_str();
62 let new_str = new_name.name.as_str();
63 if contains_unsafe(old_str) && !contains_unsafe(new_str) {
66 UNSAFE_REMOVED_FROM_NAME,
69 "removed `unsafe` from the name of `{}` in use as `{}`",
77 fn contains_unsafe(name: &str) -> bool {
78 name.contains("Unsafe") || name.contains("unsafe")