]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/unsafe_removed_from_name.rs
Dogfood new trivially_copy_pass_by_ref lint
[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::LocalInternedString;
5 use crate::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_clippy_lint! {
23     pub UNSAFE_REMOVED_FROM_NAME,
24     style,
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 use_tree) = item.node {
39             check_use_tree(use_tree, cx, item.span);
40         }
41     }
42 }
43
44 fn check_use_tree(use_tree: &UseTree, cx: &EarlyContext, span: Span) {
45     match use_tree.kind {
46         UseTreeKind::Simple(Some(new_name)) => {
47             let old_name = use_tree
48                 .prefix
49                 .segments
50                 .last()
51                 .expect("use paths cannot be empty")
52                 .ident;
53             unsafe_to_safe_check(old_name, new_name, cx, span);
54         }
55         UseTreeKind::Simple(None) |
56         UseTreeKind::Glob => {},
57         UseTreeKind::Nested(ref nested_use_tree) => {
58             for &(ref use_tree, _) in nested_use_tree {
59                 check_use_tree(use_tree, cx, span);
60             }
61         }
62     }
63 }
64
65 fn unsafe_to_safe_check(old_name: Ident, new_name: Ident, cx: &EarlyContext, span: Span) {
66     let old_str = old_name.name.as_str();
67     let new_str = new_name.name.as_str();
68     if contains_unsafe(&old_str) && !contains_unsafe(&new_str) {
69         span_lint(
70             cx,
71             UNSAFE_REMOVED_FROM_NAME,
72             span,
73             &format!("removed \"unsafe\" from the name of `{}` in use as `{}`", old_str, new_str),
74         );
75     }
76 }
77
78 fn contains_unsafe(name: &LocalInternedString) -> bool {
79     name.contains("Unsafe") || name.contains("unsafe")
80 }