Skip to content

Commit 36d9953

Browse files
author
Lukas Markeffsky
committed
use impl IntoIterator for multipart_suggestion and friends
1 parent 006ca9b commit 36d9953

File tree

16 files changed

+52
-72
lines changed

16 files changed

+52
-72
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -710,17 +710,14 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
710710
};
711711

712712
let Some(borrow_level) = borrow_level else { return false; };
713-
let sugg = move_sites
714-
.iter()
715-
.map(|move_site| {
716-
let move_out = self.move_data.moves[(*move_site).moi];
717-
let moved_place = &self.move_data.move_paths[move_out.path].place;
718-
let move_spans = self.move_spans(moved_place.as_ref(), move_out.source);
719-
let move_span = move_spans.args_or_use();
720-
let suggestion = borrow_level.ref_prefix_str().to_owned();
721-
(move_span.shrink_to_lo(), suggestion)
722-
})
723-
.collect();
713+
let sugg = move_sites.iter().map(|move_site| {
714+
let move_out = self.move_data.moves[(*move_site).moi];
715+
let moved_place = &self.move_data.move_paths[move_out.path].place;
716+
let move_spans = self.move_spans(moved_place.as_ref(), move_out.source);
717+
let move_span = move_spans.args_or_use();
718+
let suggestion = borrow_level.ref_prefix_str().to_owned();
719+
(move_span.shrink_to_lo(), suggestion)
720+
});
724721
err.multipart_suggestion_verbose(
725722
format!("consider {}borrowing {value_name}", borrow_level.mutably_str()),
726723
sugg,

compiler/rustc_builtin_macros/src/deriving/default.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -140,16 +140,13 @@ fn extract_default_variant<'a>(
140140
diag.note("only one variant can be default");
141141
for variant in &default_variants {
142142
// Suggest making each variant already tagged default.
143-
let suggestion = default_variants
144-
.iter()
145-
.filter_map(|v| {
146-
if v.span == variant.span {
147-
None
148-
} else {
149-
Some((cx.sess.find_by_name(&v.attrs, kw::Default)?.span, String::new()))
150-
}
151-
})
152-
.collect();
143+
let suggestion = default_variants.iter().filter_map(|v| {
144+
if v.span == variant.span {
145+
None
146+
} else {
147+
Some((cx.sess.find_by_name(&v.attrs, kw::Default)?.span, String::new()))
148+
}
149+
});
153150

154151
diag.tool_only_multipart_suggestion(
155152
&format!("make `{}` default", variant.ident),
@@ -211,7 +208,7 @@ fn validate_default_attribute(
211208
// repetitive `.span_help` call above.
212209
.tool_only_multipart_suggestion(
213210
suggestion_text,
214-
rest.iter().map(|attr| (attr.span, String::new())).collect(),
211+
rest.iter().map(|attr| (attr.span, String::new())),
215212
Applicability::MachineApplicable,
216213
)
217214
.emit();

compiler/rustc_errors/src/diagnostic.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ impl Diagnostic {
595595
pub fn multipart_suggestion(
596596
&mut self,
597597
msg: impl Into<SubdiagnosticMessage>,
598-
suggestion: Vec<(Span, String)>,
598+
suggestion: impl IntoIterator<Item = (Span, String)>,
599599
applicability: Applicability,
600600
) -> &mut Self {
601601
self.multipart_suggestion_with_style(
@@ -611,7 +611,7 @@ impl Diagnostic {
611611
pub fn multipart_suggestion_verbose(
612612
&mut self,
613613
msg: impl Into<SubdiagnosticMessage>,
614-
suggestion: Vec<(Span, String)>,
614+
suggestion: impl IntoIterator<Item = (Span, String)>,
615615
applicability: Applicability,
616616
) -> &mut Self {
617617
self.multipart_suggestion_with_style(
@@ -625,7 +625,7 @@ impl Diagnostic {
625625
pub fn multipart_suggestion_with_style(
626626
&mut self,
627627
msg: impl Into<SubdiagnosticMessage>,
628-
suggestion: Vec<(Span, String)>,
628+
suggestion: impl IntoIterator<Item = (Span, String)>,
629629
applicability: Applicability,
630630
style: SuggestionStyle,
631631
) -> &mut Self {
@@ -666,7 +666,7 @@ impl Diagnostic {
666666
pub fn tool_only_multipart_suggestion(
667667
&mut self,
668668
msg: impl Into<SubdiagnosticMessage>,
669-
suggestion: Vec<(Span, String)>,
669+
suggestion: impl IntoIterator<Item = (Span, String)>,
670670
applicability: Applicability,
671671
) -> &mut Self {
672672
self.multipart_suggestion_with_style(
@@ -807,7 +807,7 @@ impl Diagnostic {
807807
pub fn multipart_suggestions(
808808
&mut self,
809809
msg: impl Into<SubdiagnosticMessage>,
810-
suggestions: impl IntoIterator<Item = Vec<(Span, String)>>,
810+
suggestions: impl IntoIterator<Item = impl IntoIterator<Item = (Span, String)>>,
811811
applicability: Applicability,
812812
) -> &mut Self {
813813
let substitutions = suggestions

compiler/rustc_errors/src/diagnostic_builder.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -625,19 +625,19 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
625625
forward!(pub fn multipart_suggestion(
626626
&mut self,
627627
msg: impl Into<SubdiagnosticMessage>,
628-
suggestion: Vec<(Span, String)>,
628+
suggestion: impl IntoIterator<Item = (Span, String)>,
629629
applicability: Applicability,
630630
) -> &mut Self);
631631
forward!(pub fn multipart_suggestion_verbose(
632632
&mut self,
633633
msg: impl Into<SubdiagnosticMessage>,
634-
suggestion: Vec<(Span, String)>,
634+
suggestion: impl IntoIterator<Item = (Span, String)>,
635635
applicability: Applicability,
636636
) -> &mut Self);
637637
forward!(pub fn tool_only_multipart_suggestion(
638638
&mut self,
639639
msg: impl Into<SubdiagnosticMessage>,
640-
suggestion: Vec<(Span, String)>,
640+
suggestion: impl IntoIterator<Item = (Span, String)>,
641641
applicability: Applicability,
642642
) -> &mut Self);
643643
forward!(pub fn span_suggestion(
@@ -657,7 +657,7 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
657657
forward!(pub fn multipart_suggestions(
658658
&mut self,
659659
msg: impl Into<SubdiagnosticMessage>,
660-
suggestions: impl IntoIterator<Item = Vec<(Span, String)>>,
660+
suggestions: impl IntoIterator<Item = impl IntoIterator<Item = (Span, String)>>,
661661
applicability: Applicability,
662662
) -> &mut Self);
663663
forward!(pub fn span_suggestion_short(

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,8 @@ fn check_object_unsafe_self_trait_by_name(tcx: TyCtxt<'_>, item: &hir::TraitItem
844844
if tcx.check_is_object_safe(trait_def_id) {
845845
return;
846846
}
847-
let sugg = trait_should_be_self.iter().map(|span| (*span, "Self".to_string())).collect();
847+
let sugg =
848+
trait_should_be_self.iter().map(|span| (*span, "Self".to_string())).collect::<Vec<_>>();
848849
tcx.sess
849850
.struct_span_err(
850851
trait_should_be_self,

compiler/rustc_infer/src/infer/error_reporting/suggest.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
270270
}) => {
271271
diag.multipart_suggestion_verbose(
272272
"consider `await`ing on the `Future`",
273-
prior_arms
274-
.iter()
275-
.map(|arm| (arm.shrink_to_hi(), ".await".to_string()))
276-
.collect(),
273+
prior_arms.iter().map(|arm| (arm.shrink_to_hi(), ".await".to_string())),
277274
Applicability::MaybeIncorrect,
278275
);
279276
}

compiler/rustc_lint/src/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ pub trait LintContext: Sized {
632632
if !spans.is_empty() {
633633
db.multipart_suggestion_with_style(
634634
"if their presence wasn't intentional, you can remove them",
635-
spans.into_iter().map(|(_, span)| (span, "".to_string())).collect(),
635+
spans.into_iter().map(|(_, span)| (span, "".to_string())),
636636
Applicability::MachineApplicable,
637637
SuggestionStyle::HideCodeAlways,
638638
);

compiler/rustc_lint/src/lints.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -701,19 +701,16 @@ impl AddToDiagnostic for HiddenUnicodeCodepointsDiagSub {
701701
HiddenUnicodeCodepointsDiagSub::Escape { spans } => {
702702
diag.multipart_suggestion_with_style(
703703
fluent::suggestion_remove,
704-
spans.iter().map(|(_, span)| (*span, "".to_string())).collect(),
704+
spans.iter().map(|(_, span)| (*span, "".to_string())),
705705
Applicability::MachineApplicable,
706706
SuggestionStyle::HideCodeAlways,
707707
);
708708
diag.multipart_suggestion(
709709
fluent::suggestion_escape,
710-
spans
711-
.into_iter()
712-
.map(|(c, span)| {
713-
let c = format!("{:?}", c);
714-
(span, c[1..c.len() - 1].to_string())
715-
})
716-
.collect(),
710+
spans.into_iter().map(|(c, span)| {
711+
let c = format!("{:?}", c);
712+
(span, c[1..c.len() - 1].to_string())
713+
}),
717714
Applicability::MachineApplicable,
718715
);
719716
}

compiler/rustc_middle/src/ty/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ pub fn suggest_constraining_type_params<'a>(
387387
} else if suggestions.len() > 1 {
388388
err.multipart_suggestion_verbose(
389389
"consider restricting type parameters",
390-
suggestions.into_iter().map(|(span, suggestion, _)| (span, suggestion)).collect(),
390+
suggestions.into_iter().map(|(span, suggestion, _)| (span, suggestion)),
391391
applicability,
392392
);
393393
}

compiler/rustc_passes/src/liveness.rs

+9-12
Original file line numberDiff line numberDiff line change
@@ -1605,18 +1605,15 @@ impl<'tcx> Liveness<'_, 'tcx> {
16051605
|lint| {
16061606
lint.multipart_suggestion(
16071607
"try removing the field",
1608-
hir_ids_and_spans
1609-
.iter()
1610-
.map(|(_, pat_span, _)| {
1611-
let span = self
1612-
.ir
1613-
.tcx
1614-
.sess
1615-
.source_map()
1616-
.span_extend_to_next_char(*pat_span, ',', true);
1617-
(span.with_hi(BytePos(span.hi().0 + 1)), String::new())
1618-
})
1619-
.collect(),
1608+
hir_ids_and_spans.iter().map(|(_, pat_span, _)| {
1609+
let span = self
1610+
.ir
1611+
.tcx
1612+
.sess
1613+
.source_map()
1614+
.span_extend_to_next_char(*pat_span, ',', true);
1615+
(span.with_hi(BytePos(span.hi().0 + 1)), String::new())
1616+
}),
16201617
Applicability::MachineApplicable,
16211618
)
16221619
},

compiler/rustc_resolve/src/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1612,7 +1612,7 @@ impl<'a> Resolver<'a> {
16121612
"consider making the field{} publicly accessible",
16131613
pluralize!(fields.len())
16141614
),
1615-
fields.iter().map(|span| (*span, "pub ".to_string())).collect(),
1615+
fields.iter().map(|span| (*span, "pub ".to_string())),
16161616
Applicability::MaybeIncorrect,
16171617
);
16181618
}

compiler/rustc_resolve/src/late/diagnostics.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1464,7 +1464,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
14641464
"consider making the field{} publicly accessible",
14651465
pluralize!(fields.len())
14661466
),
1467-
fields.iter().map(|span| (*span, "pub ".to_string())).collect(),
1467+
fields.iter().map(|span| (*span, "pub ".to_string())),
14681468
Applicability::MaybeIncorrect,
14691469
);
14701470
}
@@ -2580,9 +2580,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
25802580
|err, higher_ranked, span, message, intro_sugg| {
25812581
err.multipart_suggestion_verbose(
25822582
message,
2583-
std::iter::once((span, intro_sugg))
2584-
.chain(spans_suggs.iter().cloned())
2585-
.collect(),
2583+
std::iter::once((span, intro_sugg)).chain(spans_suggs.iter().cloned()),
25862584
Applicability::MaybeIncorrect,
25872585
);
25882586
higher_ranked

src/tools/clippy/clippy_lints/src/index_refutable_slice.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,7 @@ fn lint_slice(cx: &LateContext<'_>, slice: &SliceLintInformation) {
164164
slice
165165
.pattern_spans
166166
.iter()
167-
.map(|span| (*span, pat_sugg.clone()))
168-
.collect(),
167+
.map(|span| (*span, pat_sugg.clone())),
169168
Applicability::MaybeIncorrect,
170169
);
171170

@@ -174,8 +173,7 @@ fn lint_slice(cx: &LateContext<'_>, slice: &SliceLintInformation) {
174173
slice
175174
.index_use
176175
.iter()
177-
.map(|(index, span)| (*span, value_name(*index)))
178-
.collect(),
176+
.map(|(index, span)| (*span, value_name(*index))),
179177
Applicability::MaybeIncorrect,
180178
);
181179

src/tools/clippy/clippy_lints/src/needless_for_each.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessForEach {
9292
ret_collector
9393
.spans
9494
.into_iter()
95-
.map(|span| (span, "continue".to_string()))
96-
.collect(),
95+
.map(|span| (span, "continue".to_string())),
9796
),
9897
)
9998
};

src/tools/clippy/clippy_lints/src/ptr.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,7 @@ impl<'tcx> LateLintPass<'tcx> for Ptr {
233233
r.expr_span,
234234
format!("{}{}", snippet_opt(cx, r.self_span).unwrap(), r.replacement),
235235
)
236-
}))
237-
.collect(),
236+
})),
238237
Applicability::Unspecified,
239238
);
240239
});

src/tools/clippy/clippy_utils/src/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -232,5 +232,5 @@ pub fn multispan_sugg_with_applicability<I>(
232232
) where
233233
I: IntoIterator<Item = (Span, String)>,
234234
{
235-
diag.multipart_suggestion(help_msg, sugg.into_iter().collect(), applicability);
235+
diag.multipart_suggestion(help_msg, sugg, applicability);
236236
}

0 commit comments

Comments
 (0)