Skip to content

Allow cargo fix to partially apply mismatched_lifetime_syntaxes #144601

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
9 changes: 6 additions & 3 deletions compiler/rustc_lint/src/lifetime_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ fn emit_mismatch_diagnostic<'tcx>(
lints::MismatchedLifetimeSyntaxesSuggestion::Mixed {
implicit_suggestions,
explicit_anonymous_suggestions,
tool_only: false,
optional_alternative: false,
}
});

Expand All @@ -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!(
Expand Down Expand Up @@ -508,7 +511,7 @@ fn build_mismatch_suggestion(
lints::MismatchedLifetimeSyntaxesSuggestion::Explicit {
lifetime_name,
suggestions,
tool_only: false,
optional_alternative: false,
}
}

Expand Down
60 changes: 39 additions & 21 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3276,7 +3276,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);
}
}
Expand All @@ -3287,56 +3287,74 @@ 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;
}
}

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 {
Expand All @@ -3352,12 +3370,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,
Expand All @@ -3366,8 +3384,8 @@ impl Subdiagnostic for MismatchedLifetimeSyntaxesSuggestion {
diag.multipart_suggestion_with_style(
msg,
suggestions,
Applicability::MaybeIncorrect,
style(tool_only),
applicability(optional_alternative),
style(optional_alternative),
);
}
}
Expand Down
Loading