Skip to content

WIP: mut_mut: generalize the from-expansion check #15419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions clippy_lints/src/mut_mut.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use clippy_utils::diagnostics::{span_lint, span_lint_hir};
use clippy_utils::higher;
use rustc_hir::{self as hir, AmbigArg, intravisit};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::ty;
Expand Down Expand Up @@ -55,20 +54,11 @@ pub struct MutVisitor<'a, 'tcx> {

impl<'tcx> intravisit::Visitor<'tcx> for MutVisitor<'_, 'tcx> {
fn visit_expr(&mut self, expr: &'tcx hir::Expr<'_>) {
if expr.span.in_external_macro(self.cx.sess().source_map()) {
if expr.span.from_expansion() {
return;
}

if let Some(higher::ForLoop { arg, body, .. }) = higher::ForLoop::hir(expr) {
// A `for` loop lowers to:
// ```rust
// match ::std::iter::Iterator::next(&mut iter) {
// // ^^^^
// ```
// Let's ignore the generated code.
intravisit::walk_expr(self, arg);
intravisit::walk_expr(self, body);
} else if let hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Mut, e) = expr.kind {
if let hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Mut, e) = expr.kind {
if let hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Mut, _) = e.kind {
span_lint_hir(
self.cx,
Expand Down
9 changes: 8 additions & 1 deletion tests/ui/mut_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,25 @@ fn main() {
}

let mut z = inline!(&mut $(&mut 3u32));
//~^ mut_mut
}

fn issue939() {
let array = [5, 6, 7, 8, 9];
let mut args = array.iter().skip(2);
for &arg in &mut args {
// make sure that we still check inside the block, even if we skip `&mut args` because of
// it coming from the expansion of `for-in` syntax
let a = &mut &mut 2;
//~^ mut_mut
println!("{}", arg);
}

let args = &mut args;
for arg in args {
// make sure that we still check inside the block, even if we skip `&mut args` because of
// it coming from the expansion of `for-in` syntax
let a = &mut &mut 2;
//~^ mut_mut
println!(":{}", arg);
}
}
Expand Down
22 changes: 13 additions & 9 deletions tests/ui/mut_mut.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,6 @@ error: generally you want to avoid `&mut &mut _` if possible
LL | let mut x = &mut &mut 1u32;
| ^^^^^^^^^^^^^^

error: generally you want to avoid `&mut &mut _` if possible
--> tests/ui/mut_mut.rs:55:25
|
LL | let mut z = inline!(&mut $(&mut 3u32));
| ^
|
= note: this error originates in the macro `__inline_mac_fn_main` (in Nightly builds, run with -Z macro-backtrace for more info)

error: this expression mutably borrows a mutable reference. Consider reborrowing
--> tests/ui/mut_mut.rs:36:21
|
Expand Down Expand Up @@ -57,5 +49,17 @@ error: generally you want to avoid `&mut &mut _` if possible
LL | let y: &mut &mut &mut u32 = &mut &mut &mut 2;
| ^^^^^^^^^^^^^

error: aborting due to 9 previous errors
error: generally you want to avoid `&mut &mut _` if possible
--> tests/ui/mut_mut.rs:64:17
|
LL | let a = &mut &mut 2;
| ^^^^^^^^^^^

error: generally you want to avoid `&mut &mut _` if possible
--> tests/ui/mut_mut.rs:73:17
|
LL | let a = &mut &mut 2;
| ^^^^^^^^^^^

error: aborting due to 10 previous errors