2 use rustc::{declare_lint, lint_array};
4 use syntax::source_map::Span;
5 use syntax::symbol::LocalInternedString;
6 use crate::utils::span_lint;
8 /// **What it does:** Checks for imports that remove "unsafe" from an item's
11 /// **Why is this bad?** Renaming makes it less clear which traits and
12 /// structures are unsafe.
14 /// **Known problems:** None.
18 /// use std::cell::{UnsafeCell as TotallySafeCell};
20 /// extern crate crossbeam;
21 /// use crossbeam::{spawn_unsafe as spawn};
23 declare_clippy_lint! {
24 pub UNSAFE_REMOVED_FROM_NAME,
26 "`unsafe` removed from API names on import"
29 pub struct UnsafeNameRemoval;
31 impl LintPass for UnsafeNameRemoval {
32 fn get_lints(&self) -> LintArray {
33 lint_array!(UNSAFE_REMOVED_FROM_NAME)
37 impl EarlyLintPass for UnsafeNameRemoval {
38 fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
39 if let ItemKind::Use(ref use_tree) = item.node {
40 check_use_tree(use_tree, cx, item.span);
45 fn check_use_tree(use_tree: &UseTree, cx: &EarlyContext<'_>, span: Span) {
47 UseTreeKind::Simple(Some(new_name), ..) => {
48 let old_name = use_tree
52 .expect("use paths cannot be empty")
54 unsafe_to_safe_check(old_name, new_name, cx, span);
56 UseTreeKind::Simple(None, ..) |
57 UseTreeKind::Glob => {},
58 UseTreeKind::Nested(ref nested_use_tree) => {
59 for &(ref use_tree, _) in nested_use_tree {
60 check_use_tree(use_tree, cx, span);
66 fn unsafe_to_safe_check(old_name: Ident, new_name: Ident, cx: &EarlyContext<'_>, span: Span) {
67 let old_str = old_name.name.as_str();
68 let new_str = new_name.name.as_str();
69 if contains_unsafe(&old_str) && !contains_unsafe(&new_str) {
72 UNSAFE_REMOVED_FROM_NAME,
74 &format!("removed \"unsafe\" from the name of `{}` in use as `{}`", old_str, new_str),
79 fn contains_unsafe(name: &LocalInternedString) -> bool {
80 name.contains("Unsafe") || name.contains("unsafe")