]> git.lizzy.rs Git - rust.git/blob - src/needless_features.rs
Remove * dep
[rust.git] / src / needless_features.rs
1 //! Checks for usage of nightly features that have simple stable equivalents
2 //!
3 //! This lint is **warn** by default
4
5 use rustc::lint::*;
6 use rustc_front::hir::*;
7
8 use utils::span_lint;
9 use utils;
10
11 /// **What it does:** This lint `Warn`s on use of the `as_slice(..)` function, which is unstable.
12 ///
13 /// **Why is this bad?** Using this function doesn't make your code better, but it will preclude it from building with stable Rust.
14 ///
15 /// **Known problems:** None.
16 ///
17 /// **Example:** `x.as_slice(..)`
18 declare_lint! {
19     pub UNSTABLE_AS_SLICE,
20     Warn,
21     "as_slice is not stable and can be replaced by & v[..]\
22 see https://github.com/rust-lang/rust/issues/27729"
23 }
24
25 /// **What it does:** This lint `Warn`s on use of the `as_mut_slice(..)` function, which is unstable.
26 ///
27 /// **Why is this bad?** Using this function doesn't make your code better, but it will preclude it from building with stable Rust.
28 ///
29 /// **Known problems:** None.
30 ///
31 /// **Example:** `x.as_mut_slice(..)`
32 declare_lint! {
33     pub UNSTABLE_AS_MUT_SLICE,
34     Warn,
35     "as_mut_slice is not stable and can be replaced by &mut v[..]\
36 see https://github.com/rust-lang/rust/issues/27729"
37 }
38
39 #[derive(Copy,Clone)]
40 pub struct NeedlessFeaturesPass;
41
42 impl LintPass for NeedlessFeaturesPass {
43     fn get_lints(&self) -> LintArray {
44         lint_array!(UNSTABLE_AS_SLICE, UNSTABLE_AS_MUT_SLICE)
45     }
46 }
47
48 impl LateLintPass for NeedlessFeaturesPass {
49     fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
50         if let ExprMethodCall(ref name, _, _) = expr.node {
51             if name.node.as_str() == "as_slice" && check_paths(cx, expr) {
52                 span_lint(cx, UNSTABLE_AS_SLICE, expr.span,
53                           "used as_slice() from the 'convert' nightly feature. Use &[..] \
54                            instead");
55             }
56             if name.node.as_str() == "as_mut_slice" && check_paths(cx, expr) {
57                 span_lint(cx, UNSTABLE_AS_MUT_SLICE, expr.span,
58                           "used as_mut_slice() from the 'convert' nightly feature. Use &mut [..] \
59                            instead");
60             }
61         }
62     }
63 }
64
65 fn check_paths(cx: &LateContext, expr: &Expr) -> bool {
66     utils::match_impl_method(cx, expr, &["collections", "vec", "Vec<T>"])
67 }