From 9e4ec2acc782dea54c71430b5de5281cc77eb4ad Mon Sep 17 00:00:00 2001 From: Marijn Schouten Date: Tue, 28 Oct 2025 13:27:21 +0000 Subject: [PATCH] clippy fixes and code simplification --- .../src/fn_ctxt/adjust_fulfillment_errors.rs | 34 +++++++++--------- .../src/fn_ctxt/arg_matrix.rs | 36 +++++++------------ .../rustc_hir_typeck/src/fn_ctxt/checks.rs | 21 ++++------- .../src/fn_ctxt/suggestions.rs | 12 +++---- .../rustc_hir_typeck/src/gather_locals.rs | 2 +- compiler/rustc_hir_typeck/src/loops.rs | 17 ++++----- compiler/rustc_index/src/bit_set.rs | 2 +- .../src/impossible_predicates.rs | 8 ++--- compiler/rustc_mir_transform/src/inline.rs | 2 +- 9 files changed, 55 insertions(+), 79 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/adjust_fulfillment_errors.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/adjust_fulfillment_errors.rs index 837328e379ca3..854202c312705 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/adjust_fulfillment_errors.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/adjust_fulfillment_errors.rs @@ -94,20 +94,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let find_param_matching = |matches: &dyn Fn(ParamTerm) -> bool| { predicate_args.iter().find_map(|arg| { - arg.walk().find_map(|arg| { - if let ty::GenericArgKind::Type(ty) = arg.kind() - && let ty::Param(param_ty) = *ty.kind() - && matches(ParamTerm::Ty(param_ty)) - { - Some(arg) - } else if let ty::GenericArgKind::Const(ct) = arg.kind() - && let ty::ConstKind::Param(param_ct) = ct.kind() - && matches(ParamTerm::Const(param_ct)) + arg.walk().find(|arg| match arg.kind() { + ty::GenericArgKind::Type(ty) if let ty::Param(param_ty) = ty.kind() => { + matches(ParamTerm::Ty(*param_ty)) + } + ty::GenericArgKind::Const(ct) + if let ty::ConstKind::Param(param_ct) = ct.kind() => { - Some(arg) - } else { - None + matches(ParamTerm::Const(param_ct)) } + _ => false, }) }) }; @@ -162,7 +158,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { .into_iter() .flatten() { - if self.point_at_path_if_possible(error, def_id, param, &qpath) { + if self.point_at_path_if_possible(error, def_id, param, qpath) { return true; } } @@ -194,7 +190,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { args, ) => { if let Some(param) = predicate_self_type_to_point_at - && self.point_at_path_if_possible(error, callee_def_id, param, &qpath) + && self.point_at_path_if_possible(error, callee_def_id, param, qpath) { return true; } @@ -225,7 +221,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { .into_iter() .flatten() { - if self.point_at_path_if_possible(error, callee_def_id, param, &qpath) { + if self.point_at_path_if_possible(error, callee_def_id, param, qpath) { return true; } } @@ -543,10 +539,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } /// - `blame_specific_*` means that the function will recursively traverse the expression, - /// looking for the most-specific-possible span to blame. + /// looking for the most-specific-possible span to blame. /// /// - `point_at_*` means that the function will only go "one level", pointing at the specific - /// expression mentioned. + /// expression mentioned. /// /// `blame_specific_arg_if_possible` will find the most-specific expression anywhere inside /// the provided function call expression, and mark it as responsible for the fulfillment @@ -609,6 +605,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { * - want `Vec: Copy` * - because `Option>: Copy` needs `Vec: Copy` because `impl Copy for Option` * - because `(Option, bool)` needs `Option>: Copy` because `impl Copy for (A, B)` + * * then if you pass in `(Some(vec![1, 2, 3]), false)`, this helper `point_at_specific_expr_if_possible` * will find the expression `vec![1, 2, 3]` as the "most blameable" reason for this missing constraint. * @@ -749,6 +746,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { /// - expr: `(Some(vec![1, 2, 3]), false)` /// - param: `T` /// - in_ty: `(Option, bool)` + /// /// we would drill until we arrive at `vec![1, 2, 3]`. /// /// If successful, we return `Ok(refined_expr)`. If unsuccessful, we return `Err(partially_refined_expr`), @@ -1016,7 +1014,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { .variant_with_id(variant_def_id) .fields .iter() - .map(|field| field.ty(self.tcx, *in_ty_adt_generic_args)) + .map(|field| field.ty(self.tcx, in_ty_adt_generic_args)) .enumerate() .filter(|(_index, field_type)| find_param_in_ty((*field_type).into(), param)), ) else { diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/arg_matrix.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/arg_matrix.rs index f6298adf2ebb7..16f0a7dbc6f33 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/arg_matrix.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/arg_matrix.rs @@ -174,7 +174,7 @@ impl<'tcx> ArgMatrix<'tcx> { return Some(Issue::Missing(next_unmatched_idx)); } // If we eliminate the last column, any left-over inputs are extra - if mat[i].len() == 0 { + if mat[i].is_empty() { return Some(Issue::Extra(next_unmatched_idx)); } @@ -187,28 +187,18 @@ impl<'tcx> ArgMatrix<'tcx> { continue; } - let mut useless = true; - let mut unsatisfiable = true; - if is_arg { - for j in 0..ii.len() { - // If we find at least one input this argument could satisfy - // this argument isn't unsatisfiable - if matches!(mat[j][i], Compatibility::Compatible) { - unsatisfiable = false; - break; - } - } - } - if is_input { - for j in 0..ai.len() { - // If we find at least one argument that could satisfy this input - // this input isn't useless - if matches!(mat[i][j], Compatibility::Compatible) { - useless = false; - break; - } - } - } + // If this argument can satisfy some input, then this argument is satisfiable + let unsatisfiable = if is_arg { + !mat.iter().take(ii.len()).any(|c| matches!(c[i], Compatibility::Compatible)) + } else { + true + }; + // If this input can be satisfied by some argument, then this input is useful + let useless = if is_input { + !mat[i].iter().take(ai.len()).any(|c| matches!(c, Compatibility::Compatible)) + } else { + true + }; match (is_input, is_arg, useless, unsatisfiable) { // If an argument is unsatisfied, and the input in its position is useless diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs index 0cbdd86768a67..127f2c676391d 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs @@ -777,10 +777,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } _ => bug!("unexpected type: {:?}", ty.normalized), }, - Res::Def( - DefKind::Struct | DefKind::Union | DefKind::TyAlias { .. } | DefKind::AssocTy, - _, - ) + Res::Def(DefKind::Struct | DefKind::Union | DefKind::TyAlias | DefKind::AssocTy, _) | Res::SelfTyParam { .. } | Res::SelfTyAlias { .. } => match ty.normalized.ty_adt_def() { Some(adt) if !adt.is_enum() => { @@ -868,7 +865,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let decl_ty = self.local_ty(decl.span, decl.hir_id); // Type check the initializer. - if let Some(ref init) = decl.init { + if let Some(init) = decl.init { let init_ty = self.check_decl_initializer(decl.hir_id, decl.pat, init); self.overwrite_local_ty_if_err(decl.hir_id, decl.pat, init_ty); } @@ -932,7 +929,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } // Ignore for now. hir::StmtKind::Item(_) => {} - hir::StmtKind::Expr(ref expr) => { + hir::StmtKind::Expr(expr) => { // Check with expected type of `()`. self.check_expr_has_type_or_error(expr, self.tcx.types.unit, |err| { if self.is_next_stmt_expr_continuation(stmt.hir_id) @@ -1766,10 +1763,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let params = params.get(is_method as usize..params.len() - sig.decl.c_variadic as usize)?; debug_assert_eq!(params.len(), fn_inputs.len()); - Some(( - fn_inputs.zip(params.iter().map(|param| FnParam::Param(param))).collect(), - generics, - )) + Some((fn_inputs.zip(params.iter().map(FnParam::Param)).collect(), generics)) } (None, Some(params)) => { let params = @@ -2632,7 +2626,7 @@ impl<'a, 'b, 'tcx> FnCallDiagCtxt<'a, 'b, 'tcx> { suggestions: Vec<(Span, String)>, suggestion_text: SuggestionText, ) -> Option { - let suggestion_text = match suggestion_text { + match suggestion_text { SuggestionText::None => None, SuggestionText::Provide(plural) => { Some(format!("provide the argument{}", if plural { "s" } else { "" })) @@ -2648,8 +2642,7 @@ impl<'a, 'b, 'tcx> FnCallDiagCtxt<'a, 'b, 'tcx> { SuggestionText::Swap => Some("swap these arguments".to_string()), SuggestionText::Reorder => Some("reorder these arguments".to_string()), SuggestionText::DidYouMean => Some("did you mean".to_string()), - }; - suggestion_text + } } fn arguments_formatting(&self, suggestion_span: Span) -> ArgumentsFormatting { @@ -2947,7 +2940,7 @@ impl<'a, 'b, 'tcx> ArgsCtxt<'a, 'b, 'tcx> { .fn_ctxt .typeck_results .borrow() - .expr_ty_adjusted_opt(*expr) + .expr_ty_adjusted_opt(expr) .unwrap_or_else(|| Ty::new_misc_error(self.call_ctxt.fn_ctxt.tcx)); ( self.call_ctxt.fn_ctxt.resolve_vars_if_possible(ty), diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index 28bd3e7e8d5bb..1d16c3af7fb75 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -1132,7 +1132,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { }) .collect::>(); - if all_matching_bounds_strs.len() == 0 { + if all_matching_bounds_strs.is_empty() { return; } @@ -1336,7 +1336,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { expected_ty = *inner_expected_ty; } (hir::ExprKind::Block(blk, _), _, _) => { - self.suggest_block_to_brackets(diag, *blk, expr_ty, expected_ty); + self.suggest_block_to_brackets(diag, blk, expr_ty, expected_ty); break true; } _ => break false, @@ -1463,7 +1463,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { { let span = expr .span - .find_ancestor_not_from_extern_macro(&self.tcx.sess.source_map()) + .find_ancestor_not_from_extern_macro(self.tcx.sess.source_map()) .unwrap_or(expr.span); let mut sugg = if self.precedence(expr) >= ExprPrecedence::Unambiguous { @@ -2144,7 +2144,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let span = expr .span - .find_ancestor_not_from_extern_macro(&self.tcx.sess.source_map()) + .find_ancestor_not_from_extern_macro(self.tcx.sess.source_map()) .unwrap_or(expr.span); err.span_suggestion_verbose(span.shrink_to_hi(), msg, sugg, Applicability::HasPlaceholders); true @@ -2257,7 +2257,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let src_map = tcx.sess.source_map(); let suggestion = if src_map.is_multiline(expr.span) { - let indentation = src_map.indentation_before(span).unwrap_or_else(String::new); + let indentation = src_map.indentation_before(span).unwrap_or_default(); format!("\n{indentation}/* {suggestion} */") } else { // If the entire expr is on a single line @@ -2289,7 +2289,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // Check if `expr` is contained in array of two elements if let hir::Node::Expr(array_expr) = self.tcx.parent_hir_node(expr.hir_id) && let hir::ExprKind::Array(elements) = array_expr.kind - && let [first, second] = &elements[..] + && let [first, second] = elements && second.hir_id == expr.hir_id { // Span between the two elements of the array diff --git a/compiler/rustc_hir_typeck/src/gather_locals.rs b/compiler/rustc_hir_typeck/src/gather_locals.rs index f1d6476a0f363..85c8400f227bd 100644 --- a/compiler/rustc_hir_typeck/src/gather_locals.rs +++ b/compiler/rustc_hir_typeck/src/gather_locals.rs @@ -135,7 +135,7 @@ impl<'a, 'tcx> GatherLocalsVisitor<'a, 'tcx> { /// again during type checking by querying [`FnCtxt::local_ty`] for the same hir_id. fn declare(&mut self, decl: Declaration<'tcx>) { let local_ty = match decl.ty { - Some(ref ty) => { + Some(ty) => { let o_ty = self.fcx.lower_ty(ty); let c_ty = self.fcx.infcx.canonicalize_user_type_annotation( diff --git a/compiler/rustc_hir_typeck/src/loops.rs b/compiler/rustc_hir_typeck/src/loops.rs index acfa5c473aacc..799e82ec13b80 100644 --- a/compiler/rustc_hir_typeck/src/loops.rs +++ b/compiler/rustc_hir_typeck/src/loops.rs @@ -147,7 +147,7 @@ impl<'hir> Visitor<'hir> for CheckLoopVisitor<'hir> { } } } - hir::ExprKind::Loop(ref b, _, source, _) => { + hir::ExprKind::Loop(b, _, source, _) => { let cx = match self.is_loop_match(e, b) { Some(labeled_block) => LoopMatch { labeled_block }, None => Loop(source), @@ -155,9 +155,7 @@ impl<'hir> Visitor<'hir> for CheckLoopVisitor<'hir> { self.with_context(cx, |v| v.visit_block(b)); } - hir::ExprKind::Closure(&hir::Closure { - ref fn_decl, body, fn_decl_span, kind, .. - }) => { + hir::ExprKind::Closure(&hir::Closure { fn_decl, body, fn_decl_span, kind, .. }) => { let cx = match kind { hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(kind, source)) => { Coroutine { coroutine_span: fn_decl_span, kind, source } @@ -167,16 +165,16 @@ impl<'hir> Visitor<'hir> for CheckLoopVisitor<'hir> { self.visit_fn_decl(fn_decl); self.with_context(cx, |v| v.visit_nested_body(body)); } - hir::ExprKind::Block(ref b, Some(_label)) => { + hir::ExprKind::Block(b, Some(_label)) => { self.with_context(LabeledBlock, |v| v.visit_block(b)); } - hir::ExprKind::Block(ref b, None) + hir::ExprKind::Block(b, None) if matches!(self.cx_stack.last(), Some(&Fn) | Some(&ConstBlock)) => { self.with_context(Normal, |v| v.visit_block(b)); } hir::ExprKind::Block( - ref b @ hir::Block { rules: hir::BlockCheckMode::DefaultBlock, .. }, + b @ hir::Block { rules: hir::BlockCheckMode::DefaultBlock, .. }, None, ) if matches!( self.cx_stack.last(), @@ -431,10 +429,7 @@ impl<'hir> CheckLoopVisitor<'hir> { // Accept either `state = expr` or `state = expr;`. let loop_body_expr = match body.stmts { - [] => match body.expr { - Some(expr) => expr, - None => return None, - }, + [] => body.expr?, [single] if body.expr.is_none() => match single.kind { hir::StmtKind::Expr(expr) | hir::StmtKind::Semi(expr) => expr, _ => return None, diff --git a/compiler/rustc_index/src/bit_set.rs b/compiler/rustc_index/src/bit_set.rs index 96e85fdb10a6e..a9bdf597e128f 100644 --- a/compiler/rustc_index/src/bit_set.rs +++ b/compiler/rustc_index/src/bit_set.rs @@ -1409,7 +1409,7 @@ impl BitMatrix { BitMatrix { num_rows, num_columns, - words: iter::repeat(&row.words).take(num_rows).flatten().cloned().collect(), + words: iter::repeat_n(&row.words, num_rows).flatten().cloned().collect(), marker: PhantomData, } } diff --git a/compiler/rustc_mir_transform/src/impossible_predicates.rs b/compiler/rustc_mir_transform/src/impossible_predicates.rs index 883ee32bdec91..71e4d5581ddc2 100644 --- a/compiler/rustc_mir_transform/src/impossible_predicates.rs +++ b/compiler/rustc_mir_transform/src/impossible_predicates.rs @@ -20,11 +20,11 @@ //! parameters, so this filtering serves two purposes: //! //! 1. We skip evaluating any predicates that we would -//! never be able prove are unsatisfiable (e.g. `` +//! never be able prove are unsatisfiable (e.g. `` //! 2. We avoid trying to normalize predicates involving generic -//! parameters (e.g. `::MyItem`). This can confuse -//! the normalization code (leading to cycle errors), since -//! it's usually never invoked in this way. +//! parameters (e.g. `::MyItem`). This can confuse +//! the normalization code (leading to cycle errors), since +//! it's usually never invoked in this way. use rustc_middle::mir::{Body, START_BLOCK, TerminatorKind}; use rustc_middle::ty::{TyCtxt, TypeFlags, TypeVisitableExt}; diff --git a/compiler/rustc_mir_transform/src/inline.rs b/compiler/rustc_mir_transform/src/inline.rs index dc6088849bf5e..7e1dd76f903d4 100644 --- a/compiler/rustc_mir_transform/src/inline.rs +++ b/compiler/rustc_mir_transform/src/inline.rs @@ -635,7 +635,7 @@ fn try_inlining<'tcx, I: Inliner<'tcx>>( // Normally, this shouldn't be required, but trait normalization failure can create a // validation ICE. - if !validate_types(tcx, inliner.typing_env(), &callee_body, &caller_body).is_empty() { + if !validate_types(tcx, inliner.typing_env(), &callee_body, caller_body).is_empty() { debug!("failed to validate callee body"); return Err("implementation limitation -- callee body failed validation"); }