Skip to content

Commit 3e475e9

Browse files
committed
Merge pull request #354 from Pyriphlegethon/master
Add "unnecessary mut passed" lint
2 parents 85ac834 + 390168c commit 3e475e9

File tree

5 files changed

+126
-2
lines changed

5 files changed

+126
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ A collection of lints to catch common mistakes and improve your Rust code.
66
[Jump to usage instructions](#usage)
77

88
##Lints
9-
There are 58 lints included in this crate:
9+
There are 59 lints included in this crate:
1010

1111
name | default | meaning
1212
-------------------------------------------------------------------------------------------------------|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@@ -63,6 +63,7 @@ name
6363
[type_complexity](https://github.com/Manishearth/rust-clippy/wiki#type_complexity) | warn | usage of very complex types; recommends factoring out parts into `type` definitions
6464
[unicode_not_nfc](https://github.com/Manishearth/rust-clippy/wiki#unicode_not_nfc) | allow | using a unicode literal not in NFC normal form (see http://www.unicode.org/reports/tr15/ for further information)
6565
[unit_cmp](https://github.com/Manishearth/rust-clippy/wiki#unit_cmp) | warn | comparing unit values (which is always `true` or `false`, respectively)
66+
[unnecessary_mut_passed](https://github.com/Manishearth/rust-clippy/wiki#unnecessary_mut_passed) | warn | an argument is passed as a mutable reference although the function/method only demands an immutable reference
6667
[unused_collect](https://github.com/Manishearth/rust-clippy/wiki#unused_collect) | warn | `collect()`ing an iterator without using the result; this is usually better written as a for loop
6768
[while_let_loop](https://github.com/Manishearth/rust-clippy/wiki#while_let_loop) | warn | `loop { if let { ... } else break }` can be written as a `while let` loop
6869
[wrong_pub_self_convention](https://github.com/Manishearth/rust-clippy/wiki#wrong_pub_self_convention) | allow | defining a public method named with an established prefix (like "into_") that takes `self` with the wrong convention

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub mod eta_reduction;
3333
pub mod identity_op;
3434
pub mod minmax;
3535
pub mod mut_mut;
36+
pub mod mut_reference;
3637
pub mod len_zero;
3738
pub mod attrs;
3839
pub mod collapsible_if;
@@ -66,6 +67,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
6667
reg.register_late_lint_pass(box eta_reduction::EtaPass);
6768
reg.register_late_lint_pass(box identity_op::IdentityOp);
6869
reg.register_late_lint_pass(box mut_mut::MutMut);
70+
reg.register_late_lint_pass(box mut_reference::UnnecessaryMutPassed);
6971
reg.register_late_lint_pass(box len_zero::LenZero);
7072
reg.register_late_lint_pass(box misc::CmpOwned);
7173
reg.register_late_lint_pass(box attrs::AttrPass);
@@ -138,6 +140,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
138140
misc::MODULO_ONE,
139141
misc::REDUNDANT_PATTERN,
140142
misc::TOPLEVEL_REF_ARG,
143+
mut_reference::UNNECESSARY_MUT_PASSED,
141144
needless_bool::NEEDLESS_BOOL,
142145
precedence::PRECEDENCE,
143146
ranges::RANGE_STEP_BY_ZERO,

src/mut_reference.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
use rustc::lint::*;
2+
use rustc_front::hir::*;
3+
use utils::span_lint;
4+
use rustc::middle::ty::{TypeAndMut, TypeVariants, MethodCall, TyS};
5+
use syntax::ptr::P;
6+
7+
declare_lint! {
8+
pub UNNECESSARY_MUT_PASSED,
9+
Warn,
10+
"an argument is passed as a mutable reference although the function/method only demands an \
11+
immutable reference"
12+
}
13+
14+
15+
#[derive(Copy,Clone)]
16+
pub struct UnnecessaryMutPassed;
17+
18+
impl LintPass for UnnecessaryMutPassed {
19+
fn get_lints(&self) -> LintArray {
20+
lint_array!(UNNECESSARY_MUT_PASSED)
21+
}
22+
}
23+
24+
impl LateLintPass for UnnecessaryMutPassed {
25+
fn check_expr(&mut self, cx: &LateContext, e: &Expr) {
26+
let borrowed_table = cx.tcx.tables.borrow();
27+
match e.node {
28+
ExprCall(ref fn_expr, ref arguments) => {
29+
match borrowed_table.node_types.get(&fn_expr.id) {
30+
Some(function_type) => {
31+
if let ExprPath(_, ref path) = fn_expr.node {
32+
check_arguments(cx, &arguments, function_type,
33+
&format!("{}", path));
34+
}
35+
},
36+
None => unreachable!(), // A function with unknown type is called.
37+
// If this happened the compiler would have aborted the
38+
// compilation long ago.
39+
};
40+
41+
42+
},
43+
ExprMethodCall(ref name, _, ref arguments) => {
44+
let method_call = MethodCall::expr(e.id);
45+
match borrowed_table.method_map.get(&method_call) {
46+
Some(method_type) => check_arguments(cx, &arguments, method_type.ty,
47+
&format!("{}", name.node.as_str())),
48+
None => unreachable!(), // Just like above, this should never happen.
49+
};
50+
},
51+
_ => {}
52+
}
53+
}
54+
}
55+
56+
fn check_arguments(cx: &LateContext, arguments: &[P<Expr>], type_definition: &TyS, name: &str) {
57+
if let TypeVariants::TyBareFn(_, ref fn_type) = type_definition.sty {
58+
let parameters = &fn_type.sig.skip_binder().inputs;
59+
for (argument, parameter) in arguments.iter().zip(parameters.iter()) {
60+
match parameter.sty {
61+
TypeVariants::TyRef(_, TypeAndMut {ty: _, mutbl: MutImmutable}) |
62+
TypeVariants::TyRawPtr(TypeAndMut {ty: _, mutbl: MutImmutable}) => {
63+
if let Expr_::ExprAddrOf(MutMutable, _) = argument.node {
64+
span_lint(cx, UNNECESSARY_MUT_PASSED,
65+
argument.span, &format!("The function/method \"{}\" \
66+
doesn't need a mutable reference",
67+
name));
68+
}
69+
},
70+
_ => {}
71+
}
72+
}
73+
}
74+
}

tests/compile-fail/for_loop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ impl Unrelated {
1616

1717
#[deny(needless_range_loop, explicit_iter_loop, iter_next_loop, reverse_range_loop, explicit_counter_loop)]
1818
#[deny(unused_collect)]
19-
#[allow(linkedlist,shadow_unrelated)]
19+
#[allow(linkedlist,shadow_unrelated,unnecessary_mut_passed)]
2020
fn main() {
2121
let mut vec = vec![1, 2, 3, 4];
2222
let vec2 = vec![1, 2, 3, 4];

tests/compile-fail/mut_reference.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#![feature(plugin)]
2+
#![plugin(clippy)]
3+
4+
#![allow(unused_variables)]
5+
6+
fn takes_an_immutable_reference(a: &i32) {
7+
}
8+
9+
10+
fn takes_a_mutable_reference(a: &mut i32) {
11+
}
12+
13+
struct MyStruct;
14+
15+
impl MyStruct {
16+
fn takes_an_immutable_reference(&self, a: &i32) {
17+
}
18+
19+
fn takes_a_mutable_reference(&self, a: &mut i32) {
20+
}
21+
}
22+
23+
#[deny(unnecessary_mut_passed)]
24+
fn main() {
25+
// Functions
26+
takes_an_immutable_reference(&mut 42); //~ERROR The function/method "takes_an_immutable_reference" doesn't need a mutable reference
27+
28+
// Methods
29+
let my_struct = MyStruct;
30+
my_struct.takes_an_immutable_reference(&mut 42); //~ERROR The function/method "takes_an_immutable_reference" doesn't need a mutable reference
31+
32+
33+
// No error
34+
35+
// Functions
36+
takes_an_immutable_reference(&42);
37+
takes_a_mutable_reference(&mut 42);
38+
let a = &mut 42;
39+
takes_an_immutable_reference(a);
40+
41+
// Methods
42+
my_struct.takes_an_immutable_reference(&42);
43+
my_struct.takes_a_mutable_reference(&mut 42);
44+
my_struct.takes_an_immutable_reference(a);
45+
46+
}

0 commit comments

Comments
 (0)