]> git.lizzy.rs Git - rust.git/commitdiff
New lint for needless use of nightly features
authorFlorian Gilcher <florian.gilcher@asquera.de>
Thu, 15 Oct 2015 09:13:01 +0000 (11:13 +0200)
committerFlorian Gilcher <florian.gilcher@asquera.de>
Thu, 15 Oct 2015 19:33:47 +0000 (21:33 +0200)
src/lib.rs
src/needless_features.rs [new file with mode: 0644]
tests/compile-fail/needless_features.rs [new file with mode: 0644]

index 8bf199bff4792442a82bba44d48306213776f041..31ce6738fd19e562bff24f5952eece820014d40d 100755 (executable)
@@ -50,6 +50,7 @@
 pub mod mutex_atomic;
 pub mod zero_div_zero;
 pub mod open_options;
+pub mod needless_features;
 
 mod reexport {
     pub use syntax::ast::{Name, Ident, NodeId};
@@ -94,6 +95,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
     reg.register_late_lint_pass(box open_options::NonSensicalOpenOptions);
     reg.register_late_lint_pass(box zero_div_zero::ZeroDivZeroPass);
     reg.register_late_lint_pass(box mutex_atomic::MutexAtomic);
+    reg.register_late_lint_pass(box needless_features::NeedlessFeaturesPass);
 
     reg.register_lint_group("clippy_pedantic", vec![
         methods::OPTION_UNWRAP_USED,
@@ -150,6 +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_SLICE,
+        needless_features::AS_MUT_SLICE,
         open_options::NONSENSICAL_OPEN_OPTIONS,
         precedence::PRECEDENCE,
         ptr_arg::PTR_ARG,
diff --git a/src/needless_features.rs b/src/needless_features.rs
new file mode 100644 (file)
index 0000000..950057d
--- /dev/null
@@ -0,0 +1,49 @@
+//! 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};
+
+declare_lint! {
+    pub 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,
+    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)
+    }
+}
+
+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,
+                          "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,
+                          "used as_mut_slice() from the 'convert' nightly feature. Use &mut [..] \
+                           instead");
+            }
+        }
+    }
+}
\ No newline at end of file
diff --git a/tests/compile-fail/needless_features.rs b/tests/compile-fail/needless_features.rs
new file mode 100644 (file)
index 0000000..02639ae
--- /dev/null
@@ -0,0 +1,17 @@
+#![feature(plugin)]
+#![feature(convert)]
+#![plugin(clippy)]
+
+#![deny(clippy)]
+
+fn test_as_slice() {
+    let v = vec![1];
+    v.as_slice(); //~ERROR used as_slice() from the 'convert' nightly feature. Use &[..]
+
+    let mut v2 = vec![1];
+    v2.as_mut_slice(); //~ERROR used as_mut_slice() from the 'convert' nightly feature. Use &mut [..]
+}
+
+fn main() {
+    test_as_slice();
+}