Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement suggestion for never type fallback lints #132383

Merged
merged 6 commits into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 78 additions & 5 deletions compiler/rustc_hir_typeck/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,19 +169,34 @@ pub(crate) struct MissingParenthesesInRange {
pub(crate) enum NeverTypeFallbackFlowingIntoUnsafe {
#[help]
#[diag(hir_typeck_never_type_fallback_flowing_into_unsafe_call)]
Call,
Call {
#[subdiagnostic]
sugg: SuggestAnnotations,
},
#[help]
#[diag(hir_typeck_never_type_fallback_flowing_into_unsafe_method)]
Method,
Method {
#[subdiagnostic]
sugg: SuggestAnnotations,
},
#[help]
#[diag(hir_typeck_never_type_fallback_flowing_into_unsafe_path)]
Path,
Path {
#[subdiagnostic]
sugg: SuggestAnnotations,
},
#[help]
#[diag(hir_typeck_never_type_fallback_flowing_into_unsafe_union_field)]
UnionField,
UnionField {
#[subdiagnostic]
sugg: SuggestAnnotations,
},
#[help]
#[diag(hir_typeck_never_type_fallback_flowing_into_unsafe_deref)]
Deref,
Deref {
#[subdiagnostic]
sugg: SuggestAnnotations,
},
}

#[derive(LintDiagnostic)]
Expand All @@ -191,6 +206,64 @@ pub(crate) struct DependencyOnUnitNeverTypeFallback<'tcx> {
#[note]
pub obligation_span: Span,
pub obligation: ty::Predicate<'tcx>,
#[subdiagnostic]
pub sugg: SuggestAnnotations,
}

#[derive(Clone)]
pub(crate) enum SuggestAnnotation {
Unit(Span),
Path(Span),
Local(Span),
Turbo(Span, usize, usize),
}

#[derive(Clone)]
pub(crate) struct SuggestAnnotations {
pub suggestions: Vec<SuggestAnnotation>,
}
impl Subdiagnostic for SuggestAnnotations {
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
self,
diag: &mut Diag<'_, G>,
_: &F,
) {
if self.suggestions.is_empty() {
return;
}

let mut suggestions = vec![];
for suggestion in self.suggestions {
match suggestion {
SuggestAnnotation::Unit(span) => {
suggestions.push((span, "()".to_string()));
}
SuggestAnnotation::Path(span) => {
suggestions.push((span.shrink_to_lo(), "<() as ".to_string()));
suggestions.push((span.shrink_to_hi(), ">".to_string()));
}
SuggestAnnotation::Local(span) => {
suggestions.push((span, ": ()".to_string()));
}
SuggestAnnotation::Turbo(span, n_args, idx) => suggestions.push((
span,
format!(
"::<{}>",
(0..n_args)
.map(|i| if i == idx { "()" } else { "_" })
.collect::<Vec<_>>()
.join(", "),
),
)),
}
}

diag.multipart_suggestion_verbose(
"use `()` annotations to avoid fallback changes",
suggestions,
Applicability::MachineApplicable,
);
}
}

#[derive(Subdiagnostic)]
Expand Down
Loading
Loading