]> git.lizzy.rs Git - rust.git/commitdiff
Cleanup as discussed in PR
authorFlorian Gilcher <florian.gilcher@asquera.de>
Sat, 17 Oct 2015 18:16:54 +0000 (20:16 +0200)
committerFlorian Gilcher <florian.gilcher@asquera.de>
Sat, 17 Oct 2015 18:16:54 +0000 (20:16 +0200)
src/lib.rs
src/needless_features.rs
src/utils.rs
tests/compile-fail/needless_features.rs

index 3175b92be4e0af230c35e11e7f715c4b05a7199d..a3657460fe9924d8c3d0ac13635a4a62133ffcde 100755 (executable)
@@ -152,8 +152,8 @@ pub fn plugin_registrar(reg: &mut Registry) {
         mut_reference::UNNECESSARY_MUT_PASSED,
         mutex_atomic::MUTEX_ATOMIC,
         needless_bool::NEEDLESS_BOOL,
-        needless_features::AS_MUT_SLICE,
-        needless_features::AS_SLICE,
+        needless_features::UNSTABLE_AS_MUT_SLICE,
+        needless_features::UNSTABLE_AS_SLICE,
         open_options::NONSENSICAL_OPEN_OPTIONS,
         precedence::PRECEDENCE,
         ptr_arg::PTR_ARG,
index 950057da26c3763c10b5b0db539b83cefdf9d736..b1d38df73113cc671dd07f28bb88177d63ca7dfb 100644 (file)
@@ -6,44 +6,48 @@
 use rustc_front::hir::*;
 
 use utils::{span_lint};
+use utils;
 
 declare_lint! {
-    pub AS_SLICE,
+    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 AS_MUT_SLICE,
+    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!(AS_SLICE,AS_MUT_SLICE)
+        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" {
-                span_lint(cx, AS_SLICE, expr.span,
+            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" {
-                span_lint(cx, AS_MUT_SLICE, expr.span,
+            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");
             }
         }
     }
-}
\ No newline at end of file
+}
+
+fn check_paths(cx: &LateContext, expr: &Expr) -> bool {
+    utils::match_impl_method(cx, expr, &["collections", "vec", "Vec<T>"])
+}
index 1ba6029ebe6339eb6f42ed15df1a76d142be653e..c3ebfd8147d9cddd50249d354200afbfb64ed148 100644 (file)
@@ -118,6 +118,19 @@ pub fn match_type(cx: &LateContext, ty: ty::Ty, path: &[&str]) -> bool {
     }
 }
 
+/// check if method call given in "expr" belongs to given trait
+pub fn match_impl_method(cx: &LateContext, expr: &Expr, path: &[&str]) -> bool {
+    let method_call = ty::MethodCall::expr(expr.id);
+
+    let trt_id = cx.tcx.tables
+                       .borrow().method_map.get(&method_call)
+                       .and_then(|callee| cx.tcx.impl_of_method(callee.def_id));
+    if let Some(trt_id) = trt_id {
+        match_def_path(cx, trt_id, path)
+    } else {
+        false
+    }
+}
 /// check if method call given in "expr" belongs to given trait
 pub fn match_trait_method(cx: &LateContext, expr: &Expr, path: &[&str]) -> bool {
     let method_call = ty::MethodCall::expr(expr.id);
index 02639aea66f095311a2c373b60b931f432a83494..dee2a19d5d0f32263baa942080523b50621d0ddc 100644 (file)
@@ -12,6 +12,18 @@ fn test_as_slice() {
     v2.as_mut_slice(); //~ERROR used as_mut_slice() from the 'convert' nightly feature. Use &mut [..]
 }
 
+struct ShouldWork;
+
+impl ShouldWork {
+    fn as_slice(&self) -> &ShouldWork { self }
+}
+
+fn test_should_work() {
+    let sw = ShouldWork;
+    sw.as_slice();
+}
+
 fn main() {
     test_as_slice();
+    test_should_work();
 }