1 use crate::utils::span_lint;
2 use rustc::declare_lint_pass;
3 use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
4 use rustc_session::declare_tool_lint;
5 use rustc_span::source_map::Span;
6 use rustc_span::symbol::SymbolStr;
10 /// **What it does:** Checks for imports that remove "unsafe" from an item's
13 /// **Why is this bad?** Renaming makes it less clear which traits and
14 /// structures are unsafe.
16 /// **Known problems:** None.
20 /// use std::cell::{UnsafeCell as TotallySafeCell};
22 /// extern crate crossbeam;
23 /// use crossbeam::{spawn_unsafe as spawn};
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: &SymbolStr) -> bool {
78 name.contains("Unsafe") || name.contains("unsafe")