Skip to content

Commit 1cae336

Browse files
committed
Add needless borrowed ref lint (WIP).
1 parent 06ec3d3 commit 1cae336

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ All notable changes to this project will be documented in this file.
471471
[`mutex_integer`]: https://github.com/Manishearth/rust-clippy/wiki#mutex_integer
472472
[`needless_bool`]: https://github.com/Manishearth/rust-clippy/wiki#needless_bool
473473
[`needless_borrow`]: https://github.com/Manishearth/rust-clippy/wiki#needless_borrow
474+
[`needless_borrowed_reference`]: https://github.com/Manishearth/rust-clippy/wiki#needless_borrowed_reference
474475
[`needless_continue`]: https://github.com/Manishearth/rust-clippy/wiki#needless_continue
475476
[`needless_lifetimes`]: https://github.com/Manishearth/rust-clippy/wiki#needless_lifetimes
476477
[`needless_pass_by_value`]: https://github.com/Manishearth/rust-clippy/wiki#needless_pass_by_value

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ transparently:
180180

181181
## Lints
182182

183-
There are 199 lints included in this crate:
183+
There are 200 lints included in this crate:
184184

185185
name | default | triggers on
186186
-----------------------------------------------------------------------------------------------------------------------|---------|----------------------------------------------------------------------------------------------------------------------------------
@@ -290,6 +290,7 @@ name
290290
[mutex_integer](https://github.com/Manishearth/rust-clippy/wiki#mutex_integer) | allow | using a mutex for an integer type
291291
[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 }`
292292
[needless_borrow](https://github.com/Manishearth/rust-clippy/wiki#needless_borrow) | warn | taking a reference that is going to be automatically dereferenced
293+
[needless_borrowed_reference](https://github.com/Manishearth/rust-clippy/wiki#needless_borrowed_reference) | warn | taking a needless borrowed reference
293294
[needless_continue](https://github.com/Manishearth/rust-clippy/wiki#needless_continue) | warn | `continue` statements that can be replaced by a rearrangement of code
294295
[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
295296
[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

clippy_lints/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ pub mod mut_reference;
115115
pub mod mutex_atomic;
116116
pub mod needless_bool;
117117
pub mod needless_borrow;
118+
pub mod needless_borrowed_ref;
118119
pub mod needless_continue;
119120
pub mod needless_pass_by_value;
120121
pub mod needless_update;
@@ -475,6 +476,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
475476
needless_bool::BOOL_COMPARISON,
476477
needless_bool::NEEDLESS_BOOL,
477478
needless_borrow::NEEDLESS_BORROW,
479+
needless_borrowed_ref::NEEDLESS_BORROWED_REFERENCE,
478480
needless_continue::NEEDLESS_CONTINUE,
479481
needless_pass_by_value::NEEDLESS_PASS_BY_VALUE,
480482
needless_update::NEEDLESS_UPDATE,
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//! Checks for useless borrowed references in clojures.
2+
//!
3+
//! This lint is **warn** by default
4+
5+
use rustc::lint::*;
6+
use rustc::hir::{MutImmutable, Pat, PatKind, BindingMode};
7+
use rustc::ty;
8+
use utils::{span_lint, in_macro};
9+
10+
/// **What it does:** Checks for useless borrowed references in clojures.
11+
///
12+
/// **Why is this bad?** TODO
13+
///
14+
/// **Known problems:** None.
15+
///
16+
/// **Example:**
17+
/// ```rust
18+
/// let mut v = Vec::<String>::new();
19+
/// let _ = v.iter_mut().filter(|&ref a| a.is_empty());
20+
/// ```
21+
/// It could just be |a| a.is_empty()
22+
declare_lint! {
23+
pub NEEDLESS_BORROWED_REFERENCE,
24+
Warn,
25+
"taking a needless borrowed reference"
26+
}
27+
28+
#[derive(Copy, Clone)]
29+
pub struct NeedlessBorrowedRef;
30+
31+
impl LintPass for NeedlessBorrowedRef {
32+
fn get_lints(&self) -> LintArray {
33+
lint_array!(NEEDLESS_BORROWED_REFERENCE)
34+
}
35+
}
36+
37+
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBorrowedRef {
38+
fn check_pat(&mut self, cx: &LateContext<'a, 'tcx>, pat: &'tcx Pat) {
39+
if in_macro(pat.span) {
40+
// OK, simple enough, lints doesn't check in macro.
41+
return;
42+
}
43+
44+
if_let_chain! {[
45+
// Pat is a pattern whose node
46+
// is a binding which "involves" a immutable reference...
47+
let PatKind::Binding(BindingMode::BindByRef(MutImmutable), ..) = pat.node,
48+
// Pattern's type is a reference. Get the type and mutability of referenced value (tam: TypeAndMut).
49+
let ty::TyRef(_, ref tam) = cx.tables.pat_ty(pat).sty,
50+
// This is an immutable reference.
51+
tam.mutbl == MutImmutable,
52+
], {
53+
span_lint(cx, NEEDLESS_BORROWED_REFERENCE, pat.span, "this pattern takes a needless borrowed reference")
54+
}}
55+
}
56+
}
57+

0 commit comments

Comments
 (0)