]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/unsafe_removed_from_name.rs
Fix lines that exceed max width manually
[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,
48                         &item.span,
49                     );
50                 },
51                 ViewPath_::ViewPathList(_, ref path_list_items) => 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                 ViewPath_::ViewPathGlob(_) => {},
58             }
59         }
60     }
61 }
62
63 fn unsafe_to_safe_check(old_name: Ident, new_name: Ident, cx: &EarlyContext, span: &Span) {
64     let old_str = old_name.name.as_str();
65     let new_str = new_name.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!("removed \"unsafe\" from the name of `{}` in use as `{}`", old_str, new_str),
72         );
73     }
74 }
75
76 fn contains_unsafe(name: &InternedString) -> bool {
77     name.contains("Unsafe") || name.contains("unsafe")
78 }