]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/unsafe_removed_from_name.rs
Merge pull request #1373 from Manishearth/rustup
[rust.git] / clippy_lints / src / unsafe_removed_from_name.rs
1 use rustc::lint::*;
2 use syntax::ast::*;
3 use syntax::codemap::Span;
4 use syntax::symbol::InternedString;
5 use utils::span_lint;
6
7 /// **What it does:** Checks for imports that remove "unsafe" from an item's
8 /// name.
9 ///
10 /// **Why is this bad?** Renaming makes it less clear which traits and
11 /// structures are unsafe.
12 ///
13 /// **Known problems:** None.
14 ///
15 /// **Example:**
16 /// ```rust,ignore
17 /// use std::cell::{UnsafeCell as TotallySafeCell};
18 ///
19 /// extern crate crossbeam;
20 /// use crossbeam::{spawn_unsafe as spawn};
21 /// ```
22 declare_lint! {
23     pub UNSAFE_REMOVED_FROM_NAME,
24     Warn,
25     "`unsafe` removed from API names on import"
26 }
27
28 pub struct UnsafeNameRemoval;
29
30 impl LintPass for UnsafeNameRemoval {
31     fn get_lints(&self) -> LintArray {
32         lint_array!(UNSAFE_REMOVED_FROM_NAME)
33     }
34 }
35
36 impl EarlyLintPass for UnsafeNameRemoval {
37     fn check_item(&mut self, cx: &EarlyContext, item: &Item) {
38         if let ItemKind::Use(ref item_use) = item.node {
39             match item_use.node {
40                 ViewPath_::ViewPathSimple(ref name, ref path) => {
41                     unsafe_to_safe_check(
42                         path.segments
43                             .last()
44                             .expect("use paths cannot be empty")
45                             .identifier,
46                         *name,
47                         cx, &item.span
48                         );
49                 }
50                 ViewPath_::ViewPathList(_, ref path_list_items) => {
51                     for path_list_item in path_list_items.iter() {
52                         let plid = path_list_item.node;
53                         if let Some(rename) = plid.rename {
54                             unsafe_to_safe_check(plid.name, rename, cx, &item.span);
55                         };
56                     }
57                 }
58                 ViewPath_::ViewPathGlob(_) => {}
59             }
60         }
61     }
62 }
63
64 fn unsafe_to_safe_check(old_name: Ident, new_name: Ident, cx: &EarlyContext, span: &Span) {
65     let old_str = old_name.name.as_str();
66     let new_str = new_name.name.as_str();
67     if contains_unsafe(&old_str) && !contains_unsafe(&new_str) {
68         span_lint(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 }