-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New lint [manual_try_fold
]
#11012
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
New lint [manual_try_fold
]
#11012
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use clippy_utils::{ | ||
diagnostics::span_lint_and_sugg, | ||
is_from_proc_macro, | ||
msrvs::{Msrv, ITERATOR_TRY_FOLD}, | ||
source::snippet_opt, | ||
ty::implements_trait, | ||
}; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{ | ||
def::{DefKind, Res}, | ||
Expr, ExprKind, | ||
}; | ||
use rustc_lint::{LateContext, LintContext}; | ||
use rustc_middle::lint::in_external_macro; | ||
use rustc_span::Span; | ||
|
||
use super::MANUAL_TRY_FOLD; | ||
|
||
pub(super) fn check<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
expr: &Expr<'tcx>, | ||
init: &Expr<'_>, | ||
acc: &Expr<'_>, | ||
fold_span: Span, | ||
msrv: &Msrv, | ||
) { | ||
if !in_external_macro(cx.sess(), fold_span) | ||
&& msrv.meets(ITERATOR_TRY_FOLD) | ||
&& let init_ty = cx.typeck_results().expr_ty(init) | ||
&& let Some(try_trait) = cx.tcx.lang_items().try_trait() | ||
&& implements_trait(cx, init_ty, try_trait, &[]) | ||
&& let ExprKind::Call(path, [first, rest @ ..]) = init.kind | ||
&& let ExprKind::Path(qpath) = path.kind | ||
&& let Res::Def(DefKind::Ctor(_, _), _) = cx.qpath_res(&qpath, path.hir_id) | ||
&& let ExprKind::Closure(closure) = acc.kind | ||
&& !is_from_proc_macro(cx, expr) | ||
Centri3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
&& let Some(args_snip) = closure.fn_arg_span.and_then(|fn_arg_span| snippet_opt(cx, fn_arg_span)) | ||
{ | ||
let init_snip = rest | ||
.is_empty() | ||
.then_some(first.span) | ||
.and_then(|span| snippet_opt(cx, span)) | ||
.unwrap_or("...".to_owned()); | ||
|
||
span_lint_and_sugg( | ||
cx, | ||
MANUAL_TRY_FOLD, | ||
fold_span, | ||
"usage of `Iterator::fold` on a type that implements `Try`", | ||
"use `try_fold` instead", | ||
format!("try_fold({init_snip}, {args_snip} ...)", ), | ||
Applicability::HasPlaceholders, | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,7 @@ mod manual_next_back; | |
mod manual_ok_or; | ||
mod manual_saturating_arithmetic; | ||
mod manual_str_repeat; | ||
mod manual_try_fold; | ||
mod map_clone; | ||
mod map_collect_result_unit; | ||
mod map_err_ignore; | ||
|
@@ -3286,6 +3287,35 @@ declare_clippy_lint! { | |
"calling `.drain(..).collect()` to move all elements into a new collection" | ||
} | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks for usage of `Iterator::fold` with a type that implements `Try`. | ||
/// | ||
/// ### Why is this bad? | ||
/// The code should use `try_fold` instead, which short-circuits on failure, thus opening the | ||
/// door for additional optimizations not possible with `fold` as rustc can guarantee the | ||
/// function is never called on `None`, `Err`, etc., alleviating otherwise necessary checks. It's | ||
/// also slightly more idiomatic. | ||
/// | ||
/// ### Known issues | ||
/// This lint doesn't take into account whether a function does something on the failure case, | ||
/// i.e., whether short-circuiting will affect behavior. Refactoring to `try_fold` is not | ||
/// desirable in those cases. | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
/// vec![1, 2, 3].iter().fold(Some(0i32), |sum, i| sum?.checked_add(*i)); | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// vec![1, 2, 3].iter().try_fold(0i32, |sum, i| sum.checked_add(*i)); | ||
/// ``` | ||
#[clippy::version = "1.72.0"] | ||
pub MANUAL_TRY_FOLD, | ||
perf, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IDK, if If not, I think we should do some tests on more creates or start in pedantic first There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The "known problem" isn't really an issue, imo. The known problem is referring to something like x.iter().fold(Some(1), |mut acc, i| if let Some(acc) = acc { acc.checked_add(i) } else { println!("it's none!"); Some(1) }); (pseudocode) This code is not really possible with But something like x.iter().fold(Some(1), |mut acc, i| acc?.checked_add(i)); (pseudocode again) Can be changed to There are very few cases where that's the desired behavior, as it doesn't change the outcome at all. You can short-circuit there and it changes nothing. Unless the programmer explicitly handles the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also agree that, if this behaviour is well-documented (as I think it is, as it appears in the Also, I don't think I've ever seen a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I'm just a more cautious when it comes to warn-by-default lints. I would really like nightly lints at some point, but first I finally want to stabilize lint reasons. I believe it should be fine :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
In that specific case, sure. But sometimes Example: self.public_key
.public_subkeys
.iter()
.filter(|sub_key| {
if sub_key.key_id().as_ref() == key_id.as_ref() {
log::trace!(
"Found a matching key id {:?} == {:?}",
sub_key.key_id(),
key_id
);
true
} else {
log::trace!("Not the one we want: {:?}", sub_key);
false
}
})
.fold(
Err(Error::KeyNotFoundError {
key_ref: format!("{:?}", key_id),
}),
|previous_res, sub_key| {
if previous_res.is_err() {
log::trace!("Test next candidate subkey");
signature.verify(sub_key, &mut data).map_err(|e| {
Error::VerificationError {
source: Box::new(e),
key_ref: format!("{:?}", sub_key.key_id()),
}
})
} else {
log::trace!("Signature already verified, nop");
Ok(())
}
},
) This code initializes with an error of "KeyNotFoundError", and replaces it with an error of "VerificationError" after a subkey has been tried unsuccessfully, but still tries all of the subkeys before returning an error. If there's a successful result it short-circuits, but in the This might be rare enough to not be worth worrying about - I'm not suggesting that the lint should be reverted necessarily. But the |
||
"checks for usage of `Iterator::fold` with a type that implements `Try`" | ||
} | ||
|
||
pub struct Methods { | ||
avoid_breaking_exported_api: bool, | ||
msrv: Msrv, | ||
|
@@ -3416,7 +3446,8 @@ impl_lint_pass!(Methods => [ | |
CLEAR_WITH_DRAIN, | ||
MANUAL_NEXT_BACK, | ||
UNNECESSARY_LITERAL_UNWRAP, | ||
DRAIN_COLLECT | ||
DRAIN_COLLECT, | ||
MANUAL_TRY_FOLD, | ||
]); | ||
|
||
/// Extracts a method call name, args, and `Span` of the method name. | ||
|
@@ -3709,7 +3740,10 @@ impl Methods { | |
Some(("cloned", recv2, [], _, _)) => iter_overeager_cloned::check(cx, expr, recv, recv2, false, true), | ||
_ => {}, | ||
}, | ||
("fold", [init, acc]) => unnecessary_fold::check(cx, expr, init, acc, span), | ||
("fold", [init, acc]) => { | ||
manual_try_fold::check(cx, expr, init, acc, call_span, &self.msrv); | ||
unnecessary_fold::check(cx, expr, init, acc, span); | ||
}, | ||
("for_each", [_]) => { | ||
if let Some(("inspect", _, [_], span2, _)) = method_call(recv) { | ||
inspect_for_each::check(cx, expr, span2); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
//@aux-build:proc_macros.rs:proc-macro | ||
#![allow(clippy::unnecessary_fold, unused)] | ||
#![warn(clippy::manual_try_fold)] | ||
#![feature(try_trait_v2)] | ||
|
||
use std::ops::ControlFlow; | ||
use std::ops::FromResidual; | ||
use std::ops::Try; | ||
|
||
#[macro_use] | ||
extern crate proc_macros; | ||
|
||
// Test custom `Try` with more than 1 argument | ||
struct NotOption(i32, i32); | ||
|
||
impl<R> FromResidual<R> for NotOption { | ||
fn from_residual(_: R) -> Self { | ||
todo!() | ||
} | ||
} | ||
|
||
impl Try for NotOption { | ||
type Output = (); | ||
type Residual = (); | ||
|
||
fn from_output(_: Self::Output) -> Self { | ||
todo!() | ||
} | ||
|
||
fn branch(self) -> ControlFlow<Self::Residual, Self::Output> { | ||
todo!() | ||
} | ||
} | ||
|
||
// Test custom `Try` with only 1 argument | ||
#[derive(Default)] | ||
struct NotOptionButWorse(i32); | ||
|
||
impl<R> FromResidual<R> for NotOptionButWorse { | ||
fn from_residual(_: R) -> Self { | ||
todo!() | ||
} | ||
} | ||
|
||
impl Try for NotOptionButWorse { | ||
type Output = (); | ||
type Residual = (); | ||
|
||
fn from_output(_: Self::Output) -> Self { | ||
todo!() | ||
} | ||
|
||
fn branch(self) -> ControlFlow<Self::Residual, Self::Output> { | ||
todo!() | ||
} | ||
} | ||
|
||
fn main() { | ||
[1, 2, 3] | ||
.iter() | ||
.fold(Some(0i32), |sum, i| sum?.checked_add(*i)) | ||
.unwrap(); | ||
[1, 2, 3] | ||
.iter() | ||
.fold(NotOption(0i32, 0i32), |sum, i| NotOption(0i32, 0i32)); | ||
[1, 2, 3] | ||
.iter() | ||
.fold(NotOptionButWorse(0i32), |sum, i| NotOptionButWorse(0i32)); | ||
// Do not lint | ||
[1, 2, 3].iter().try_fold(0i32, |sum, i| sum.checked_add(*i)).unwrap(); | ||
[1, 2, 3].iter().fold(0i32, |sum, i| sum + i); | ||
[1, 2, 3] | ||
.iter() | ||
.fold(NotOptionButWorse::default(), |sum, i| NotOptionButWorse::default()); | ||
external! { | ||
[1, 2, 3].iter().fold(Some(0i32), |sum, i| sum?.checked_add(*i)).unwrap(); | ||
[1, 2, 3].iter().try_fold(0i32, |sum, i| sum.checked_add(*i)).unwrap(); | ||
} | ||
with_span! { | ||
span | ||
[1, 2, 3].iter().fold(Some(0i32), |sum, i| sum?.checked_add(*i)).unwrap(); | ||
[1, 2, 3].iter().try_fold(0i32, |sum, i| sum.checked_add(*i)).unwrap(); | ||
} | ||
} | ||
|
||
#[clippy::msrv = "1.26.0"] | ||
fn msrv_too_low() { | ||
[1, 2, 3] | ||
.iter() | ||
.fold(Some(0i32), |sum, i| sum?.checked_add(*i)) | ||
.unwrap(); | ||
} | ||
|
||
#[clippy::msrv = "1.27.0"] | ||
fn msrv_juust_right() { | ||
[1, 2, 3] | ||
.iter() | ||
.fold(Some(0i32), |sum, i| sum?.checked_add(*i)) | ||
.unwrap(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
error: usage of `Iterator::fold` on a type that implements `Try` | ||
--> $DIR/manual_try_fold.rs:61:10 | ||
| | ||
LL | .fold(Some(0i32), |sum, i| sum?.checked_add(*i)) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `try_fold` instead: `try_fold(0i32, |sum, i| ...)` | ||
| | ||
= note: `-D clippy::manual-try-fold` implied by `-D warnings` | ||
|
||
error: usage of `Iterator::fold` on a type that implements `Try` | ||
--> $DIR/manual_try_fold.rs:65:10 | ||
| | ||
LL | .fold(NotOption(0i32, 0i32), |sum, i| NotOption(0i32, 0i32)); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `try_fold` instead: `try_fold(..., |sum, i| ...)` | ||
|
||
error: usage of `Iterator::fold` on a type that implements `Try` | ||
--> $DIR/manual_try_fold.rs:68:10 | ||
| | ||
LL | .fold(NotOptionButWorse(0i32), |sum, i| NotOptionButWorse(0i32)); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `try_fold` instead: `try_fold(0i32, |sum, i| ...)` | ||
|
||
error: usage of `Iterator::fold` on a type that implements `Try` | ||
--> $DIR/manual_try_fold.rs:98:10 | ||
| | ||
LL | .fold(Some(0i32), |sum, i| sum?.checked_add(*i)) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `try_fold` instead: `try_fold(0i32, |sum, i| ...)` | ||
|
||
error: aborting due to 4 previous errors | ||
|
Uh oh!
There was an error while loading. Please reload this page.