1 use crate::utils::span_lint;
2 use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
3 use rustc::{declare_tool_lint, lint_array};
5 use syntax::source_map::Span;
6 use syntax::symbol::LocalInternedString;
9 /// **What it does:** Checks for imports that remove "unsafe" from an item's
12 /// **Why is this bad?** Renaming makes it less clear which traits and
13 /// structures are unsafe.
15 /// **Known problems:** None.
19 /// use std::cell::{UnsafeCell as TotallySafeCell};
21 /// extern crate crossbeam;
22 /// use crossbeam::{spawn_unsafe as spawn};
24 pub UNSAFE_REMOVED_FROM_NAME,
26 "`unsafe` removed from API names on import"
29 pub struct UnsafeNameRemoval;
31 impl LintPass for UnsafeNameRemoval {
32 fn get_lints(&self) -> LintArray {
33 lint_array!(UNSAFE_REMOVED_FROM_NAME)
36 fn name(&self) -> &'static str {
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);
49 fn check_use_tree(use_tree: &UseTree, cx: &EarlyContext<'_>, span: Span) {
51 UseTreeKind::Simple(Some(new_name), ..) => {
52 let old_name = use_tree
56 .expect("use paths cannot be empty")
58 unsafe_to_safe_check(old_name, new_name, cx, span);
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);
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) {
75 UNSAFE_REMOVED_FROM_NAME,
78 "removed \"unsafe\" from the name of `{}` in use as `{}`",
85 fn contains_unsafe(name: &LocalInternedString) -> bool {
86 name.contains("Unsafe") || name.contains("unsafe")