Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit c63ba52

Browse files
committed
migrate: array_into_iter.rs
1 parent 95d3e0c commit c63ba52

File tree

2 files changed

+45
-39
lines changed

2 files changed

+45
-39
lines changed

compiler/rustc_lint/src/array_into_iter.rs

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
#![deny(rustc::untranslatable_diagnostic)]
2+
#![deny(rustc::diagnostic_outside_of_impl)]
3+
use crate::lints::{ArrayIntoIterDiag, ArrayIntoIterDiagSub};
14
use crate::{LateContext, LateLintPass, LintContext};
2-
use rustc_errors::{fluent, Applicability};
35
use rustc_hir as hir;
46
use rustc_middle::ty;
57
use rustc_middle::ty::adjustment::{Adjust, Adjustment};
@@ -118,41 +120,23 @@ impl<'tcx> LateLintPass<'tcx> for ArrayIntoIter {
118120
// to an array or to a slice.
119121
_ => bug!("array type coerced to something other than array or slice"),
120122
};
121-
cx.struct_span_lint(
123+
let sub = if self.for_expr_span == expr.span {
124+
Some(ArrayIntoIterDiagSub::RemoveIntoIter {
125+
span: receiver_arg.span.shrink_to_hi().to(expr.span.shrink_to_hi()),
126+
})
127+
} else if receiver_ty.is_array() {
128+
Some(ArrayIntoIterDiagSub::UseExplicitIntoIter {
129+
start_span: expr.span.shrink_to_lo(),
130+
end_span: receiver_arg.span.shrink_to_hi().to(expr.span.shrink_to_hi()),
131+
})
132+
} else {
133+
None
134+
};
135+
cx.emit_spanned_lint(
122136
ARRAY_INTO_ITER,
123137
call.ident.span,
124-
fluent::lint_array_into_iter,
125-
|diag| {
126-
diag.set_arg("target", target);
127-
diag.span_suggestion(
128-
call.ident.span,
129-
fluent::use_iter_suggestion,
130-
"iter",
131-
Applicability::MachineApplicable,
132-
);
133-
if self.for_expr_span == expr.span {
134-
diag.span_suggestion(
135-
receiver_arg.span.shrink_to_hi().to(expr.span.shrink_to_hi()),
136-
fluent::remove_into_iter_suggestion,
137-
"",
138-
Applicability::MaybeIncorrect,
139-
);
140-
} else if receiver_ty.is_array() {
141-
diag.multipart_suggestion(
142-
fluent::use_explicit_into_iter_suggestion,
143-
vec![
144-
(expr.span.shrink_to_lo(), "IntoIterator::into_iter(".into()),
145-
(
146-
receiver_arg.span.shrink_to_hi().to(expr.span.shrink_to_hi()),
147-
")".into(),
148-
),
149-
],
150-
Applicability::MaybeIncorrect,
151-
);
152-
}
153-
diag
154-
},
155-
)
138+
ArrayIntoIterDiag { target, suggestion: call.ident.span, sub },
139+
);
156140
}
157141
}
158142
}

compiler/rustc_lint/src/lints.rs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,32 @@ use rustc_span::{symbol::Ident, Span, Symbol};
66

77
use crate::LateContext;
88

9+
#[derive(LintDiagnostic)]
10+
#[diag(lint_array_into_iter)]
11+
pub struct ArrayIntoIterDiag<'a> {
12+
pub target: &'a str,
13+
#[suggestion(use_iter_suggestion, code = "iter", applicability = "machine-applicable")]
14+
pub suggestion: Span,
15+
#[subdiagnostic]
16+
pub sub: Option<ArrayIntoIterDiagSub>,
17+
}
18+
19+
#[derive(SessionSubdiagnostic)]
20+
pub enum ArrayIntoIterDiagSub {
21+
#[suggestion(remove_into_iter_suggestion, code = "", applicability = "maybe-incorrect")]
22+
RemoveIntoIter {
23+
#[primary_span]
24+
span: Span,
25+
},
26+
#[multipart_suggestion(use_explicit_into_iter_suggestion, applicability = "maybe-incorrect")]
27+
UseExplicitIntoIter {
28+
#[suggestion_part(code = "IntoIterator::into_iter(")]
29+
start_span: Span,
30+
#[suggestion_part(code = ")")]
31+
end_span: Span,
32+
},
33+
}
34+
935
#[derive(LintDiagnostic)]
1036
#[diag(lint_cstring_ptr)]
1137
#[note]
@@ -454,11 +480,7 @@ pub struct PathStatementDrop {
454480

455481
#[derive(SessionSubdiagnostic)]
456482
pub enum PathStatementDropSub {
457-
#[suggestion(
458-
suggestion,
459-
code = "drop({snippet});",
460-
applicability = "machine-applicable"
461-
)]
483+
#[suggestion(suggestion, code = "drop({snippet});", applicability = "machine-applicable")]
462484
Suggestion {
463485
#[primary_span]
464486
span: Span,

0 commit comments

Comments
 (0)