Skip to content

Commit 6a2525c

Browse files
authored
Merge pull request #1536 from CBenoit/master
Add lint `&ref x` patterns that could be just `x`
2 parents 687fe1e + e9964e7 commit 6a2525c

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

CHANGELOG.md

+1
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

+2-1
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

+2
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,
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//! Checks for useless borrowed references.
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.
11+
///
12+
/// **Why is this bad?** It is completely useless and make the code look more complex than it actually is.
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+
/// This clojure takes a reference on something that has been matched as a reference and de-referenced.
22+
/// As such, it could just be |a| a.is_empty()
23+
declare_lint! {
24+
pub NEEDLESS_BORROWED_REFERENCE,
25+
Warn,
26+
"taking a needless borrowed reference"
27+
}
28+
29+
#[derive(Copy, Clone)]
30+
pub struct NeedlessBorrowedRef;
31+
32+
impl LintPass for NeedlessBorrowedRef {
33+
fn get_lints(&self) -> LintArray {
34+
lint_array!(NEEDLESS_BORROWED_REFERENCE)
35+
}
36+
}
37+
38+
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBorrowedRef {
39+
fn check_pat(&mut self, cx: &LateContext<'a, 'tcx>, pat: &'tcx Pat) {
40+
if in_macro(pat.span) {
41+
// OK, simple enough, lints doesn't check in macro.
42+
return;
43+
}
44+
45+
if_let_chain! {[
46+
// Pat is a pattern whose node
47+
// is a binding which "involves" a immutable reference...
48+
let PatKind::Binding(BindingMode::BindByRef(MutImmutable), ..) = pat.node,
49+
// Pattern's type is a reference. Get the type and mutability of referenced value (tam: TypeAndMut).
50+
let ty::TyRef(_, ref tam) = cx.tables.pat_ty(pat).sty,
51+
// This is an immutable reference.
52+
tam.mutbl == MutImmutable,
53+
], {
54+
span_lint(cx, NEEDLESS_BORROWED_REFERENCE, pat.span, "this pattern takes a reference on something that is being de-referenced")
55+
}}
56+
}
57+
}
58+

0 commit comments

Comments
 (0)