Skip to content

Commit caa7394

Browse files
committed
Auto merge of rust-lang#11879 - samueltardieu:issue-11876, r=Alexendoo
`manual_try_fold`: check that `fold` is really `Iterator::fold` Fix rust-lang#11876 changelog: [`manual_try_fold`]: suggest using `try_fold` only for `Iterator::fold` uses
2 parents 5ba6480 + 0d09cb0 commit caa7394

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

clippy_lints/src/methods/manual_try_fold.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use clippy_config::msrvs::{self, Msrv};
22
use clippy_utils::diagnostics::span_lint_and_sugg;
3-
use clippy_utils::is_from_proc_macro;
43
use clippy_utils::source::snippet_opt;
54
use clippy_utils::ty::implements_trait;
5+
use clippy_utils::{is_from_proc_macro, is_trait_method};
66
use rustc_errors::Applicability;
77
use rustc_hir::def::{DefKind, Res};
88
use rustc_hir::{Expr, ExprKind};
99
use rustc_lint::{LateContext, LintContext};
1010
use rustc_middle::lint::in_external_macro;
11-
use rustc_span::Span;
11+
use rustc_span::{sym, Span};
1212

1313
use super::MANUAL_TRY_FOLD;
1414

@@ -22,6 +22,7 @@ pub(super) fn check<'tcx>(
2222
) {
2323
if !in_external_macro(cx.sess(), fold_span)
2424
&& msrv.meets(msrvs::ITERATOR_TRY_FOLD)
25+
&& is_trait_method(cx, expr, sym::Iterator)
2526
&& let init_ty = cx.typeck_results().expr_ty(init)
2627
&& let Some(try_trait) = cx.tcx.lang_items().try_trait()
2728
&& implements_trait(cx, init_ty, try_trait, &[])

tests/ui/manual_try_fold.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,33 @@ fn msrv_juust_right() {
9696
.fold(Some(0i32), |sum, i| sum?.checked_add(*i))
9797
.unwrap();
9898
}
99+
100+
mod issue11876 {
101+
struct Foo;
102+
103+
impl Bar for Foo {
104+
type Output = u32;
105+
}
106+
107+
trait Bar: Sized {
108+
type Output;
109+
fn fold<A, F>(self, init: A, func: F) -> Fold<Self, A, F>
110+
where
111+
A: Clone,
112+
F: Fn(A, Self::Output) -> A,
113+
{
114+
Fold { this: self, init, func }
115+
}
116+
}
117+
118+
#[allow(dead_code)]
119+
struct Fold<S, A, F> {
120+
this: S,
121+
init: A,
122+
func: F,
123+
}
124+
125+
fn main() {
126+
Foo.fold(Some(0), |acc, entry| Some(acc? + entry));
127+
}
128+
}

0 commit comments

Comments
 (0)