Include reference in lint diagnostic
This commit is contained in:
parent
7d7eb973d0
commit
77f288c18d
3 changed files with 9 additions and 4 deletions
|
@ -215,7 +215,7 @@ lint_expectation = this lint expectation is unfulfilled
|
||||||
.rationale = {$rationale}
|
.rationale = {$rationale}
|
||||||
|
|
||||||
lint_for_loops_over_fallibles =
|
lint_for_loops_over_fallibles =
|
||||||
for loop over {$article} `{$ty}`. This is more readably written as an `if let` statement
|
for loop over {$article} `{$ref_prefix}{$ty}`. This is more readably written as an `if let` statement
|
||||||
.suggestion = consider using `if let` to clear intent
|
.suggestion = consider using `if let` to clear intent
|
||||||
.remove_next = to iterate over `{$recv_snip}` remove the call to `next`
|
.remove_next = to iterate over `{$recv_snip}` remove the call to `next`
|
||||||
.use_while_let = to check pattern in a loop use `while let`
|
.use_while_let = to check pattern in a loop use `while let`
|
||||||
|
|
|
@ -52,8 +52,7 @@ impl<'tcx> LateLintPass<'tcx> for ForLoopsOverFallibles {
|
||||||
|
|
||||||
let ty = cx.typeck_results().expr_ty(arg);
|
let ty = cx.typeck_results().expr_ty(arg);
|
||||||
|
|
||||||
// let &ty::Adt(adt, args) = ty.kind() else { return };
|
let (adt, args, ref_mutability) = match ty.kind() {
|
||||||
let (adt, args, _) = match ty.kind() {
|
|
||||||
&ty::Adt(adt, args) => (adt, args, None),
|
&ty::Adt(adt, args) => (adt, args, None),
|
||||||
&ty::Ref(_, ty, mutability) => match ty.kind() {
|
&ty::Ref(_, ty, mutability) => match ty.kind() {
|
||||||
&ty::Adt(adt, args) => (adt, args, Some(mutability)),
|
&ty::Adt(adt, args) => (adt, args, Some(mutability)),
|
||||||
|
@ -68,6 +67,11 @@ impl<'tcx> LateLintPass<'tcx> for ForLoopsOverFallibles {
|
||||||
_ => return,
|
_ => return,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let ref_prefix = match ref_mutability {
|
||||||
|
None => "",
|
||||||
|
Some(ref_mutability) => ref_mutability.ref_prefix_str(),
|
||||||
|
};
|
||||||
|
|
||||||
let sub = if let Some(recv) = extract_iterator_next_call(cx, arg)
|
let sub = if let Some(recv) = extract_iterator_next_call(cx, arg)
|
||||||
&& let Ok(recv_snip) = cx.sess().source_map().span_to_snippet(recv.span)
|
&& let Ok(recv_snip) = cx.sess().source_map().span_to_snippet(recv.span)
|
||||||
{
|
{
|
||||||
|
@ -93,7 +97,7 @@ impl<'tcx> LateLintPass<'tcx> for ForLoopsOverFallibles {
|
||||||
cx.emit_span_lint(
|
cx.emit_span_lint(
|
||||||
FOR_LOOPS_OVER_FALLIBLES,
|
FOR_LOOPS_OVER_FALLIBLES,
|
||||||
arg.span,
|
arg.span,
|
||||||
ForLoopsOverFalliblesDiag { article, ty, sub, question_mark, suggestion },
|
ForLoopsOverFalliblesDiag { article, ref_prefix, ty, sub, question_mark, suggestion },
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -613,6 +613,7 @@ pub enum PtrNullChecksDiag<'a> {
|
||||||
#[diag(lint_for_loops_over_fallibles)]
|
#[diag(lint_for_loops_over_fallibles)]
|
||||||
pub struct ForLoopsOverFalliblesDiag<'a> {
|
pub struct ForLoopsOverFalliblesDiag<'a> {
|
||||||
pub article: &'static str,
|
pub article: &'static str,
|
||||||
|
pub ref_prefix: &'static str,
|
||||||
pub ty: &'static str,
|
pub ty: &'static str,
|
||||||
#[subdiagnostic]
|
#[subdiagnostic]
|
||||||
pub sub: ForLoopsOverFalliblesLoopSub<'a>,
|
pub sub: ForLoopsOverFalliblesLoopSub<'a>,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue