Skip to content

Commit 77f288c

Browse files
committed
Include reference in lint diagnostic
1 parent 7d7eb97 commit 77f288c

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

compiler/rustc_lint/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ lint_expectation = this lint expectation is unfulfilled
215215
.rationale = {$rationale}
216216
217217
lint_for_loops_over_fallibles =
218-
for loop over {$article} `{$ty}`. This is more readably written as an `if let` statement
218+
for loop over {$article} `{$ref_prefix}{$ty}`. This is more readably written as an `if let` statement
219219
.suggestion = consider using `if let` to clear intent
220220
.remove_next = to iterate over `{$recv_snip}` remove the call to `next`
221221
.use_while_let = to check pattern in a loop use `while let`

compiler/rustc_lint/src/for_loops_over_fallibles.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ impl<'tcx> LateLintPass<'tcx> for ForLoopsOverFallibles {
5252

5353
let ty = cx.typeck_results().expr_ty(arg);
5454

55-
// let &ty::Adt(adt, args) = ty.kind() else { return };
56-
let (adt, args, _) = match ty.kind() {
55+
let (adt, args, ref_mutability) = match ty.kind() {
5756
&ty::Adt(adt, args) => (adt, args, None),
5857
&ty::Ref(_, ty, mutability) => match ty.kind() {
5958
&ty::Adt(adt, args) => (adt, args, Some(mutability)),
@@ -68,6 +67,11 @@ impl<'tcx> LateLintPass<'tcx> for ForLoopsOverFallibles {
6867
_ => return,
6968
};
7069

70+
let ref_prefix = match ref_mutability {
71+
None => "",
72+
Some(ref_mutability) => ref_mutability.ref_prefix_str(),
73+
};
74+
7175
let sub = if let Some(recv) = extract_iterator_next_call(cx, arg)
7276
&& let Ok(recv_snip) = cx.sess().source_map().span_to_snippet(recv.span)
7377
{
@@ -93,7 +97,7 @@ impl<'tcx> LateLintPass<'tcx> for ForLoopsOverFallibles {
9397
cx.emit_span_lint(
9498
FOR_LOOPS_OVER_FALLIBLES,
9599
arg.span,
96-
ForLoopsOverFalliblesDiag { article, ty, sub, question_mark, suggestion },
100+
ForLoopsOverFalliblesDiag { article, ref_prefix, ty, sub, question_mark, suggestion },
97101
);
98102
}
99103
}

compiler/rustc_lint/src/lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,7 @@ pub enum PtrNullChecksDiag<'a> {
613613
#[diag(lint_for_loops_over_fallibles)]
614614
pub struct ForLoopsOverFalliblesDiag<'a> {
615615
pub article: &'static str,
616+
pub ref_prefix: &'static str,
616617
pub ty: &'static str,
617618
#[subdiagnostic]
618619
pub sub: ForLoopsOverFalliblesLoopSub<'a>,

0 commit comments

Comments
 (0)