Rollup merge of #144601 - kornelski:cargo-fix-mismatched_lifetime_syntaxes, r=petrochenkov
Allow `cargo fix` to partially apply `mismatched_lifetime_syntaxes` Workaround for https://github.com/rust-lang/rust/issues/144588#issuecomment-3128445308 Not all suggestions have to be hidden from `cargo fix`, only redundant ones. The redundant ones are already hidden from the user, so the same `tool_only` flag can be used to hide them from `cargo fix`. This way `cargo fix` will be able to correctly apply the fixes, and will apply only the fix that the compiler visibly suggests to the user.
This commit is contained in:
@@ -434,7 +434,7 @@ fn emit_mismatch_diagnostic<'tcx>(
|
||||
lints::MismatchedLifetimeSyntaxesSuggestion::Mixed {
|
||||
implicit_suggestions,
|
||||
explicit_anonymous_suggestions,
|
||||
tool_only: false,
|
||||
optional_alternative: false,
|
||||
}
|
||||
});
|
||||
|
||||
@@ -455,7 +455,10 @@ fn emit_mismatch_diagnostic<'tcx>(
|
||||
let implicit_suggestion = should_suggest_implicit.then(|| {
|
||||
let suggestions = make_implicit_suggestions(&suggest_change_to_implicit);
|
||||
|
||||
lints::MismatchedLifetimeSyntaxesSuggestion::Implicit { suggestions, tool_only: false }
|
||||
lints::MismatchedLifetimeSyntaxesSuggestion::Implicit {
|
||||
suggestions,
|
||||
optional_alternative: false,
|
||||
}
|
||||
});
|
||||
|
||||
tracing::debug!(
|
||||
@@ -508,7 +511,7 @@ fn build_mismatch_suggestion(
|
||||
lints::MismatchedLifetimeSyntaxesSuggestion::Explicit {
|
||||
lifetime_name,
|
||||
suggestions,
|
||||
tool_only: false,
|
||||
optional_alternative: false,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3292,7 +3292,7 @@ impl<'a, G: EmissionGuarantee> LintDiagnostic<'a, G> for MismatchedLifetimeSynta
|
||||
diag.subdiagnostic(s);
|
||||
|
||||
for mut s in suggestions {
|
||||
s.make_tool_only();
|
||||
s.make_optional_alternative();
|
||||
diag.subdiagnostic(s);
|
||||
}
|
||||
}
|
||||
@@ -3303,33 +3303,33 @@ impl<'a, G: EmissionGuarantee> LintDiagnostic<'a, G> for MismatchedLifetimeSynta
|
||||
pub(crate) enum MismatchedLifetimeSyntaxesSuggestion {
|
||||
Implicit {
|
||||
suggestions: Vec<Span>,
|
||||
tool_only: bool,
|
||||
optional_alternative: bool,
|
||||
},
|
||||
|
||||
Mixed {
|
||||
implicit_suggestions: Vec<Span>,
|
||||
explicit_anonymous_suggestions: Vec<(Span, String)>,
|
||||
tool_only: bool,
|
||||
optional_alternative: bool,
|
||||
},
|
||||
|
||||
Explicit {
|
||||
lifetime_name: String,
|
||||
suggestions: Vec<(Span, String)>,
|
||||
tool_only: bool,
|
||||
optional_alternative: bool,
|
||||
},
|
||||
}
|
||||
|
||||
impl MismatchedLifetimeSyntaxesSuggestion {
|
||||
fn make_tool_only(&mut self) {
|
||||
fn make_optional_alternative(&mut self) {
|
||||
use MismatchedLifetimeSyntaxesSuggestion::*;
|
||||
|
||||
let tool_only = match self {
|
||||
Implicit { tool_only, .. } | Mixed { tool_only, .. } | Explicit { tool_only, .. } => {
|
||||
tool_only
|
||||
}
|
||||
let optional_alternative = match self {
|
||||
Implicit { optional_alternative, .. }
|
||||
| Mixed { optional_alternative, .. }
|
||||
| Explicit { optional_alternative, .. } => optional_alternative,
|
||||
};
|
||||
|
||||
*tool_only = true;
|
||||
*optional_alternative = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3337,22 +3337,40 @@ impl Subdiagnostic for MismatchedLifetimeSyntaxesSuggestion {
|
||||
fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>) {
|
||||
use MismatchedLifetimeSyntaxesSuggestion::*;
|
||||
|
||||
let style = |tool_only| {
|
||||
if tool_only { SuggestionStyle::CompletelyHidden } else { SuggestionStyle::ShowAlways }
|
||||
let style = |optional_alternative| {
|
||||
if optional_alternative {
|
||||
SuggestionStyle::CompletelyHidden
|
||||
} else {
|
||||
SuggestionStyle::ShowAlways
|
||||
}
|
||||
};
|
||||
|
||||
let applicability = |optional_alternative| {
|
||||
// `cargo fix` can't handle more than one fix for the same issue,
|
||||
// so hide alternative suggestions from it by marking them as maybe-incorrect
|
||||
if optional_alternative {
|
||||
Applicability::MaybeIncorrect
|
||||
} else {
|
||||
Applicability::MachineApplicable
|
||||
}
|
||||
};
|
||||
|
||||
match self {
|
||||
Implicit { suggestions, tool_only } => {
|
||||
Implicit { suggestions, optional_alternative } => {
|
||||
let suggestions = suggestions.into_iter().map(|s| (s, String::new())).collect();
|
||||
diag.multipart_suggestion_with_style(
|
||||
fluent::lint_mismatched_lifetime_syntaxes_suggestion_implicit,
|
||||
suggestions,
|
||||
Applicability::MaybeIncorrect,
|
||||
style(tool_only),
|
||||
applicability(optional_alternative),
|
||||
style(optional_alternative),
|
||||
);
|
||||
}
|
||||
|
||||
Mixed { implicit_suggestions, explicit_anonymous_suggestions, tool_only } => {
|
||||
Mixed {
|
||||
implicit_suggestions,
|
||||
explicit_anonymous_suggestions,
|
||||
optional_alternative,
|
||||
} => {
|
||||
let message = if implicit_suggestions.is_empty() {
|
||||
fluent::lint_mismatched_lifetime_syntaxes_suggestion_mixed_only_paths
|
||||
} else {
|
||||
@@ -3368,12 +3386,12 @@ impl Subdiagnostic for MismatchedLifetimeSyntaxesSuggestion {
|
||||
diag.multipart_suggestion_with_style(
|
||||
message,
|
||||
suggestions,
|
||||
Applicability::MaybeIncorrect,
|
||||
style(tool_only),
|
||||
applicability(optional_alternative),
|
||||
style(optional_alternative),
|
||||
);
|
||||
}
|
||||
|
||||
Explicit { lifetime_name, suggestions, tool_only } => {
|
||||
Explicit { lifetime_name, suggestions, optional_alternative } => {
|
||||
diag.arg("lifetime_name", lifetime_name);
|
||||
let msg = diag.eagerly_translate(
|
||||
fluent::lint_mismatched_lifetime_syntaxes_suggestion_explicit,
|
||||
@@ -3382,8 +3400,8 @@ impl Subdiagnostic for MismatchedLifetimeSyntaxesSuggestion {
|
||||
diag.multipart_suggestion_with_style(
|
||||
msg,
|
||||
suggestions,
|
||||
Applicability::MaybeIncorrect,
|
||||
style(tool_only),
|
||||
applicability(optional_alternative),
|
||||
style(optional_alternative),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user