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