mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-12-04 02:20:04 +00:00
Reborrow mutable references in explicit_iter_loop
This commit is contained in:
parent
482baf2bcc
commit
949712c90a
24 changed files with 159 additions and 58 deletions
|
@ -808,7 +808,7 @@ fn check_deprecated_cfg_attr(cx: &EarlyContext<'_>, attr: &Attribute, msrv: &Msr
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_nested_cfg(cx: &EarlyContext<'_>, items: &[NestedMetaItem]) {
|
fn check_nested_cfg(cx: &EarlyContext<'_>, items: &[NestedMetaItem]) {
|
||||||
for item in items.iter() {
|
for item in items {
|
||||||
if let NestedMetaItem::MetaItem(meta) = item {
|
if let NestedMetaItem::MetaItem(meta) = item {
|
||||||
if !meta.has_name(sym::any) && !meta.has_name(sym::all) {
|
if !meta.has_name(sym::any) && !meta.has_name(sym::all) {
|
||||||
continue;
|
continue;
|
||||||
|
@ -842,7 +842,7 @@ fn check_nested_cfg(cx: &EarlyContext<'_>, items: &[NestedMetaItem]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_nested_misused_cfg(cx: &EarlyContext<'_>, items: &[NestedMetaItem]) {
|
fn check_nested_misused_cfg(cx: &EarlyContext<'_>, items: &[NestedMetaItem]) {
|
||||||
for item in items.iter() {
|
for item in items {
|
||||||
if let NestedMetaItem::MetaItem(meta) = item {
|
if let NestedMetaItem::MetaItem(meta) = item {
|
||||||
if meta.has_name(sym!(features)) && let Some(val) = meta.value_str() {
|
if meta.has_name(sym!(features)) && let Some(val) = meta.value_str() {
|
||||||
span_lint_and_sugg(
|
span_lint_and_sugg(
|
||||||
|
|
|
@ -161,7 +161,7 @@ impl<'a, 'tcx> Visitor<'tcx> for NumericFallbackVisitor<'a, 'tcx> {
|
||||||
let fields_def = &variant.fields;
|
let fields_def = &variant.fields;
|
||||||
|
|
||||||
// Push field type then visit each field expr.
|
// Push field type then visit each field expr.
|
||||||
for field in fields.iter() {
|
for field in *fields {
|
||||||
let bound =
|
let bound =
|
||||||
fields_def
|
fields_def
|
||||||
.iter()
|
.iter()
|
||||||
|
|
|
@ -562,7 +562,7 @@ fn has_where_lifetimes<'tcx>(cx: &LateContext<'tcx>, generics: &'tcx Generics<'_
|
||||||
// if the bounds define new lifetimes, they are fine to occur
|
// if the bounds define new lifetimes, they are fine to occur
|
||||||
let allowed_lts = allowed_lts_from(pred.bound_generic_params);
|
let allowed_lts = allowed_lts_from(pred.bound_generic_params);
|
||||||
// now walk the bounds
|
// now walk the bounds
|
||||||
for bound in pred.bounds.iter() {
|
for bound in pred.bounds {
|
||||||
walk_param_bound(&mut visitor, bound);
|
walk_param_bound(&mut visitor, bound);
|
||||||
}
|
}
|
||||||
// and check that all lifetimes are allowed
|
// and check that all lifetimes are allowed
|
||||||
|
|
|
@ -84,7 +84,7 @@ pub(super) fn check(cx: &LateContext<'_>, self_arg: &Expr<'_>, call_expr: &Expr<
|
||||||
"it is more concise to loop over containers instead of using explicit \
|
"it is more concise to loop over containers instead of using explicit \
|
||||||
iteration methods",
|
iteration methods",
|
||||||
"to write this more concisely, try",
|
"to write this more concisely, try",
|
||||||
format!("{}{}", adjust.display(), object.to_string()),
|
format!("{}{object}", adjust.display()),
|
||||||
applicability,
|
applicability,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,14 +33,6 @@ pub(super) fn check(cx: &LateContext<'_>, self_arg: &Expr<'_>, call_expr: &Expr<
|
||||||
|
|
||||||
let mut applicability = Applicability::MachineApplicable;
|
let mut applicability = Applicability::MachineApplicable;
|
||||||
let object = snippet_with_applicability(cx, self_arg.span, "_", &mut applicability);
|
let object = snippet_with_applicability(cx, self_arg.span, "_", &mut applicability);
|
||||||
let prefix = match adjust {
|
|
||||||
AdjustKind::None => "",
|
|
||||||
AdjustKind::Borrow => "&",
|
|
||||||
AdjustKind::BorrowMut => "&mut ",
|
|
||||||
AdjustKind::Deref => "*",
|
|
||||||
AdjustKind::Reborrow => "&*",
|
|
||||||
AdjustKind::ReborrowMut => "&mut *",
|
|
||||||
};
|
|
||||||
span_lint_and_sugg(
|
span_lint_and_sugg(
|
||||||
cx,
|
cx,
|
||||||
EXPLICIT_ITER_LOOP,
|
EXPLICIT_ITER_LOOP,
|
||||||
|
@ -48,11 +40,12 @@ pub(super) fn check(cx: &LateContext<'_>, self_arg: &Expr<'_>, call_expr: &Expr<
|
||||||
"it is more concise to loop over references to containers instead of using explicit \
|
"it is more concise to loop over references to containers instead of using explicit \
|
||||||
iteration methods",
|
iteration methods",
|
||||||
"to write this more concisely, try",
|
"to write this more concisely, try",
|
||||||
format!("{prefix}{object}"),
|
format!("{}{object}", adjust.display()),
|
||||||
applicability,
|
applicability,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
enum AdjustKind {
|
enum AdjustKind {
|
||||||
None,
|
None,
|
||||||
Borrow,
|
Borrow,
|
||||||
|
@ -76,16 +69,35 @@ impl AdjustKind {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reborrow(mutbl: AutoBorrowMutability) -> Self {
|
fn reborrow(mutbl: Mutability) -> Self {
|
||||||
|
match mutbl {
|
||||||
|
Mutability::Not => Self::Reborrow,
|
||||||
|
Mutability::Mut => Self::ReborrowMut,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn auto_reborrow(mutbl: AutoBorrowMutability) -> Self {
|
||||||
match mutbl {
|
match mutbl {
|
||||||
AutoBorrowMutability::Not => Self::Reborrow,
|
AutoBorrowMutability::Not => Self::Reborrow,
|
||||||
AutoBorrowMutability::Mut { .. } => Self::ReborrowMut,
|
AutoBorrowMutability::Mut { .. } => Self::ReborrowMut,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn display(self) -> &'static str {
|
||||||
|
match self {
|
||||||
|
Self::None => "",
|
||||||
|
Self::Borrow => "&",
|
||||||
|
Self::BorrowMut => "&mut ",
|
||||||
|
Self::Deref => "*",
|
||||||
|
Self::Reborrow => "&*",
|
||||||
|
Self::ReborrowMut => "&mut *",
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Checks if an `iter` or `iter_mut` call returns `IntoIterator::IntoIter`. Returns how the
|
/// Checks if an `iter` or `iter_mut` call returns `IntoIterator::IntoIter`. Returns how the
|
||||||
/// argument needs to be adjusted.
|
/// argument needs to be adjusted.
|
||||||
|
#[expect(clippy::too_many_lines)]
|
||||||
fn is_ref_iterable<'tcx>(
|
fn is_ref_iterable<'tcx>(
|
||||||
cx: &LateContext<'tcx>,
|
cx: &LateContext<'tcx>,
|
||||||
self_arg: &Expr<'_>,
|
self_arg: &Expr<'_>,
|
||||||
|
@ -108,27 +120,50 @@ fn is_ref_iterable<'tcx>(
|
||||||
let self_is_copy = is_copy(cx, self_ty);
|
let self_is_copy = is_copy(cx, self_ty);
|
||||||
|
|
||||||
if adjustments.is_empty() && self_is_copy {
|
if adjustments.is_empty() && self_is_copy {
|
||||||
|
// Exact type match, already checked earlier
|
||||||
return Some((AdjustKind::None, self_ty));
|
return Some((AdjustKind::None, self_ty));
|
||||||
}
|
}
|
||||||
|
|
||||||
let res_ty = cx.tcx.erase_regions(EarlyBinder::bind(req_res_ty).subst(cx.tcx, typeck.node_substs(call_expr.hir_id)));
|
let res_ty = cx.tcx.erase_regions(EarlyBinder::bind(req_res_ty)
|
||||||
if !adjustments.is_empty() && self_is_copy {
|
.subst(cx.tcx, typeck.node_substs(call_expr.hir_id)));
|
||||||
if implements_trait(cx, self_ty, trait_id, &[])
|
|
||||||
&& let Some(ty) = make_normalized_projection(cx.tcx, cx.param_env, trait_id, sym!(IntoIter), [self_ty])
|
|
||||||
&& ty == res_ty
|
|
||||||
{
|
|
||||||
return Some((AdjustKind::None, self_ty));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let mutbl = if let ty::Ref(_, _, mutbl) = *req_self_ty.kind() {
|
let mutbl = if let ty::Ref(_, _, mutbl) = *req_self_ty.kind() {
|
||||||
Some(mutbl)
|
Some(mutbl)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if !adjustments.is_empty() {
|
||||||
|
if self_is_copy {
|
||||||
|
// Using by value won't consume anything
|
||||||
|
if implements_trait(cx, self_ty, trait_id, &[])
|
||||||
|
&& let Some(ty) =
|
||||||
|
make_normalized_projection(cx.tcx, cx.param_env, trait_id, sym!(IntoIter), [self_ty])
|
||||||
|
&& ty == res_ty
|
||||||
|
{
|
||||||
|
return Some((AdjustKind::None, self_ty));
|
||||||
|
}
|
||||||
|
} else if let ty::Ref(region, ty, Mutability::Mut) = *self_ty.kind()
|
||||||
|
&& let Some(mutbl) = mutbl
|
||||||
|
{
|
||||||
|
// Attempt to reborrow the mutable reference
|
||||||
|
let self_ty = if mutbl.is_mut() {
|
||||||
|
self_ty
|
||||||
|
} else {
|
||||||
|
cx.tcx.mk_ref(region, TypeAndMut { ty, mutbl })
|
||||||
|
};
|
||||||
|
if implements_trait(cx, self_ty, trait_id, &[])
|
||||||
|
&& let Some(ty) =
|
||||||
|
make_normalized_projection(cx.tcx, cx.param_env, trait_id, sym!(IntoIter), [self_ty])
|
||||||
|
&& ty == res_ty
|
||||||
|
{
|
||||||
|
return Some((AdjustKind::reborrow(mutbl), self_ty));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
if let Some(mutbl) = mutbl
|
if let Some(mutbl) = mutbl
|
||||||
&& !self_ty.is_ref()
|
&& !self_ty.is_ref()
|
||||||
{
|
{
|
||||||
|
// Attempt to borrow
|
||||||
let self_ty = cx.tcx.mk_ref(cx.tcx.lifetimes.re_erased, TypeAndMut {
|
let self_ty = cx.tcx.mk_ref(cx.tcx.lifetimes.re_erased, TypeAndMut {
|
||||||
ty: self_ty,
|
ty: self_ty,
|
||||||
mutbl,
|
mutbl,
|
||||||
|
@ -157,7 +192,7 @@ fn is_ref_iterable<'tcx>(
|
||||||
make_normalized_projection(cx.tcx, cx.param_env, trait_id, sym!(IntoIter), [target])
|
make_normalized_projection(cx.tcx, cx.param_env, trait_id, sym!(IntoIter), [target])
|
||||||
&& ty == res_ty
|
&& ty == res_ty
|
||||||
{
|
{
|
||||||
Some((AdjustKind::reborrow(mutbl), target))
|
Some((AdjustKind::auto_reborrow(mutbl), target))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
|
@ -687,6 +687,8 @@ impl<'tcx> LateLintPass<'tcx> for Loops {
|
||||||
manual_while_let_some::check(cx, condition, body, span);
|
manual_while_let_some::check(cx, condition, body, span);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extract_msrv_attr!(LateContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Loops {
|
impl Loops {
|
||||||
|
|
|
@ -148,7 +148,7 @@ impl<'a, 'tcx> Visitor<'tcx> for SameItemPushVisitor<'a, 'tcx> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_block(&mut self, b: &'tcx Block<'_>) {
|
fn visit_block(&mut self, b: &'tcx Block<'_>) {
|
||||||
for stmt in b.stmts.iter() {
|
for stmt in b.stmts {
|
||||||
self.visit_stmt(stmt);
|
self.visit_stmt(stmt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -101,7 +101,7 @@ impl<'tcx> LateLintPass<'tcx> for MacroUseImports {
|
||||||
});
|
});
|
||||||
if !id.is_local();
|
if !id.is_local();
|
||||||
then {
|
then {
|
||||||
for kid in cx.tcx.module_children(id).iter() {
|
for kid in cx.tcx.module_children(id) {
|
||||||
if let Res::Def(DefKind::Macro(_mac_type), mac_id) = kid.res {
|
if let Res::Def(DefKind::Macro(_mac_type), mac_id) = kid.res {
|
||||||
let span = mac_attr.span;
|
let span = mac_attr.span;
|
||||||
let def_path = cx.tcx.def_path_str(mac_id);
|
let def_path = cx.tcx.def_path_str(mac_id);
|
||||||
|
|
|
@ -25,7 +25,7 @@ pub(crate) fn check<'tcx>(cx: &LateContext<'tcx>, ex: &Expr<'tcx>, arms: &[Arm<'
|
||||||
let mut ident_bind_name = kw::Underscore;
|
let mut ident_bind_name = kw::Underscore;
|
||||||
if !matching_wild {
|
if !matching_wild {
|
||||||
// Looking for unused bindings (i.e.: `_e`)
|
// Looking for unused bindings (i.e.: `_e`)
|
||||||
for pat in inner.iter() {
|
for pat in inner {
|
||||||
if let PatKind::Binding(_, id, ident, None) = pat.kind {
|
if let PatKind::Binding(_, id, ident, None) = pat.kind {
|
||||||
if ident.as_str().starts_with('_') && !is_local_used(cx, arm.body, id) {
|
if ident.as_str().starts_with('_') && !is_local_used(cx, arm.body, id) {
|
||||||
ident_bind_name = ident.name;
|
ident_bind_name = ident.name;
|
||||||
|
|
|
@ -140,7 +140,7 @@ impl<'a, 'tcx> SigDropChecker<'a, 'tcx> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for generic_arg in b.iter() {
|
for generic_arg in *b {
|
||||||
if let GenericArgKind::Type(ty) = generic_arg.unpack() {
|
if let GenericArgKind::Type(ty) = generic_arg.unpack() {
|
||||||
if self.has_sig_drop_attr(cx, ty) {
|
if self.has_sig_drop_attr(cx, ty) {
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -81,7 +81,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, scrutine
|
||||||
/// Finds function return type by examining return expressions in match arms.
|
/// Finds function return type by examining return expressions in match arms.
|
||||||
fn find_return_type<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx ExprKind<'_>) -> Option<Ty<'tcx>> {
|
fn find_return_type<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx ExprKind<'_>) -> Option<Ty<'tcx>> {
|
||||||
if let ExprKind::Match(_, arms, MatchSource::TryDesugar) = expr {
|
if let ExprKind::Match(_, arms, MatchSource::TryDesugar) = expr {
|
||||||
for arm in arms.iter() {
|
for arm in *arms {
|
||||||
if let ExprKind::Ret(Some(ret)) = arm.body.kind {
|
if let ExprKind::Ret(Some(ret)) = arm.body.kind {
|
||||||
return Some(cx.typeck_results().expr_ty(ret));
|
return Some(cx.typeck_results().expr_ty(ret));
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,7 @@ pub(super) fn check(cx: &EarlyContext<'_>, lit_span: Span, suffix: &str, lit_sni
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let mut seen = (false, false);
|
let mut seen = (false, false);
|
||||||
for ch in lit_snip.as_bytes()[2..=maybe_last_sep_idx].iter() {
|
for ch in &lit_snip.as_bytes()[2..=maybe_last_sep_idx] {
|
||||||
match ch {
|
match ch {
|
||||||
b'a'..=b'f' => seen.0 = true,
|
b'a'..=b'f' => seen.0 = true,
|
||||||
b'A'..=b'F' => seen.1 = true,
|
b'A'..=b'F' => seen.1 = true,
|
||||||
|
|
|
@ -59,7 +59,7 @@ impl<'tcx> LateLintPass<'tcx> for TypeParamMismatch {
|
||||||
then {
|
then {
|
||||||
// get the name and span of the generic parameters in the Impl
|
// get the name and span of the generic parameters in the Impl
|
||||||
let mut impl_params = Vec::new();
|
let mut impl_params = Vec::new();
|
||||||
for p in generic_args.args.iter() {
|
for p in generic_args.args {
|
||||||
match p {
|
match p {
|
||||||
GenericArg::Type(Ty {kind: TyKind::Path(QPath::Resolved(_, path)), ..}) =>
|
GenericArg::Type(Ty {kind: TyKind::Path(QPath::Resolved(_, path)), ..}) =>
|
||||||
impl_params.push((path.segments[0].ident.to_string(), path.span)),
|
impl_params.push((path.segments[0].ident.to_string(), path.span)),
|
||||||
|
|
|
@ -292,7 +292,7 @@ fn check_final_expr<'tcx>(
|
||||||
// (except for unit type functions) so we don't match it
|
// (except for unit type functions) so we don't match it
|
||||||
ExprKind::Match(_, arms, MatchSource::Normal) => {
|
ExprKind::Match(_, arms, MatchSource::Normal) => {
|
||||||
let match_ty = cx.typeck_results().expr_ty(peeled_drop_expr);
|
let match_ty = cx.typeck_results().expr_ty(peeled_drop_expr);
|
||||||
for arm in arms.iter() {
|
for arm in *arms {
|
||||||
check_final_expr(cx, arm.body, semi_spans.clone(), RetReplacement::Unit, Some(match_ty));
|
check_final_expr(cx, arm.body, semi_spans.clone(), RetReplacement::Unit, Some(match_ty));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -333,7 +333,7 @@ impl<'cx, 'sdt, 'tcx> SigDropChecker<'cx, 'sdt, 'tcx> {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for generic_arg in b.iter() {
|
for generic_arg in *b {
|
||||||
if let GenericArgKind::Type(ty) = generic_arg.unpack() {
|
if let GenericArgKind::Type(ty) = generic_arg.unpack() {
|
||||||
if self.has_sig_drop_attr(ty) {
|
if self.has_sig_drop_attr(ty) {
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -139,7 +139,7 @@ impl<'tcx> LateLintPass<'tcx> for TraitBounds {
|
||||||
) = cx.tcx.hir().get_if_local(*def_id);
|
) = cx.tcx.hir().get_if_local(*def_id);
|
||||||
then {
|
then {
|
||||||
if self_bounds_map.is_empty() {
|
if self_bounds_map.is_empty() {
|
||||||
for bound in self_bounds.iter() {
|
for bound in *self_bounds {
|
||||||
let Some((self_res, self_segments, _)) = get_trait_info_from_bound(bound) else { continue };
|
let Some((self_res, self_segments, _)) = get_trait_info_from_bound(bound) else { continue };
|
||||||
self_bounds_map.insert(self_res, self_segments);
|
self_bounds_map.insert(self_res, self_segments);
|
||||||
}
|
}
|
||||||
|
@ -184,7 +184,7 @@ impl<'tcx> LateLintPass<'tcx> for TraitBounds {
|
||||||
|
|
||||||
// Iterate the bounds and add them to our seen hash
|
// Iterate the bounds and add them to our seen hash
|
||||||
// If we haven't yet seen it, add it to the fixed traits
|
// If we haven't yet seen it, add it to the fixed traits
|
||||||
for bound in bounds.iter() {
|
for bound in bounds {
|
||||||
let Some(def_id) = bound.trait_ref.trait_def_id() else { continue; };
|
let Some(def_id) = bound.trait_ref.trait_def_id() else { continue; };
|
||||||
|
|
||||||
let new_trait = seen_def_ids.insert(def_id);
|
let new_trait = seen_def_ids.insert(def_id);
|
||||||
|
|
|
@ -75,7 +75,7 @@ impl<'tcx> LateLintPass<'tcx> for InterningDefinedSymbol {
|
||||||
|
|
||||||
for &module in &[&paths::KW_MODULE, &paths::SYM_MODULE] {
|
for &module in &[&paths::KW_MODULE, &paths::SYM_MODULE] {
|
||||||
for def_id in def_path_def_ids(cx, module) {
|
for def_id in def_path_def_ids(cx, module) {
|
||||||
for item in cx.tcx.module_children(def_id).iter() {
|
for item in cx.tcx.module_children(def_id) {
|
||||||
if_chain! {
|
if_chain! {
|
||||||
if let Res::Def(DefKind::Const, item_def_id) = item.res;
|
if let Res::Def(DefKind::Const, item_def_id) = item.res;
|
||||||
let ty = cx.tcx.type_of(item_def_id).subst_identity();
|
let ty = cx.tcx.type_of(item_def_id).subst_identity();
|
||||||
|
|
|
@ -61,7 +61,7 @@ pub fn is_min_const_fn<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, msrv: &Msrv)
|
||||||
body.local_decls.iter().next().unwrap().source_info.span,
|
body.local_decls.iter().next().unwrap().source_info.span,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
for bb in body.basic_blocks.iter() {
|
for bb in &*body.basic_blocks {
|
||||||
check_terminator(tcx, body, bb.terminator(), msrv)?;
|
check_terminator(tcx, body, bb.terminator(), msrv)?;
|
||||||
for stmt in &bb.statements {
|
for stmt in &bb.statements {
|
||||||
check_statement(tcx, body, def_id, stmt)?;
|
check_statement(tcx, body, def_id, stmt)?;
|
||||||
|
@ -89,7 +89,7 @@ fn check_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, span: Span) -> McfResult {
|
||||||
return Err((span, "function pointers in const fn are unstable".into()));
|
return Err((span, "function pointers in const fn are unstable".into()));
|
||||||
},
|
},
|
||||||
ty::Dynamic(preds, _, _) => {
|
ty::Dynamic(preds, _, _) => {
|
||||||
for pred in preds.iter() {
|
for pred in *preds {
|
||||||
match pred.skip_binder() {
|
match pred.skip_binder() {
|
||||||
ty::ExistentialPredicate::AutoTrait(_) | ty::ExistentialPredicate::Projection(_) => {
|
ty::ExistentialPredicate::AutoTrait(_) | ty::ExistentialPredicate::Projection(_) => {
|
||||||
return Err((
|
return Err((
|
||||||
|
|
|
@ -277,7 +277,7 @@ pub fn is_must_use_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
|
||||||
false
|
false
|
||||||
},
|
},
|
||||||
ty::Dynamic(binder, _, _) => {
|
ty::Dynamic(binder, _, _) => {
|
||||||
for predicate in binder.iter() {
|
for predicate in *binder {
|
||||||
if let ty::ExistentialPredicate::Trait(ref trait_ref) = predicate.skip_binder() {
|
if let ty::ExistentialPredicate::Trait(ref trait_ref) = predicate.skip_binder() {
|
||||||
if cx.tcx.has_attr(trait_ref.def_id, sym::must_use) {
|
if cx.tcx.has_attr(trait_ref.def_id, sym::must_use) {
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -82,7 +82,7 @@ pub struct ParamBindingIdCollector {
|
||||||
impl<'tcx> ParamBindingIdCollector {
|
impl<'tcx> ParamBindingIdCollector {
|
||||||
fn collect_binding_hir_ids(body: &'tcx hir::Body<'tcx>) -> Vec<hir::HirId> {
|
fn collect_binding_hir_ids(body: &'tcx hir::Body<'tcx>) -> Vec<hir::HirId> {
|
||||||
let mut hir_ids: Vec<hir::HirId> = Vec::new();
|
let mut hir_ids: Vec<hir::HirId> = Vec::new();
|
||||||
for param in body.params.iter() {
|
for param in body.params {
|
||||||
let mut finder = ParamBindingIdCollector {
|
let mut finder = ParamBindingIdCollector {
|
||||||
binding_hir_ids: Vec::new(),
|
binding_hir_ids: Vec::new(),
|
||||||
};
|
};
|
||||||
|
|
|
@ -474,7 +474,7 @@ fn read_crates(toml_path: &Path) -> (Vec<CrateSource>, RecursiveOptions) {
|
||||||
});
|
});
|
||||||
} else if let Some(ref versions) = tk.versions {
|
} else if let Some(ref versions) = tk.versions {
|
||||||
// if we have multiple versions, save each one
|
// if we have multiple versions, save each one
|
||||||
for ver in versions.iter() {
|
for ver in versions {
|
||||||
crate_sources.push(CrateSource::CratesIo {
|
crate_sources.push(CrateSource::CratesIo {
|
||||||
name: tk.name.clone(),
|
name: tk.name.clone(),
|
||||||
version: ver.to_string(),
|
version: ver.to_string(),
|
||||||
|
|
|
@ -17,6 +17,13 @@ fn main() {
|
||||||
for _ in &vec {}
|
for _ in &vec {}
|
||||||
for _ in &mut vec {}
|
for _ in &mut vec {}
|
||||||
|
|
||||||
|
let rvec = &vec;
|
||||||
|
for _ in rvec {}
|
||||||
|
|
||||||
|
let rmvec = &mut vec;
|
||||||
|
for _ in &*rmvec {}
|
||||||
|
for _ in &mut *rmvec {}
|
||||||
|
|
||||||
for _ in &vec {} // these are fine
|
for _ in &vec {} // these are fine
|
||||||
for _ in &mut vec {} // these are fine
|
for _ in &mut vec {} // these are fine
|
||||||
|
|
||||||
|
@ -29,9 +36,13 @@ fn main() {
|
||||||
|
|
||||||
let ll: LinkedList<()> = LinkedList::new();
|
let ll: LinkedList<()> = LinkedList::new();
|
||||||
for _ in &ll {}
|
for _ in &ll {}
|
||||||
|
let rll = ≪
|
||||||
|
for _ in rll {}
|
||||||
|
|
||||||
let vd: VecDeque<()> = VecDeque::new();
|
let vd: VecDeque<()> = VecDeque::new();
|
||||||
for _ in &vd {}
|
for _ in &vd {}
|
||||||
|
let rvd = &vd;
|
||||||
|
for _ in rvd {}
|
||||||
|
|
||||||
let bh: BinaryHeap<()> = BinaryHeap::new();
|
let bh: BinaryHeap<()> = BinaryHeap::new();
|
||||||
for _ in &bh {}
|
for _ in &bh {}
|
||||||
|
@ -137,4 +148,7 @@ fn main() {
|
||||||
let mut x = CustomType;
|
let mut x = CustomType;
|
||||||
for _ in &x {}
|
for _ in &x {}
|
||||||
for _ in &mut x {}
|
for _ in &mut x {}
|
||||||
|
|
||||||
|
let r = &x;
|
||||||
|
for _ in r {}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,13 @@ fn main() {
|
||||||
for _ in vec.iter() {}
|
for _ in vec.iter() {}
|
||||||
for _ in vec.iter_mut() {}
|
for _ in vec.iter_mut() {}
|
||||||
|
|
||||||
|
let rvec = &vec;
|
||||||
|
for _ in rvec.iter() {}
|
||||||
|
|
||||||
|
let rmvec = &mut vec;
|
||||||
|
for _ in rmvec.iter() {}
|
||||||
|
for _ in rmvec.iter_mut() {}
|
||||||
|
|
||||||
for _ in &vec {} // these are fine
|
for _ in &vec {} // these are fine
|
||||||
for _ in &mut vec {} // these are fine
|
for _ in &mut vec {} // these are fine
|
||||||
|
|
||||||
|
@ -29,9 +36,13 @@ fn main() {
|
||||||
|
|
||||||
let ll: LinkedList<()> = LinkedList::new();
|
let ll: LinkedList<()> = LinkedList::new();
|
||||||
for _ in ll.iter() {}
|
for _ in ll.iter() {}
|
||||||
|
let rll = ≪
|
||||||
|
for _ in rll.iter() {}
|
||||||
|
|
||||||
let vd: VecDeque<()> = VecDeque::new();
|
let vd: VecDeque<()> = VecDeque::new();
|
||||||
for _ in vd.iter() {}
|
for _ in vd.iter() {}
|
||||||
|
let rvd = &vd;
|
||||||
|
for _ in rvd.iter() {}
|
||||||
|
|
||||||
let bh: BinaryHeap<()> = BinaryHeap::new();
|
let bh: BinaryHeap<()> = BinaryHeap::new();
|
||||||
for _ in bh.iter() {}
|
for _ in bh.iter() {}
|
||||||
|
@ -137,4 +148,7 @@ fn main() {
|
||||||
let mut x = CustomType;
|
let mut x = CustomType;
|
||||||
for _ in x.iter() {}
|
for _ in x.iter() {}
|
||||||
for _ in x.iter_mut() {}
|
for _ in x.iter_mut() {}
|
||||||
|
|
||||||
|
let r = &x;
|
||||||
|
for _ in r.iter() {}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,19 +17,37 @@ LL | for _ in vec.iter_mut() {}
|
||||||
| ^^^^^^^^^^^^^^ help: to write this more concisely, try: `&mut vec`
|
| ^^^^^^^^^^^^^^ help: to write this more concisely, try: `&mut vec`
|
||||||
|
|
||||||
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
||||||
--> $DIR/explicit_iter_loop.rs:23:14
|
--> $DIR/explicit_iter_loop.rs:21:14
|
||||||
|
|
|
||||||
|
LL | for _ in rvec.iter() {}
|
||||||
|
| ^^^^^^^^^^^ help: to write this more concisely, try: `rvec`
|
||||||
|
|
||||||
|
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
||||||
|
--> $DIR/explicit_iter_loop.rs:24:14
|
||||||
|
|
|
||||||
|
LL | for _ in rmvec.iter() {}
|
||||||
|
| ^^^^^^^^^^^^ help: to write this more concisely, try: `&*rmvec`
|
||||||
|
|
||||||
|
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
||||||
|
--> $DIR/explicit_iter_loop.rs:25:14
|
||||||
|
|
|
||||||
|
LL | for _ in rmvec.iter_mut() {}
|
||||||
|
| ^^^^^^^^^^^^^^^^ help: to write this more concisely, try: `&mut *rmvec`
|
||||||
|
|
||||||
|
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
||||||
|
--> $DIR/explicit_iter_loop.rs:30:14
|
||||||
|
|
|
|
||||||
LL | for _ in [1, 2, 3].iter() {}
|
LL | for _ in [1, 2, 3].iter() {}
|
||||||
| ^^^^^^^^^^^^^^^^ help: to write this more concisely, try: `&[1, 2, 3]`
|
| ^^^^^^^^^^^^^^^^ help: to write this more concisely, try: `&[1, 2, 3]`
|
||||||
|
|
||||||
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
||||||
--> $DIR/explicit_iter_loop.rs:25:14
|
--> $DIR/explicit_iter_loop.rs:32:14
|
||||||
|
|
|
|
||||||
LL | for _ in (&mut [1, 2, 3]).iter() {}
|
LL | for _ in (&mut [1, 2, 3]).iter() {}
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^ help: to write this more concisely, try: `&*(&mut [1, 2, 3])`
|
| ^^^^^^^^^^^^^^^^^^^^^^^ help: to write this more concisely, try: `&*(&mut [1, 2, 3])`
|
||||||
|
|
||||||
error: the method `iter` doesn't need a mutable reference
|
error: the method `iter` doesn't need a mutable reference
|
||||||
--> $DIR/explicit_iter_loop.rs:25:14
|
--> $DIR/explicit_iter_loop.rs:32:14
|
||||||
|
|
|
|
||||||
LL | for _ in (&mut [1, 2, 3]).iter() {}
|
LL | for _ in (&mut [1, 2, 3]).iter() {}
|
||||||
| ^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^
|
||||||
|
@ -37,70 +55,88 @@ LL | for _ in (&mut [1, 2, 3]).iter() {}
|
||||||
= note: `-D clippy::unnecessary-mut-passed` implied by `-D warnings`
|
= note: `-D clippy::unnecessary-mut-passed` implied by `-D warnings`
|
||||||
|
|
||||||
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
||||||
--> $DIR/explicit_iter_loop.rs:27:14
|
--> $DIR/explicit_iter_loop.rs:34:14
|
||||||
|
|
|
|
||||||
LL | for _ in [0; 32].iter() {}
|
LL | for _ in [0; 32].iter() {}
|
||||||
| ^^^^^^^^^^^^^^ help: to write this more concisely, try: `&[0; 32]`
|
| ^^^^^^^^^^^^^^ help: to write this more concisely, try: `&[0; 32]`
|
||||||
|
|
||||||
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
||||||
--> $DIR/explicit_iter_loop.rs:28:14
|
--> $DIR/explicit_iter_loop.rs:35:14
|
||||||
|
|
|
|
||||||
LL | for _ in [0; 33].iter() {}
|
LL | for _ in [0; 33].iter() {}
|
||||||
| ^^^^^^^^^^^^^^ help: to write this more concisely, try: `&[0; 33]`
|
| ^^^^^^^^^^^^^^ help: to write this more concisely, try: `&[0; 33]`
|
||||||
|
|
||||||
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
||||||
--> $DIR/explicit_iter_loop.rs:31:14
|
--> $DIR/explicit_iter_loop.rs:38:14
|
||||||
|
|
|
|
||||||
LL | for _ in ll.iter() {}
|
LL | for _ in ll.iter() {}
|
||||||
| ^^^^^^^^^ help: to write this more concisely, try: `&ll`
|
| ^^^^^^^^^ help: to write this more concisely, try: `&ll`
|
||||||
|
|
||||||
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
||||||
--> $DIR/explicit_iter_loop.rs:34:14
|
--> $DIR/explicit_iter_loop.rs:40:14
|
||||||
|
|
|
||||||
|
LL | for _ in rll.iter() {}
|
||||||
|
| ^^^^^^^^^^ help: to write this more concisely, try: `rll`
|
||||||
|
|
||||||
|
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
||||||
|
--> $DIR/explicit_iter_loop.rs:43:14
|
||||||
|
|
|
|
||||||
LL | for _ in vd.iter() {}
|
LL | for _ in vd.iter() {}
|
||||||
| ^^^^^^^^^ help: to write this more concisely, try: `&vd`
|
| ^^^^^^^^^ help: to write this more concisely, try: `&vd`
|
||||||
|
|
||||||
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
||||||
--> $DIR/explicit_iter_loop.rs:37:14
|
--> $DIR/explicit_iter_loop.rs:45:14
|
||||||
|
|
|
||||||
|
LL | for _ in rvd.iter() {}
|
||||||
|
| ^^^^^^^^^^ help: to write this more concisely, try: `rvd`
|
||||||
|
|
||||||
|
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
||||||
|
--> $DIR/explicit_iter_loop.rs:48:14
|
||||||
|
|
|
|
||||||
LL | for _ in bh.iter() {}
|
LL | for _ in bh.iter() {}
|
||||||
| ^^^^^^^^^ help: to write this more concisely, try: `&bh`
|
| ^^^^^^^^^ help: to write this more concisely, try: `&bh`
|
||||||
|
|
||||||
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
||||||
--> $DIR/explicit_iter_loop.rs:40:14
|
--> $DIR/explicit_iter_loop.rs:51:14
|
||||||
|
|
|
|
||||||
LL | for _ in hm.iter() {}
|
LL | for _ in hm.iter() {}
|
||||||
| ^^^^^^^^^ help: to write this more concisely, try: `&hm`
|
| ^^^^^^^^^ help: to write this more concisely, try: `&hm`
|
||||||
|
|
||||||
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
||||||
--> $DIR/explicit_iter_loop.rs:43:14
|
--> $DIR/explicit_iter_loop.rs:54:14
|
||||||
|
|
|
|
||||||
LL | for _ in bt.iter() {}
|
LL | for _ in bt.iter() {}
|
||||||
| ^^^^^^^^^ help: to write this more concisely, try: `&bt`
|
| ^^^^^^^^^ help: to write this more concisely, try: `&bt`
|
||||||
|
|
||||||
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
||||||
--> $DIR/explicit_iter_loop.rs:46:14
|
--> $DIR/explicit_iter_loop.rs:57:14
|
||||||
|
|
|
|
||||||
LL | for _ in hs.iter() {}
|
LL | for _ in hs.iter() {}
|
||||||
| ^^^^^^^^^ help: to write this more concisely, try: `&hs`
|
| ^^^^^^^^^ help: to write this more concisely, try: `&hs`
|
||||||
|
|
||||||
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
||||||
--> $DIR/explicit_iter_loop.rs:49:14
|
--> $DIR/explicit_iter_loop.rs:60:14
|
||||||
|
|
|
|
||||||
LL | for _ in bs.iter() {}
|
LL | for _ in bs.iter() {}
|
||||||
| ^^^^^^^^^ help: to write this more concisely, try: `&bs`
|
| ^^^^^^^^^ help: to write this more concisely, try: `&bs`
|
||||||
|
|
||||||
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
||||||
--> $DIR/explicit_iter_loop.rs:138:14
|
--> $DIR/explicit_iter_loop.rs:149:14
|
||||||
|
|
|
|
||||||
LL | for _ in x.iter() {}
|
LL | for _ in x.iter() {}
|
||||||
| ^^^^^^^^ help: to write this more concisely, try: `&x`
|
| ^^^^^^^^ help: to write this more concisely, try: `&x`
|
||||||
|
|
||||||
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
||||||
--> $DIR/explicit_iter_loop.rs:139:14
|
--> $DIR/explicit_iter_loop.rs:150:14
|
||||||
|
|
|
|
||||||
LL | for _ in x.iter_mut() {}
|
LL | for _ in x.iter_mut() {}
|
||||||
| ^^^^^^^^^^^^ help: to write this more concisely, try: `&mut x`
|
| ^^^^^^^^^^^^ help: to write this more concisely, try: `&mut x`
|
||||||
|
|
||||||
error: aborting due to 16 previous errors
|
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
||||||
|
--> $DIR/explicit_iter_loop.rs:153:14
|
||||||
|
|
|
||||||
|
LL | for _ in r.iter() {}
|
||||||
|
| ^^^^^^^^ help: to write this more concisely, try: `r`
|
||||||
|
|
||||||
|
error: aborting due to 22 previous errors
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue