1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//! Checks for usage of nightly features that have simple stable equivalents
//!
//! This lint is **warn** by default

use rustc::lint::*;
use rustc_front::hir::*;

use utils::{span_lint};
use utils;

declare_lint! {
    pub UNSTABLE_AS_SLICE,
    Warn,
    "as_slice is not stable and can be replaced by & v[..]\
see https://github.com/rust-lang/rust/issues/27729"
}

declare_lint! {
    pub UNSTABLE_AS_MUT_SLICE,
    Warn,
    "as_mut_slice is not stable and can be replaced by &mut v[..]\
see https://github.com/rust-lang/rust/issues/27729"
}

#[derive(Copy,Clone)]
pub struct NeedlessFeaturesPass;

impl LintPass for NeedlessFeaturesPass {
    fn get_lints(&self) -> LintArray {
        lint_array!(UNSTABLE_AS_SLICE,UNSTABLE_AS_MUT_SLICE)
    }
}

impl LateLintPass for NeedlessFeaturesPass {
    fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
        if let ExprMethodCall(ref name, _, _) = expr.node {
            if name.node.as_str() == "as_slice" && check_paths(cx, expr) {
                span_lint(cx, UNSTABLE_AS_SLICE, expr.span,
                          "used as_slice() from the 'convert' nightly feature. Use &[..] \
                           instead");
            }
            if name.node.as_str() == "as_mut_slice" && check_paths(cx, expr) {
                span_lint(cx, UNSTABLE_AS_MUT_SLICE, expr.span,
                          "used as_mut_slice() from the 'convert' nightly feature. Use &mut [..] \
                           instead");
            }
        }
    }
}

fn check_paths(cx: &LateContext, expr: &Expr) -> bool {
    utils::match_impl_method(cx, expr, &["collections", "vec", "Vec<T>"])
}