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:
Stuart Cook
2025-08-07 20:49:45 +10:00
committed by GitHub
2 changed files with 45 additions and 24 deletions

View File

@@ -434,7 +434,7 @@ fn emit_mismatch_diagnostic<'tcx>(
lints::MismatchedLifetimeSyntaxesSuggestion::Mixed { lints::MismatchedLifetimeSyntaxesSuggestion::Mixed {
implicit_suggestions, implicit_suggestions,
explicit_anonymous_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 implicit_suggestion = should_suggest_implicit.then(|| {
let suggestions = make_implicit_suggestions(&suggest_change_to_implicit); 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!( tracing::debug!(
@@ -508,7 +511,7 @@ fn build_mismatch_suggestion(
lints::MismatchedLifetimeSyntaxesSuggestion::Explicit { lints::MismatchedLifetimeSyntaxesSuggestion::Explicit {
lifetime_name, lifetime_name,
suggestions, suggestions,
tool_only: false, optional_alternative: false,
} }
} }

View File

@@ -3292,7 +3292,7 @@ impl<'a, G: EmissionGuarantee> LintDiagnostic<'a, G> for MismatchedLifetimeSynta
diag.subdiagnostic(s); diag.subdiagnostic(s);
for mut s in suggestions { for mut s in suggestions {
s.make_tool_only(); s.make_optional_alternative();
diag.subdiagnostic(s); diag.subdiagnostic(s);
} }
} }
@@ -3303,33 +3303,33 @@ impl<'a, G: EmissionGuarantee> LintDiagnostic<'a, G> for MismatchedLifetimeSynta
pub(crate) enum MismatchedLifetimeSyntaxesSuggestion { pub(crate) enum MismatchedLifetimeSyntaxesSuggestion {
Implicit { Implicit {
suggestions: Vec<Span>, suggestions: Vec<Span>,
tool_only: bool, optional_alternative: bool,
}, },
Mixed { Mixed {
implicit_suggestions: Vec<Span>, implicit_suggestions: Vec<Span>,
explicit_anonymous_suggestions: Vec<(Span, String)>, explicit_anonymous_suggestions: Vec<(Span, String)>,
tool_only: bool, optional_alternative: bool,
}, },
Explicit { Explicit {
lifetime_name: String, lifetime_name: String,
suggestions: Vec<(Span, String)>, suggestions: Vec<(Span, String)>,
tool_only: bool, optional_alternative: bool,
}, },
} }
impl MismatchedLifetimeSyntaxesSuggestion { impl MismatchedLifetimeSyntaxesSuggestion {
fn make_tool_only(&mut self) { fn make_optional_alternative(&mut self) {
use MismatchedLifetimeSyntaxesSuggestion::*; use MismatchedLifetimeSyntaxesSuggestion::*;
let tool_only = match self { let optional_alternative = match self {
Implicit { tool_only, .. } | Mixed { tool_only, .. } | Explicit { tool_only, .. } => { Implicit { optional_alternative, .. }
tool_only | 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>) { fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>) {
use MismatchedLifetimeSyntaxesSuggestion::*; use MismatchedLifetimeSyntaxesSuggestion::*;
let style = |tool_only| { let style = |optional_alternative| {
if tool_only { SuggestionStyle::CompletelyHidden } else { SuggestionStyle::ShowAlways } 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 { match self {
Implicit { suggestions, tool_only } => { Implicit { suggestions, optional_alternative } => {
let suggestions = suggestions.into_iter().map(|s| (s, String::new())).collect(); let suggestions = suggestions.into_iter().map(|s| (s, String::new())).collect();
diag.multipart_suggestion_with_style( diag.multipart_suggestion_with_style(
fluent::lint_mismatched_lifetime_syntaxes_suggestion_implicit, fluent::lint_mismatched_lifetime_syntaxes_suggestion_implicit,
suggestions, suggestions,
Applicability::MaybeIncorrect, applicability(optional_alternative),
style(tool_only), 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() { let message = if implicit_suggestions.is_empty() {
fluent::lint_mismatched_lifetime_syntaxes_suggestion_mixed_only_paths fluent::lint_mismatched_lifetime_syntaxes_suggestion_mixed_only_paths
} else { } else {
@@ -3368,12 +3386,12 @@ impl Subdiagnostic for MismatchedLifetimeSyntaxesSuggestion {
diag.multipart_suggestion_with_style( diag.multipart_suggestion_with_style(
message, message,
suggestions, suggestions,
Applicability::MaybeIncorrect, applicability(optional_alternative),
style(tool_only), style(optional_alternative),
); );
} }
Explicit { lifetime_name, suggestions, tool_only } => { Explicit { lifetime_name, suggestions, optional_alternative } => {
diag.arg("lifetime_name", lifetime_name); diag.arg("lifetime_name", lifetime_name);
let msg = diag.eagerly_translate( let msg = diag.eagerly_translate(
fluent::lint_mismatched_lifetime_syntaxes_suggestion_explicit, fluent::lint_mismatched_lifetime_syntaxes_suggestion_explicit,
@@ -3382,8 +3400,8 @@ impl Subdiagnostic for MismatchedLifetimeSyntaxesSuggestion {
diag.multipart_suggestion_with_style( diag.multipart_suggestion_with_style(
msg, msg,
suggestions, suggestions,
Applicability::MaybeIncorrect, applicability(optional_alternative),
style(tool_only), style(optional_alternative),
); );
} }
} }