]> git.lizzy.rs Git - rust.git/blob - src/unsafe_removed_from_name.rs
Rustup to *1.10.0-nightly (9c6904ca1 2016-05-18)*
[rust.git] / src / unsafe_removed_from_name.rs
1 use rustc::hir::*;
2 use rustc::lint::*;
3 use syntax::ast::Name;
4 use syntax::codemap::Span;
5 use syntax::parse::token::InternedString;
6 use utils::span_lint;
7
8 /// **What it does:** This lint checks for imports that remove "unsafe" from an item's name
9 ///
10 /// **Why is this bad?** Renaming makes it less clear which traits and structures are unsafe.
11 ///
12 /// **Known problems:** None.
13 ///
14 /// **Example:**
15 /// ```rust,ignore
16 /// use std::cell::{UnsafeCell as TotallySafeCell};
17 ///
18 /// extern crate crossbeam;
19 /// use crossbeam::{spawn_unsafe as spawn};
20 /// ```
21 declare_lint! {
22     pub UNSAFE_REMOVED_FROM_NAME,
23     Warn,
24     "unsafe removed from name"
25 }
26
27 pub struct UnsafeNameRemoval;
28
29 impl LintPass for UnsafeNameRemoval {
30     fn get_lints(&self) -> LintArray {
31         lint_array!(UNSAFE_REMOVED_FROM_NAME)
32     }
33 }
34
35 impl LateLintPass for UnsafeNameRemoval {
36     fn check_item(&mut self, cx: &LateContext, item: &Item) {
37         if let ItemUse(ref item_use) = item.node {
38             match item_use.node {
39                 ViewPath_::ViewPathSimple(ref name, ref path) => {
40                     unsafe_to_safe_check(
41                         path.segments
42                             .last()
43                             .expect("use paths cannot be empty")
44                             .name,
45                         *name,
46                         cx, &item.span
47                         );
48                 },
49                 ViewPath_::ViewPathList(_, ref path_list_items) => {
50                     for path_list_item in path_list_items.iter() {
51                         let plid = path_list_item.node;
52                         if let (Some(name), Some(rename)) = (plid.name(), plid.rename()) {
53                             unsafe_to_safe_check(name, rename, cx, &item.span);
54                         };
55                     }
56                 },
57                 ViewPath_::ViewPathGlob(_) => {}
58             }
59         }
60     }
61 }
62
63 fn unsafe_to_safe_check(old_name: Name, new_name: Name, cx: &LateContext, span: &Span) {
64     let old_str = old_name.as_str();
65     let new_str = new_name.as_str();
66     if contains_unsafe(&old_str) && !contains_unsafe(&new_str) {
67         span_lint(
68             cx,
69             UNSAFE_REMOVED_FROM_NAME,
70             *span,
71             &format!(
72                 "removed \"unsafe\" from the name of `{}` in use as `{}`",
73                 old_str,
74                 new_str
75             ));
76     }
77 }
78
79 fn contains_unsafe(name: &InternedString) -> bool {
80     name.contains("Unsafe") || name.contains("unsafe")
81 }