3 use syntax::codemap::Span;
4 use syntax::symbol::InternedString;
7 /// **What it does:** Checks for imports that remove "unsafe" from an item's
10 /// **Why is this bad?** Renaming makes it less clear which traits and
11 /// structures are unsafe.
13 /// **Known problems:** None.
17 /// use std::cell::{UnsafeCell as TotallySafeCell};
19 /// extern crate crossbeam;
20 /// use crossbeam::{spawn_unsafe as spawn};
22 declare_clippy_lint! {
23 pub UNSAFE_REMOVED_FROM_NAME,
25 "`unsafe` removed from API names on import"
28 pub struct UnsafeNameRemoval;
30 impl LintPass for UnsafeNameRemoval {
31 fn get_lints(&self) -> LintArray {
32 lint_array!(UNSAFE_REMOVED_FROM_NAME)
36 impl EarlyLintPass for UnsafeNameRemoval {
37 fn check_item(&mut self, cx: &EarlyContext, item: &Item) {
38 if let ItemKind::Use(ref use_tree) = item.node {
39 check_use_tree(use_tree, cx, &item.span);
44 fn check_use_tree(use_tree: &UseTree, cx: &EarlyContext, span: &Span) {
46 UseTreeKind::Simple(Some(new_name)) => {
47 let old_name = use_tree
51 .expect("use paths cannot be empty")
53 unsafe_to_safe_check(old_name, new_name, cx, span);
55 UseTreeKind::Simple(None) |
56 UseTreeKind::Glob => {},
57 UseTreeKind::Nested(ref nested_use_tree) => {
58 for &(ref use_tree, _) in nested_use_tree {
59 check_use_tree(use_tree, cx, span);
65 fn unsafe_to_safe_check(old_name: Ident, new_name: Ident, cx: &EarlyContext, span: &Span) {
66 let old_str = old_name.name.as_str();
67 let new_str = new_name.name.as_str();
68 if contains_unsafe(&old_str) && !contains_unsafe(&new_str) {
71 UNSAFE_REMOVED_FROM_NAME,
73 &format!("removed \"unsafe\" from the name of `{}` in use as `{}`", old_str, new_str),
78 fn contains_unsafe(name: &InternedString) -> bool {
79 name.contains("Unsafe") || name.contains("unsafe")