Skip to content

Commit 7fa38f6

Browse files
committed
Fix FP with mut_mut and for loops
1 parent 5f6b982 commit 7fa38f6

File tree

2 files changed

+69
-21
lines changed

2 files changed

+69
-21
lines changed

clippy_lints/src/mut_mut.rs

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
use rustc::hir;
2+
use rustc::hir::intravisit;
13
use rustc::lint::*;
24
use rustc::ty::{TypeAndMut, TyRef};
3-
use rustc::hir::*;
4-
use utils::{in_external_macro, span_lint};
5+
use utils::{in_external_macro, recover_for_loop, span_lint};
56

67
/// **What it does:** This lint checks for instances of `mut mut` references.
78
///
@@ -27,30 +28,56 @@ impl LintPass for MutMut {
2728
}
2829

2930
impl LateLintPass for MutMut {
30-
fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
31-
if in_external_macro(cx, expr.span) {
31+
fn check_block(&mut self, cx: &LateContext, block: &hir::Block) {
32+
intravisit::walk_block(&mut MutVisitor { cx: cx }, block);
33+
}
34+
35+
fn check_ty(&mut self, cx: &LateContext, ty: &hir::Ty) {
36+
use rustc::hir::intravisit::Visitor;
37+
38+
MutVisitor { cx: cx }.visit_ty(ty);
39+
}
40+
}
41+
42+
pub struct MutVisitor<'a, 'tcx: 'a> {
43+
cx: &'a LateContext<'a, 'tcx>,
44+
}
45+
46+
impl<'a, 'tcx, 'v> intravisit::Visitor<'v> for MutVisitor<'a, 'tcx> {
47+
fn visit_expr(&mut self, expr: &'v hir::Expr) {
48+
if in_external_macro(self.cx, expr.span) {
3249
return;
3350
}
3451

35-
if let ExprAddrOf(MutMutable, ref e) = expr.node {
36-
if let ExprAddrOf(MutMutable, _) = e.node {
37-
span_lint(cx, MUT_MUT, expr.span, "generally you want to avoid `&mut &mut _` if possible");
38-
} else {
39-
if let TyRef(_, TypeAndMut { mutbl: MutMutable, .. }) = cx.tcx.expr_ty(e).sty {
40-
span_lint(cx,
41-
MUT_MUT,
42-
expr.span,
43-
"this expression mutably borrows a mutable reference. Consider reborrowing");
44-
}
52+
if let Some((_, arg, body)) = recover_for_loop(expr) {
53+
// A `for` loop lowers to:
54+
// ```rust
55+
// match ::std::iter::Iterator::next(&mut iter) {
56+
// // ^^^^
57+
// ```
58+
// Let's ignore the generated code.
59+
intravisit::walk_expr(self, arg);
60+
intravisit::walk_expr(self, body);
61+
} else if let hir::ExprAddrOf(hir::MutMutable, ref e) = expr.node {
62+
if let hir::ExprAddrOf(hir::MutMutable, _) = e.node {
63+
span_lint(self.cx, MUT_MUT, expr.span, "generally you want to avoid `&mut &mut _` if possible");
64+
} else if let TyRef(_, TypeAndMut { mutbl: hir::MutMutable, .. }) = self.cx.tcx.expr_ty(e).sty {
65+
span_lint(self.cx,
66+
MUT_MUT,
67+
expr.span,
68+
"this expression mutably borrows a mutable reference. Consider reborrowing");
4569
}
4670
}
4771
}
4872

49-
fn check_ty(&mut self, cx: &LateContext, ty: &Ty) {
50-
if let TyRptr(_, MutTy { ty: ref pty, mutbl: MutMutable }) = ty.node {
51-
if let TyRptr(_, MutTy { mutbl: MutMutable, .. }) = pty.node {
52-
span_lint(cx, MUT_MUT, ty.span, "generally you want to avoid `&mut &mut _` if possible");
73+
fn visit_ty(&mut self, ty: &hir::Ty) {
74+
if let hir::TyRptr(_, hir::MutTy { ty: ref pty, mutbl: hir::MutMutable }) = ty.node {
75+
if let hir::TyRptr(_, hir::MutTy { mutbl: hir::MutMutable, .. }) = pty.node {
76+
span_lint(self.cx, MUT_MUT, ty.span, "generally you want to avoid `&mut &mut _` if possible");
5377
}
78+
5479
}
80+
81+
intravisit::walk_ty(self, ty);
5582
}
5683
}

tests/compile-fail/mut_mut.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22
#![plugin(clippy)]
33

44
#![allow(unused, no_effect, unnecessary_operation)]
5+
#![deny(mut_mut)]
56

67
//#![plugin(regex_macros)]
78
//extern crate regex;
89

9-
#[deny(mut_mut)]
1010
fn fun(x : &mut &mut u32) -> bool { //~ERROR generally you want to avoid `&mut &mut
1111
**x > 0
1212
}
1313

14-
#[deny(mut_mut)]
1514
fn less_fun(x : *mut *mut u32) {
1615
let y = x;
1716
}
@@ -21,23 +20,45 @@ macro_rules! mut_ptr {
2120
//~^ ERROR generally you want to avoid `&mut &mut
2221
}
2322

24-
#[deny(mut_mut)]
2523
#[allow(unused_mut, unused_variables)]
2624
fn main() {
2725
let mut x = &mut &mut 1u32; //~ERROR generally you want to avoid `&mut &mut
2826
{
2927
let mut y = &mut x; //~ERROR this expression mutably borrows a mutable reference
3028
}
3129

30+
if fun(x) {
31+
let y : &mut &mut u32 = &mut &mut 2;
32+
//~^ ERROR generally you want to avoid `&mut &mut
33+
//~| ERROR generally you want to avoid `&mut &mut
34+
//~| ERROR generally you want to avoid `&mut &mut
35+
**y + **x;
36+
}
37+
3238
if fun(x) {
3339
let y : &mut &mut &mut u32 = &mut &mut &mut 2;
3440
//~^ ERROR generally you want to avoid `&mut &mut
3541
//~| ERROR generally you want to avoid `&mut &mut
3642
//~| ERROR generally you want to avoid `&mut &mut
3743
//~| ERROR generally you want to avoid `&mut &mut
44+
//~| ERROR generally you want to avoid `&mut &mut
45+
//~| ERROR generally you want to avoid `&mut &mut
3846
***y + **x;
3947
}
4048

4149
let mut z = mut_ptr!(&mut 3u32);
4250
//~^ NOTE in this expansion of mut_ptr!
4351
}
52+
53+
fn issue939() {
54+
let array = [5, 6, 7, 8, 9];
55+
let mut args = array.iter().skip(2);
56+
for &arg in &mut args {
57+
println!("{}", arg);
58+
}
59+
60+
let args = &mut args;
61+
for arg in args {
62+
println!(":{}", arg);
63+
}
64+
}

0 commit comments

Comments
 (0)