Skip to content

Commit d8ed17a

Browse files
authored
Rollup merge of #144601 - kornelski:cargo-fix-mismatched_lifetime_syntaxes, r=petrochenkov
Allow `cargo fix` to partially apply `mismatched_lifetime_syntaxes` Workaround for #144588 (comment) 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.
2 parents 21cf201 + 081b565 commit d8ed17a

File tree

2 files changed

+45
-24
lines changed

2 files changed

+45
-24
lines changed

compiler/rustc_lint/src/lifetime_syntax.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ fn emit_mismatch_diagnostic<'tcx>(
434434
lints::MismatchedLifetimeSyntaxesSuggestion::Mixed {
435435
implicit_suggestions,
436436
explicit_anonymous_suggestions,
437-
tool_only: false,
437+
optional_alternative: false,
438438
}
439439
});
440440

@@ -455,7 +455,10 @@ fn emit_mismatch_diagnostic<'tcx>(
455455
let implicit_suggestion = should_suggest_implicit.then(|| {
456456
let suggestions = make_implicit_suggestions(&suggest_change_to_implicit);
457457

458-
lints::MismatchedLifetimeSyntaxesSuggestion::Implicit { suggestions, tool_only: false }
458+
lints::MismatchedLifetimeSyntaxesSuggestion::Implicit {
459+
suggestions,
460+
optional_alternative: false,
461+
}
459462
});
460463

461464
tracing::debug!(
@@ -508,7 +511,7 @@ fn build_mismatch_suggestion(
508511
lints::MismatchedLifetimeSyntaxesSuggestion::Explicit {
509512
lifetime_name,
510513
suggestions,
511-
tool_only: false,
514+
optional_alternative: false,
512515
}
513516
}
514517

compiler/rustc_lint/src/lints.rs

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3292,7 +3292,7 @@ impl<'a, G: EmissionGuarantee> LintDiagnostic<'a, G> for MismatchedLifetimeSynta
32923292
diag.subdiagnostic(s);
32933293

32943294
for mut s in suggestions {
3295-
s.make_tool_only();
3295+
s.make_optional_alternative();
32963296
diag.subdiagnostic(s);
32973297
}
32983298
}
@@ -3303,56 +3303,74 @@ impl<'a, G: EmissionGuarantee> LintDiagnostic<'a, G> for MismatchedLifetimeSynta
33033303
pub(crate) enum MismatchedLifetimeSyntaxesSuggestion {
33043304
Implicit {
33053305
suggestions: Vec<Span>,
3306-
tool_only: bool,
3306+
optional_alternative: bool,
33073307
},
33083308

33093309
Mixed {
33103310
implicit_suggestions: Vec<Span>,
33113311
explicit_anonymous_suggestions: Vec<(Span, String)>,
3312-
tool_only: bool,
3312+
optional_alternative: bool,
33133313
},
33143314

33153315
Explicit {
33163316
lifetime_name: String,
33173317
suggestions: Vec<(Span, String)>,
3318-
tool_only: bool,
3318+
optional_alternative: bool,
33193319
},
33203320
}
33213321

33223322
impl MismatchedLifetimeSyntaxesSuggestion {
3323-
fn make_tool_only(&mut self) {
3323+
fn make_optional_alternative(&mut self) {
33243324
use MismatchedLifetimeSyntaxesSuggestion::*;
33253325

3326-
let tool_only = match self {
3327-
Implicit { tool_only, .. } | Mixed { tool_only, .. } | Explicit { tool_only, .. } => {
3328-
tool_only
3329-
}
3326+
let optional_alternative = match self {
3327+
Implicit { optional_alternative, .. }
3328+
| Mixed { optional_alternative, .. }
3329+
| Explicit { optional_alternative, .. } => optional_alternative,
33303330
};
33313331

3332-
*tool_only = true;
3332+
*optional_alternative = true;
33333333
}
33343334
}
33353335

33363336
impl Subdiagnostic for MismatchedLifetimeSyntaxesSuggestion {
33373337
fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>) {
33383338
use MismatchedLifetimeSyntaxesSuggestion::*;
33393339

3340-
let style = |tool_only| {
3341-
if tool_only { SuggestionStyle::CompletelyHidden } else { SuggestionStyle::ShowAlways }
3340+
let style = |optional_alternative| {
3341+
if optional_alternative {
3342+
SuggestionStyle::CompletelyHidden
3343+
} else {
3344+
SuggestionStyle::ShowAlways
3345+
}
3346+
};
3347+
3348+
let applicability = |optional_alternative| {
3349+
// `cargo fix` can't handle more than one fix for the same issue,
3350+
// so hide alternative suggestions from it by marking them as maybe-incorrect
3351+
if optional_alternative {
3352+
Applicability::MaybeIncorrect
3353+
} else {
3354+
Applicability::MachineApplicable
3355+
}
33423356
};
33433357

33443358
match self {
3345-
Implicit { suggestions, tool_only } => {
3359+
Implicit { suggestions, optional_alternative } => {
33463360
let suggestions = suggestions.into_iter().map(|s| (s, String::new())).collect();
33473361
diag.multipart_suggestion_with_style(
33483362
fluent::lint_mismatched_lifetime_syntaxes_suggestion_implicit,
33493363
suggestions,
3350-
Applicability::MaybeIncorrect,
3351-
style(tool_only),
3364+
applicability(optional_alternative),
3365+
style(optional_alternative),
33523366
);
33533367
}
33543368

3355-
Mixed { implicit_suggestions, explicit_anonymous_suggestions, tool_only } => {
3369+
Mixed {
3370+
implicit_suggestions,
3371+
explicit_anonymous_suggestions,
3372+
optional_alternative,
3373+
} => {
33563374
let message = if implicit_suggestions.is_empty() {
33573375
fluent::lint_mismatched_lifetime_syntaxes_suggestion_mixed_only_paths
33583376
} else {
@@ -3368,12 +3386,12 @@ impl Subdiagnostic for MismatchedLifetimeSyntaxesSuggestion {
33683386
diag.multipart_suggestion_with_style(
33693387
message,
33703388
suggestions,
3371-
Applicability::MaybeIncorrect,
3372-
style(tool_only),
3389+
applicability(optional_alternative),
3390+
style(optional_alternative),
33733391
);
33743392
}
33753393

3376-
Explicit { lifetime_name, suggestions, tool_only } => {
3394+
Explicit { lifetime_name, suggestions, optional_alternative } => {
33773395
diag.arg("lifetime_name", lifetime_name);
33783396
let msg = diag.eagerly_translate(
33793397
fluent::lint_mismatched_lifetime_syntaxes_suggestion_explicit,
@@ -3382,8 +3400,8 @@ impl Subdiagnostic for MismatchedLifetimeSyntaxesSuggestion {
33823400
diag.multipart_suggestion_with_style(
33833401
msg,
33843402
suggestions,
3385-
Applicability::MaybeIncorrect,
3386-
style(tool_only),
3403+
applicability(optional_alternative),
3404+
style(optional_alternative),
33873405
);
33883406
}
33893407
}

0 commit comments

Comments
 (0)