]> git.lizzy.rs Git - rust.git/commitdiff
Add needless borrowed ref lint (WIP).
authorBenoît CORTIER <benoit.cortier@fried-world.eu>
Sat, 24 Jun 2017 10:04:56 +0000 (12:04 +0200)
committerBenoît CORTIER <benoit.cortier@fried-world.eu>
Sat, 24 Jun 2017 10:04:56 +0000 (12:04 +0200)
CHANGELOG.md
README.md
clippy_lints/src/lib.rs
clippy_lints/src/needless_borrowed_ref.rs [new file with mode: 0644]

index 5c11be44c08577ff03b0dd238f0c7522d294ff0c..bd2da3f361b30d8c2c7d14cc4692d41ebb121827 100644 (file)
@@ -471,6 +471,7 @@ All notable changes to this project will be documented in this file.
 [`mutex_integer`]: https://github.com/Manishearth/rust-clippy/wiki#mutex_integer
 [`needless_bool`]: https://github.com/Manishearth/rust-clippy/wiki#needless_bool
 [`needless_borrow`]: https://github.com/Manishearth/rust-clippy/wiki#needless_borrow
+[`needless_borrowed_reference`]: https://github.com/Manishearth/rust-clippy/wiki#needless_borrowed_reference
 [`needless_continue`]: https://github.com/Manishearth/rust-clippy/wiki#needless_continue
 [`needless_lifetimes`]: https://github.com/Manishearth/rust-clippy/wiki#needless_lifetimes
 [`needless_pass_by_value`]: https://github.com/Manishearth/rust-clippy/wiki#needless_pass_by_value
index 4b79c11aa387abeffb470a8c3a472d4db5e35a91..6f4770ae669f6bb096ea5e91a235272af017fdaa 100644 (file)
--- a/README.md
+++ b/README.md
@@ -180,7 +180,7 @@ transparently:
 
 ## Lints
 
-There are 199 lints included in this crate:
+There are 200 lints included in this crate:
 
 name                                                                                                                   | default | triggers on
 -----------------------------------------------------------------------------------------------------------------------|---------|----------------------------------------------------------------------------------------------------------------------------------
@@ -290,6 +290,7 @@ name
 [mutex_integer](https://github.com/Manishearth/rust-clippy/wiki#mutex_integer)                                         | allow   | using a mutex for an integer type
 [needless_bool](https://github.com/Manishearth/rust-clippy/wiki#needless_bool)                                         | warn    | if-statements with plain booleans in the then- and else-clause, e.g. `if p { true } else { false }`
 [needless_borrow](https://github.com/Manishearth/rust-clippy/wiki#needless_borrow)                                     | warn    | taking a reference that is going to be automatically dereferenced
+[needless_borrowed_reference](https://github.com/Manishearth/rust-clippy/wiki#needless_borrowed_reference)             | warn    | taking a needless borrowed reference
 [needless_continue](https://github.com/Manishearth/rust-clippy/wiki#needless_continue)                                 | warn    | `continue` statements that can be replaced by a rearrangement of code
 [needless_lifetimes](https://github.com/Manishearth/rust-clippy/wiki#needless_lifetimes)                               | warn    | using explicit lifetimes for references in function arguments when elision rules would allow omitting them
 [needless_pass_by_value](https://github.com/Manishearth/rust-clippy/wiki#needless_pass_by_value)                       | warn    | functions taking arguments by value, but not consuming them in its body
index ec73e9d50d9af07c77f02d90860cc08972bf961d..1ef7f3d3513f4c4b224e2241c7df2a85e0970aef 100644 (file)
@@ -115,6 +115,7 @@ macro_rules! declare_restriction_lint {
 pub mod mutex_atomic;
 pub mod needless_bool;
 pub mod needless_borrow;
+pub mod needless_borrowed_ref;
 pub mod needless_continue;
 pub mod needless_pass_by_value;
 pub mod needless_update;
@@ -475,6 +476,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
         needless_bool::BOOL_COMPARISON,
         needless_bool::NEEDLESS_BOOL,
         needless_borrow::NEEDLESS_BORROW,
+        needless_borrowed_ref::NEEDLESS_BORROWED_REFERENCE,
         needless_continue::NEEDLESS_CONTINUE,
         needless_pass_by_value::NEEDLESS_PASS_BY_VALUE,
         needless_update::NEEDLESS_UPDATE,
diff --git a/clippy_lints/src/needless_borrowed_ref.rs b/clippy_lints/src/needless_borrowed_ref.rs
new file mode 100644 (file)
index 0000000..5eadf01
--- /dev/null
@@ -0,0 +1,57 @@
+//! Checks for useless borrowed references in clojures.
+//!
+//! This lint is **warn** by default
+
+use rustc::lint::*;
+use rustc::hir::{MutImmutable, Pat, PatKind, BindingMode};
+use rustc::ty;
+use utils::{span_lint, in_macro};
+
+/// **What it does:** Checks for useless borrowed references in clojures.
+///
+/// **Why is this bad?** TODO
+///
+/// **Known problems:** None.
+///
+/// **Example:**
+/// ```rust
+///     let mut v = Vec::<String>::new();
+///     let _ = v.iter_mut().filter(|&ref a| a.is_empty());
+/// ```
+/// It could just be |a| a.is_empty()
+declare_lint! {
+    pub NEEDLESS_BORROWED_REFERENCE,
+    Warn,
+    "taking a needless borrowed reference"
+}
+
+#[derive(Copy, Clone)]
+pub struct NeedlessBorrowedRef;
+
+impl LintPass for NeedlessBorrowedRef {
+    fn get_lints(&self) -> LintArray {
+        lint_array!(NEEDLESS_BORROWED_REFERENCE)
+    }
+}
+
+impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBorrowedRef {
+    fn check_pat(&mut self, cx: &LateContext<'a, 'tcx>, pat: &'tcx Pat) {
+        if in_macro(pat.span) {
+            // OK, simple enough, lints doesn't check in macro.
+            return;
+        }
+
+        if_let_chain! {[
+            // Pat is a pattern whose node
+            // is a binding which "involves" a immutable reference...
+            let PatKind::Binding(BindingMode::BindByRef(MutImmutable), ..) = pat.node,
+            // Pattern's type is a reference. Get the type and mutability of referenced value (tam: TypeAndMut).
+            let ty::TyRef(_, ref tam) = cx.tables.pat_ty(pat).sty,
+            // This is an immutable reference.
+            tam.mutbl == MutImmutable,
+        ], {
+            span_lint(cx, NEEDLESS_BORROWED_REFERENCE, pat.span, "this pattern takes a needless borrowed reference")
+        }}
+    }
+}
+