]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/unsafe_removed_from_name.rs
68ceaaac8a31432416cb38c6540494c13357221b
[rust.git] / clippy_lints / src / unsafe_removed_from_name.rs
1 use crate::utils::span_lint;
2 use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
3 use rustc::{declare_tool_lint, lint_array};
4 use syntax::ast::*;
5 use syntax::source_map::Span;
6 use syntax::symbol::LocalInternedString;
7
8 declare_clippy_lint! {
9     /// **What it does:** Checks for imports that remove "unsafe" from an item's
10     /// name.
11     ///
12     /// **Why is this bad?** Renaming makes it less clear which traits and
13     /// structures are unsafe.
14     ///
15     /// **Known problems:** None.
16     ///
17     /// **Example:**
18     /// ```rust,ignore
19     /// use std::cell::{UnsafeCell as TotallySafeCell};
20     ///
21     /// extern crate crossbeam;
22     /// use crossbeam::{spawn_unsafe as spawn};
23     /// ```
24     pub UNSAFE_REMOVED_FROM_NAME,
25     style,
26     "`unsafe` removed from API names on import"
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     fn name(&self) -> &'static str {
37         "UnsafeNameRemoval"
38     }
39 }
40
41 impl EarlyLintPass for UnsafeNameRemoval {
42     fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
43         if let ItemKind::Use(ref use_tree) = item.node {
44             check_use_tree(use_tree, cx, item.span);
45         }
46     }
47 }
48
49 fn check_use_tree(use_tree: &UseTree, cx: &EarlyContext<'_>, span: Span) {
50     match use_tree.kind {
51         UseTreeKind::Simple(Some(new_name), ..) => {
52             let old_name = use_tree
53                 .prefix
54                 .segments
55                 .last()
56                 .expect("use paths cannot be empty")
57                 .ident;
58             unsafe_to_safe_check(old_name, new_name, cx, span);
59         },
60         UseTreeKind::Simple(None, ..) | UseTreeKind::Glob => {},
61         UseTreeKind::Nested(ref nested_use_tree) => {
62             for &(ref use_tree, _) in nested_use_tree {
63                 check_use_tree(use_tree, cx, span);
64             }
65         },
66     }
67 }
68
69 fn unsafe_to_safe_check(old_name: Ident, new_name: Ident, cx: &EarlyContext<'_>, span: Span) {
70     let old_str = old_name.name.as_str();
71     let new_str = new_name.name.as_str();
72     if contains_unsafe(&old_str) && !contains_unsafe(&new_str) {
73         span_lint(
74             cx,
75             UNSAFE_REMOVED_FROM_NAME,
76             span,
77             &format!(
78                 "removed \"unsafe\" from the name of `{}` in use as `{}`",
79                 old_str, new_str
80             ),
81         );
82     }
83 }
84
85 fn contains_unsafe(name: &LocalInternedString) -> bool {
86     name.contains("Unsafe") || name.contains("unsafe")
87 }