Exclude pattern guards from unnecessary_fold lint

Methods like `Iterator::any` borrow the iterator mutably,
which is not allowed within a pattern guard and will fail to compile.
This commit prevents clippy from suggesting this type of change.

Closes #3069
This commit is contained in:
Joshua Holmer 2018-10-12 10:26:55 -04:00
parent d445dbfe16
commit d3c06f7252
2 changed files with 25 additions and 0 deletions

View file

@ -9,6 +9,7 @@
use crate::rustc::hir;
use crate::rustc::hir::{ExprKind, Guard, Node};
use crate::rustc::hir::def::Def;
use crate::rustc::lint::{in_external_macro, LateContext, LateLintPass, Lint, LintArray, LintContext, LintPass};
use crate::rustc::ty::{self, Ty};
@ -1428,6 +1429,23 @@ fn lint_unnecessary_fold(cx: &LateContext<'_, '_>, expr: &hir::Expr, fold_args:
return;
}
// `Iterator::any` cannot be used within a pattern guard
// See https://github.com/rust-lang-nursery/rust-clippy/issues/3069
if_chain! {
if let Some(fold_parent) = cx.tcx.hir.find(cx.tcx.hir.get_parent_node(expr.id));
if let Node::Expr(fold_parent) = fold_parent;
if let ExprKind::Match(_, ref arms, _) = fold_parent.node;
if arms.iter().any(|arm| {
if let Some(Guard::If(ref guard)) = arm.guard {
return guard.id == expr.id;
}
false
});
then {
return;
}
}
assert!(fold_args.len() == 3,
"Expected fold_args to have three entries - the receiver, the initial value and the closure");