Skip to content

Commit a84d778

Browse files
committed
clippy fixes and code simplification
1 parent 8aab621 commit a84d778

File tree

9 files changed

+61
-85
lines changed

9 files changed

+61
-85
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/adjust_fulfillment_errors.rs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -94,20 +94,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
9494

9595
let find_param_matching = |matches: &dyn Fn(ParamTerm) -> bool| {
9696
predicate_args.iter().find_map(|arg| {
97-
arg.walk().find_map(|arg| {
98-
if let ty::GenericArgKind::Type(ty) = arg.kind()
99-
&& let ty::Param(param_ty) = *ty.kind()
100-
&& matches(ParamTerm::Ty(param_ty))
101-
{
102-
Some(arg)
103-
} else if let ty::GenericArgKind::Const(ct) = arg.kind()
104-
&& let ty::ConstKind::Param(param_ct) = ct.kind()
105-
&& matches(ParamTerm::Const(param_ct))
97+
arg.walk().find(|arg| match arg.kind() {
98+
ty::GenericArgKind::Type(ty) if let ty::Param(param_ty) = ty.kind() => {
99+
matches(ParamTerm::Ty(*param_ty))
100+
}
101+
ty::GenericArgKind::Const(ct)
102+
if let ty::ConstKind::Param(param_ct) = ct.kind() =>
106103
{
107-
Some(arg)
108-
} else {
109-
None
104+
matches(ParamTerm::Const(param_ct))
110105
}
106+
_ => false,
111107
})
112108
})
113109
};
@@ -162,7 +158,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
162158
.into_iter()
163159
.flatten()
164160
{
165-
if self.point_at_path_if_possible(error, def_id, param, &qpath) {
161+
if self.point_at_path_if_possible(error, def_id, param, qpath) {
166162
return true;
167163
}
168164
}
@@ -194,7 +190,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
194190
args,
195191
) => {
196192
if let Some(param) = predicate_self_type_to_point_at
197-
&& self.point_at_path_if_possible(error, callee_def_id, param, &qpath)
193+
&& self.point_at_path_if_possible(error, callee_def_id, param, qpath)
198194
{
199195
return true;
200196
}
@@ -225,7 +221,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
225221
.into_iter()
226222
.flatten()
227223
{
228-
if self.point_at_path_if_possible(error, callee_def_id, param, &qpath) {
224+
if self.point_at_path_if_possible(error, callee_def_id, param, qpath) {
229225
return true;
230226
}
231227
}
@@ -544,10 +540,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
544540
}
545541

546542
/// - `blame_specific_*` means that the function will recursively traverse the expression,
547-
/// looking for the most-specific-possible span to blame.
543+
/// looking for the most-specific-possible span to blame.
548544
///
549545
/// - `point_at_*` means that the function will only go "one level", pointing at the specific
550-
/// expression mentioned.
546+
/// expression mentioned.
551547
///
552548
/// `blame_specific_arg_if_possible` will find the most-specific expression anywhere inside
553549
/// the provided function call expression, and mark it as responsible for the fulfillment
@@ -607,9 +603,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
607603
/**
608604
* Recursively searches for the most-specific blameable expression.
609605
* For example, if you have a chain of constraints like:
610-
* - want `Vec<i32>: Copy`
611-
* - because `Option<Vec<i32>>: Copy` needs `Vec<i32>: Copy` because `impl <T: Copy> Copy for Option<T>`
612-
* - because `(Option<Vec<i32>, bool)` needs `Option<Vec<i32>>: Copy` because `impl <A: Copy, B: Copy> Copy for (A, B)`
606+
* - want `Vec<i32>: Copy`
607+
* - because `Option<Vec<i32>>: Copy` needs `Vec<i32>: Copy` because `impl <T: Copy> Copy for Option<T>`
608+
* - because `(Option<Vec<i32>, bool)` needs `Option<Vec<i32>>: Copy` because `impl <A: Copy, B: Copy> Copy for (A, B)`
609+
*
613610
* then if you pass in `(Some(vec![1, 2, 3]), false)`, this helper `point_at_specific_expr_if_possible`
614611
* will find the expression `vec![1, 2, 3]` as the "most blameable" reason for this missing constraint.
615612
*
@@ -747,9 +744,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
747744

748745
/// Drills into `expr` to arrive at the equivalent location of `find_generic_param` in `in_ty`.
749746
/// For example, given
750-
/// - expr: `(Some(vec![1, 2, 3]), false)`
751-
/// - param: `T`
752-
/// - in_ty: `(Option<Vec<T>, bool)`
747+
/// - expr: `(Some(vec![1, 2, 3]), false)`
748+
/// - param: `T`
749+
/// - in_ty: `(Option<Vec<T>, bool)`
750+
///
753751
/// we would drill until we arrive at `vec![1, 2, 3]`.
754752
///
755753
/// If successful, we return `Ok(refined_expr)`. If unsuccessful, we return `Err(partially_refined_expr`),
@@ -1017,7 +1015,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10171015
.variant_with_id(variant_def_id)
10181016
.fields
10191017
.iter()
1020-
.map(|field| field.ty(self.tcx, *in_ty_adt_generic_args))
1018+
.map(|field| field.ty(self.tcx, in_ty_adt_generic_args))
10211019
.enumerate()
10221020
.filter(|(_index, field_type)| find_param_in_ty((*field_type).into(), param)),
10231021
) else {

compiler/rustc_hir_typeck/src/fn_ctxt/arg_matrix.rs

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ impl<'tcx> ArgMatrix<'tcx> {
174174
return Some(Issue::Missing(next_unmatched_idx));
175175
}
176176
// If we eliminate the last column, any left-over inputs are extra
177-
if mat[i].len() == 0 {
177+
if mat[i].is_empty() {
178178
return Some(Issue::Extra(next_unmatched_idx));
179179
}
180180

@@ -187,28 +187,18 @@ impl<'tcx> ArgMatrix<'tcx> {
187187
continue;
188188
}
189189

190-
let mut useless = true;
191-
let mut unsatisfiable = true;
192-
if is_arg {
193-
for j in 0..ii.len() {
194-
// If we find at least one input this argument could satisfy
195-
// this argument isn't unsatisfiable
196-
if matches!(mat[j][i], Compatibility::Compatible) {
197-
unsatisfiable = false;
198-
break;
199-
}
200-
}
201-
}
202-
if is_input {
203-
for j in 0..ai.len() {
204-
// If we find at least one argument that could satisfy this input
205-
// this input isn't useless
206-
if matches!(mat[i][j], Compatibility::Compatible) {
207-
useless = false;
208-
break;
209-
}
210-
}
211-
}
190+
// If this argument can satisfy some input, then this argument is satisfiable
191+
let unsatisfiable = if is_arg {
192+
!mat.iter().take(ii.len()).any(|c| matches!(c[i], Compatibility::Compatible))
193+
} else {
194+
true
195+
};
196+
// If this input can be satisfied by some argument, then this input is useful
197+
let useless = if is_input {
198+
!mat[i].iter().take(ai.len()).any(|c| matches!(c, Compatibility::Compatible))
199+
} else {
200+
true
201+
};
212202

213203
match (is_input, is_arg, useless, unsatisfiable) {
214204
// If an argument is unsatisfied, and the input in its position is useless

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -780,10 +780,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
780780
}
781781
_ => bug!("unexpected type: {:?}", ty.normalized),
782782
},
783-
Res::Def(
784-
DefKind::Struct | DefKind::Union | DefKind::TyAlias { .. } | DefKind::AssocTy,
785-
_,
786-
)
783+
Res::Def(DefKind::Struct | DefKind::Union | DefKind::TyAlias | DefKind::AssocTy, _)
787784
| Res::SelfTyParam { .. }
788785
| Res::SelfTyAlias { .. } => match ty.normalized.ty_adt_def() {
789786
Some(adt) if !adt.is_enum() => {
@@ -871,7 +868,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
871868
let decl_ty = self.local_ty(decl.span, decl.hir_id);
872869

873870
// Type check the initializer.
874-
if let Some(ref init) = decl.init {
871+
if let Some(init) = decl.init {
875872
let init_ty = self.check_decl_initializer(decl.hir_id, decl.pat, init);
876873
self.overwrite_local_ty_if_err(decl.hir_id, decl.pat, init_ty);
877874
}
@@ -935,7 +932,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
935932
}
936933
// Ignore for now.
937934
hir::StmtKind::Item(_) => {}
938-
hir::StmtKind::Expr(ref expr) => {
935+
hir::StmtKind::Expr(expr) => {
939936
// Check with expected type of `()`.
940937
self.check_expr_has_type_or_error(expr, self.tcx.types.unit, |err| {
941938
if self.is_next_stmt_expr_continuation(stmt.hir_id)
@@ -1774,10 +1771,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17741771
let params =
17751772
params.get(is_method as usize..params.len() - sig.decl.c_variadic as usize)?;
17761773
debug_assert_eq!(params.len(), fn_inputs.len());
1777-
Some((
1778-
fn_inputs.zip(params.iter().map(|param| FnParam::Param(param))).collect(),
1779-
generics,
1780-
))
1774+
Some((fn_inputs.zip(params.iter().map(FnParam::Param)).collect(), generics))
17811775
}
17821776
(None, Some(params)) => {
17831777
let params =
@@ -2639,7 +2633,7 @@ impl<'a, 'b, 'tcx> FnCallDiagCtxt<'a, 'b, 'tcx> {
26392633
suggestions: Vec<(Span, String)>,
26402634
suggestion_text: SuggestionText,
26412635
) -> Option<String> {
2642-
let suggestion_text = match suggestion_text {
2636+
match suggestion_text {
26432637
SuggestionText::None => None,
26442638
SuggestionText::Provide(plural) => {
26452639
Some(format!("provide the argument{}", if plural { "s" } else { "" }))
@@ -2655,8 +2649,7 @@ impl<'a, 'b, 'tcx> FnCallDiagCtxt<'a, 'b, 'tcx> {
26552649
SuggestionText::Swap => Some("swap these arguments".to_string()),
26562650
SuggestionText::Reorder => Some("reorder these arguments".to_string()),
26572651
SuggestionText::DidYouMean => Some("did you mean".to_string()),
2658-
};
2659-
suggestion_text
2652+
}
26602653
}
26612654

26622655
fn arguments_formatting(&self, suggestion_span: Span) -> ArgumentsFormatting {
@@ -2954,7 +2947,7 @@ impl<'a, 'b, 'tcx> ArgsCtxt<'a, 'b, 'tcx> {
29542947
.fn_ctxt
29552948
.typeck_results
29562949
.borrow()
2957-
.expr_ty_adjusted_opt(*expr)
2950+
.expr_ty_adjusted_opt(expr)
29582951
.unwrap_or_else(|| Ty::new_misc_error(self.call_ctxt.fn_ctxt.tcx));
29592952
(
29602953
self.call_ctxt.fn_ctxt.resolve_vars_if_possible(ty),

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,7 +1132,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11321132
})
11331133
.collect::<Vec<String>>();
11341134

1135-
if all_matching_bounds_strs.len() == 0 {
1135+
if all_matching_bounds_strs.is_empty() {
11361136
return;
11371137
}
11381138

@@ -1336,7 +1336,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13361336
expected_ty = *inner_expected_ty;
13371337
}
13381338
(hir::ExprKind::Block(blk, _), _, _) => {
1339-
self.suggest_block_to_brackets(diag, *blk, expr_ty, expected_ty);
1339+
self.suggest_block_to_brackets(diag, blk, expr_ty, expected_ty);
13401340
break true;
13411341
}
13421342
_ => break false,
@@ -1463,7 +1463,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14631463
{
14641464
let span = expr
14651465
.span
1466-
.find_ancestor_not_from_extern_macro(&self.tcx.sess.source_map())
1466+
.find_ancestor_not_from_extern_macro(self.tcx.sess.source_map())
14671467
.unwrap_or(expr.span);
14681468

14691469
let mut sugg = if self.precedence(expr) >= ExprPrecedence::Unambiguous {
@@ -2133,7 +2133,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21332133

21342134
let span = expr
21352135
.span
2136-
.find_ancestor_not_from_extern_macro(&self.tcx.sess.source_map())
2136+
.find_ancestor_not_from_extern_macro(self.tcx.sess.source_map())
21372137
.unwrap_or(expr.span);
21382138
err.span_suggestion_verbose(span.shrink_to_hi(), msg, sugg, Applicability::HasPlaceholders);
21392139
true
@@ -2246,7 +2246,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
22462246

22472247
let src_map = tcx.sess.source_map();
22482248
let suggestion = if src_map.is_multiline(expr.span) {
2249-
let indentation = src_map.indentation_before(span).unwrap_or_else(String::new);
2249+
let indentation = src_map.indentation_before(span).unwrap_or_default();
22502250
format!("\n{indentation}/* {suggestion} */")
22512251
} else {
22522252
// If the entire expr is on a single line
@@ -2278,7 +2278,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
22782278
// Check if `expr` is contained in array of two elements
22792279
if let hir::Node::Expr(array_expr) = self.tcx.parent_hir_node(expr.hir_id)
22802280
&& let hir::ExprKind::Array(elements) = array_expr.kind
2281-
&& let [first, second] = &elements[..]
2281+
&& let [first, second] = elements
22822282
&& second.hir_id == expr.hir_id
22832283
{
22842284
// Span between the two elements of the array

compiler/rustc_hir_typeck/src/gather_locals.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ impl<'a, 'tcx> GatherLocalsVisitor<'a, 'tcx> {
135135
/// again during type checking by querying [`FnCtxt::local_ty`] for the same hir_id.
136136
fn declare(&mut self, decl: Declaration<'tcx>) {
137137
let local_ty = match decl.ty {
138-
Some(ref ty) => {
138+
Some(ty) => {
139139
let o_ty = self.fcx.lower_ty(ty);
140140

141141
let c_ty = self.fcx.infcx.canonicalize_user_type_annotation(

compiler/rustc_hir_typeck/src/loops.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -147,17 +147,15 @@ impl<'hir> Visitor<'hir> for CheckLoopVisitor<'hir> {
147147
}
148148
}
149149
}
150-
hir::ExprKind::Loop(ref b, _, source, _) => {
150+
hir::ExprKind::Loop(b, _, source, _) => {
151151
let cx = match self.is_loop_match(e, b) {
152152
Some(labeled_block) => LoopMatch { labeled_block },
153153
None => Loop(source),
154154
};
155155

156156
self.with_context(cx, |v| v.visit_block(b));
157157
}
158-
hir::ExprKind::Closure(&hir::Closure {
159-
ref fn_decl, body, fn_decl_span, kind, ..
160-
}) => {
158+
hir::ExprKind::Closure(&hir::Closure { fn_decl, body, fn_decl_span, kind, .. }) => {
161159
let cx = match kind {
162160
hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(kind, source)) => {
163161
Coroutine { coroutine_span: fn_decl_span, kind, source }
@@ -167,16 +165,16 @@ impl<'hir> Visitor<'hir> for CheckLoopVisitor<'hir> {
167165
self.visit_fn_decl(fn_decl);
168166
self.with_context(cx, |v| v.visit_nested_body(body));
169167
}
170-
hir::ExprKind::Block(ref b, Some(_label)) => {
168+
hir::ExprKind::Block(b, Some(_label)) => {
171169
self.with_context(LabeledBlock, |v| v.visit_block(b));
172170
}
173-
hir::ExprKind::Block(ref b, None)
171+
hir::ExprKind::Block(b, None)
174172
if matches!(self.cx_stack.last(), Some(&Fn) | Some(&ConstBlock)) =>
175173
{
176174
self.with_context(Normal, |v| v.visit_block(b));
177175
}
178176
hir::ExprKind::Block(
179-
ref b @ hir::Block { rules: hir::BlockCheckMode::DefaultBlock, .. },
177+
b @ hir::Block { rules: hir::BlockCheckMode::DefaultBlock, .. },
180178
None,
181179
) if matches!(
182180
self.cx_stack.last(),
@@ -431,10 +429,7 @@ impl<'hir> CheckLoopVisitor<'hir> {
431429

432430
// Accept either `state = expr` or `state = expr;`.
433431
let loop_body_expr = match body.stmts {
434-
[] => match body.expr {
435-
Some(expr) => expr,
436-
None => return None,
437-
},
432+
[] => body.expr?,
438433
[single] if body.expr.is_none() => match single.kind {
439434
hir::StmtKind::Expr(expr) | hir::StmtKind::Semi(expr) => expr,
440435
_ => return None,

compiler/rustc_index/src/bit_set.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1409,7 +1409,7 @@ impl<R: Idx, C: Idx> BitMatrix<R, C> {
14091409
BitMatrix {
14101410
num_rows,
14111411
num_columns,
1412-
words: iter::repeat(&row.words).take(num_rows).flatten().cloned().collect(),
1412+
words: iter::repeat_n(&row.words, num_rows).flatten().cloned().collect(),
14131413
marker: PhantomData,
14141414
}
14151415
}

compiler/rustc_mir_transform/src/impossible_predicates.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
//! parameters, so this filtering serves two purposes:
2121
//!
2222
//! 1. We skip evaluating any predicates that we would
23-
//! never be able prove are unsatisfiable (e.g. `<T as Foo>`
23+
//! never be able prove are unsatisfiable (e.g. `<T as Foo>`
2424
//! 2. We avoid trying to normalize predicates involving generic
25-
//! parameters (e.g. `<T as Foo>::MyItem`). This can confuse
26-
//! the normalization code (leading to cycle errors), since
27-
//! it's usually never invoked in this way.
25+
//! parameters (e.g. `<T as Foo>::MyItem`). This can confuse
26+
//! the normalization code (leading to cycle errors), since
27+
//! it's usually never invoked in this way.
2828
2929
use rustc_middle::mir::{Body, START_BLOCK, TerminatorKind};
3030
use rustc_middle::ty::{TyCtxt, TypeFlags, TypeVisitableExt};

compiler/rustc_mir_transform/src/inline.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ fn try_inlining<'tcx, I: Inliner<'tcx>>(
635635

636636
// Normally, this shouldn't be required, but trait normalization failure can create a
637637
// validation ICE.
638-
if !validate_types(tcx, inliner.typing_env(), &callee_body, &caller_body).is_empty() {
638+
if !validate_types(tcx, inliner.typing_env(), &callee_body, caller_body).is_empty() {
639639
debug!("failed to validate callee body");
640640
return Err("implementation limitation -- callee body failed validation");
641641
}

0 commit comments

Comments
 (0)