-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #93001 - flip1995:clippyup, r=Manishearth
Out of cycle Clippy update I want to do an out-of-cycle sync for #8295, and possibly backport this to stable together with rust-lang/rust#92938. If this doesn't get backported to stable, then I at least want to backport it to beta. r? `@Manishearth`
- Loading branch information
Showing
43 changed files
with
754 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::source::snippet; | ||
use clippy_utils::ty::{get_iterator_item_ty, is_copy}; | ||
use itertools::Itertools; | ||
use rustc_errors::Applicability; | ||
use rustc_hir as hir; | ||
use rustc_lint::LateContext; | ||
use rustc_middle::ty; | ||
use std::ops::Not; | ||
|
||
use super::ITER_OVEREAGER_CLONED; | ||
use crate::redundant_clone::REDUNDANT_CLONE; | ||
|
||
/// lint overeager use of `cloned()` for `Iterator`s | ||
pub(super) fn check<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
expr: &'tcx hir::Expr<'_>, | ||
recv: &'tcx hir::Expr<'_>, | ||
name: &str, | ||
map_arg: &[hir::Expr<'_>], | ||
) { | ||
// Check if it's iterator and get type associated with `Item`. | ||
let inner_ty = match get_iterator_item_ty(cx, cx.typeck_results().expr_ty_adjusted(recv)) { | ||
Some(ty) => ty, | ||
_ => return, | ||
}; | ||
|
||
match inner_ty.kind() { | ||
ty::Ref(_, ty, _) if !is_copy(cx, ty) => {}, | ||
_ => return, | ||
}; | ||
|
||
let (lint, preserve_cloned) = match name { | ||
"count" => (REDUNDANT_CLONE, false), | ||
_ => (ITER_OVEREAGER_CLONED, true), | ||
}; | ||
let wildcard_params = map_arg.is_empty().not().then(|| "...").unwrap_or_default(); | ||
let msg = format!( | ||
"called `cloned().{}({})` on an `Iterator`. It may be more efficient to call `{}({}){}` instead", | ||
name, | ||
wildcard_params, | ||
name, | ||
wildcard_params, | ||
preserve_cloned.then(|| ".cloned()").unwrap_or_default(), | ||
); | ||
|
||
span_lint_and_sugg( | ||
cx, | ||
lint, | ||
expr.span, | ||
&msg, | ||
"try this", | ||
format!( | ||
"{}.{}({}){}", | ||
snippet(cx, recv.span, ".."), | ||
name, | ||
map_arg.iter().map(|a| snippet(cx, a.span, "..")).join(", "), | ||
preserve_cloned.then(|| ".cloned()").unwrap_or_default(), | ||
), | ||
Applicability::MachineApplicable, | ||
); | ||
} |
Oops, something went wrong.