Skip to content

Commit 29bb399

Browse files
authored
Unrolled build for rust-lang#127301
Rollup merge of rust-lang#127301 - estebank:fix-suggestions, r=Urgau Tweak some structured suggestions to be more verbose and accurate Addressing some issues I found while working on rust-lang#127282. ``` error: this URL is not a hyperlink --> $DIR/auxiliary/include-str-bare-urls.md:1:11 | LL | HEADS UP! https://example.com MUST SHOW UP IN THE STDERR FILE! | ^^^^^^^^^^^^^^^^^^^ | = note: bare URLs are not automatically turned into clickable links note: the lint level is defined here --> $DIR/include-str-bare-urls.rs:14:9 | LL | #![deny(rustdoc::bare_urls)] | ^^^^^^^^^^^^^^^^^^ help: use an automatic link instead | LL | HEADS UP! <https://example.com> MUST SHOW UP IN THE STDERR FILE! | + + ``` ``` error[E0384]: cannot assign twice to immutable variable `v` --> $DIR/assign-imm-local-twice.rs:7:5 | LL | v = 1; | ----- first assignment to `v` LL | println!("v={}", v); LL | v = 2; | ^^^^^ cannot assign twice to immutable variable | help: consider making this binding mutable | LL | let mut v: isize; | +++ ``` ``` error[E0393]: the type parameter `Rhs` must be explicitly specified --> $DIR/issue-22560.rs:9:23 | LL | trait Sub<Rhs=Self> { | ------------------- type parameter `Rhs` must be specified for this ... LL | type Test = dyn Add + Sub; | ^^^ | = note: because of the default `Self` reference, type parameters must be specified on object types help: set the type parameter to the desired type | LL | type Test = dyn Add + Sub<Rhs>; | +++++ ``` ``` error[E0596]: cannot borrow `v` as mutable, as it is not declared as mutable --> $DIR/issue-33819.rs:4:34 | LL | Some(ref v) => { let a = &mut v; }, | ^^^^^^ cannot borrow as mutable | help: try removing `&mut` here | LL - Some(ref v) => { let a = &mut v; }, LL + Some(ref v) => { let a = v; }, | ``` ``` help: remove the invocation before committing it to a version control system | LL - dbg!(); | ``` ``` error[E0308]: mismatched types --> $DIR/issue-39974.rs:1:21 | LL | const LENGTH: f64 = 2; | ^ expected `f64`, found integer | help: use a float literal | LL | const LENGTH: f64 = 2.0; | ++ ``` ``` error[E0529]: expected an array or slice, found `Vec<i32>` --> $DIR/match-ergonomics.rs:8:9 | LL | [&v] => {}, | ^^^^ pattern cannot match with input type `Vec<i32>` | help: consider slicing here | LL | match x[..] { | ++++ ``` ``` error[E0609]: no field `0` on type `[u32; 1]` --> $DIR/parenthesized-deref-suggestion.rs:10:21 | LL | (x as [u32; 1]).0; | ^ unknown field | help: instead of using tuple indexing, use array indexing | LL | (x as [u32; 1])[0]; | ~ + ```
2 parents 9f877c9 + a5e7da0 commit 29bb399

File tree

82 files changed

+808
-468
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+808
-468
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+11-14
Original file line numberDiff line numberDiff line change
@@ -3757,13 +3757,11 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
37573757
assigned_span: Span,
37583758
err_place: Place<'tcx>,
37593759
) {
3760-
let (from_arg, local_decl, local_name) = match err_place.as_local() {
3761-
Some(local) => (
3762-
self.body.local_kind(local) == LocalKind::Arg,
3763-
Some(&self.body.local_decls[local]),
3764-
self.local_names[local],
3765-
),
3766-
None => (false, None, None),
3760+
let (from_arg, local_decl) = match err_place.as_local() {
3761+
Some(local) => {
3762+
(self.body.local_kind(local) == LocalKind::Arg, Some(&self.body.local_decls[local]))
3763+
}
3764+
None => (false, None),
37673765
};
37683766

37693767
// If root local is initialized immediately (everything apart from let
@@ -3795,13 +3793,12 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
37953793
err.span_label(assigned_span, format!("first assignment to {place_description}"));
37963794
}
37973795
if let Some(decl) = local_decl
3798-
&& let Some(name) = local_name
37993796
&& decl.can_be_made_mutable()
38003797
{
3801-
err.span_suggestion(
3802-
decl.source_info.span,
3798+
err.span_suggestion_verbose(
3799+
decl.source_info.span.shrink_to_lo(),
38033800
"consider making this binding mutable",
3804-
format!("mut {name}"),
3801+
"mut ".to_string(),
38053802
Applicability::MachineApplicable,
38063803
);
38073804
if !from_arg
@@ -3813,10 +3810,10 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
38133810
}))
38143811
)
38153812
{
3816-
err.span_suggestion(
3817-
decl.source_info.span,
3813+
err.span_suggestion_verbose(
3814+
decl.source_info.span.shrink_to_lo(),
38183815
"to modify the original value, take a borrow instead",
3819-
format!("ref mut {name}"),
3816+
"ref mut ".to_string(),
38203817
Applicability::MaybeIncorrect,
38213818
);
38223819
}

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+16-13
Original file line numberDiff line numberDiff line change
@@ -408,21 +408,21 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
408408
fn_decl.implicit_self,
409409
hir::ImplicitSelfKind::RefImm | hir::ImplicitSelfKind::RefMut
410410
) {
411-
err.span_suggestion(
412-
upvar_ident.span,
411+
err.span_suggestion_verbose(
412+
upvar_ident.span.shrink_to_lo(),
413413
"consider changing this to be mutable",
414-
format!("mut {}", upvar_ident.name),
414+
"mut ",
415415
Applicability::MachineApplicable,
416416
);
417417
break;
418418
}
419419
}
420420
}
421421
} else {
422-
err.span_suggestion(
423-
upvar_ident.span,
422+
err.span_suggestion_verbose(
423+
upvar_ident.span.shrink_to_lo(),
424424
"consider changing this to be mutable",
425-
format!("mut {}", upvar_ident.name),
425+
"mut ",
426426
Applicability::MachineApplicable,
427427
);
428428
}
@@ -449,8 +449,8 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
449449
.is_ok_and(|snippet| snippet.starts_with("&mut ")) =>
450450
{
451451
err.span_label(span, format!("cannot {act}"));
452-
err.span_suggestion(
453-
span,
452+
err.span_suggestion_verbose(
453+
span.with_hi(span.lo() + BytePos(5)),
454454
"try removing `&mut` here",
455455
"",
456456
Applicability::MaybeIncorrect,
@@ -755,13 +755,16 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
755755
pat: hir::Pat { kind: hir::PatKind::Ref(_, _), .. },
756756
..
757757
}) = node
758-
&& let Ok(name) =
759-
self.infcx.tcx.sess.source_map().span_to_snippet(local_decl.source_info.span)
760758
{
761-
err.span_suggestion(
762-
pat_span,
759+
err.multipart_suggestion(
763760
"consider changing this to be mutable",
764-
format!("&(mut {name})"),
761+
vec![
762+
(pat_span.until(local_decl.source_info.span), "&(mut ".to_string()),
763+
(
764+
local_decl.source_info.span.shrink_to_hi().with_hi(pat_span.hi()),
765+
")".to_string(),
766+
),
767+
],
765768
Applicability::MachineApplicable,
766769
);
767770
return;

compiler/rustc_errors/src/emitter.rs

+20-3
Original file line numberDiff line numberDiff line change
@@ -2273,9 +2273,26 @@ impl HumanEmitter {
22732273
&normalize_whitespace(last_line),
22742274
Style::NoStyle,
22752275
);
2276-
buffer.puts(*row_num, 0, &self.maybe_anonymized(line_num), Style::LineNumber);
2277-
buffer.puts(*row_num, max_line_num_len + 1, "+ ", Style::Addition);
2278-
buffer.append(*row_num, &normalize_whitespace(line_to_add), Style::NoStyle);
2276+
if !line_to_add.trim().is_empty() {
2277+
// Check if after the removal, the line is left with only whitespace. If so, we
2278+
// will not show an "addition" line, as removing the whole line is what the user
2279+
// would really want.
2280+
// For example, for the following:
2281+
// |
2282+
// 2 - .await
2283+
// 2 + (note the left over whitepsace)
2284+
// |
2285+
// We really want
2286+
// |
2287+
// 2 - .await
2288+
// |
2289+
// *row_num -= 1;
2290+
buffer.puts(*row_num, 0, &self.maybe_anonymized(line_num), Style::LineNumber);
2291+
buffer.puts(*row_num, max_line_num_len + 1, "+ ", Style::Addition);
2292+
buffer.append(*row_num, &normalize_whitespace(line_to_add), Style::NoStyle);
2293+
} else {
2294+
*row_num -= 1;
2295+
}
22792296
} else {
22802297
*row_num -= 2;
22812298
}

compiler/rustc_hir_analysis/src/errors.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -453,12 +453,11 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for MissingTypeParams {
453453
} else {
454454
// The user wrote `Iterator`, so we don't have a type we can suggest, but at
455455
// least we can clue them to the correct syntax `Iterator<Type>`.
456-
err.span_suggestion(
457-
self.span,
456+
err.span_suggestion_verbose(
457+
self.span.shrink_to_hi(),
458458
fluent::hir_analysis_suggestion,
459459
format!(
460-
"{}<{}>",
461-
snippet,
460+
"<{}>",
462461
self.missing_type_params
463462
.iter()
464463
.map(|n| n.to_string())

compiler/rustc_hir_typeck/src/expr.rs

+26-18
Original file line numberDiff line numberDiff line change
@@ -2551,10 +2551,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
25512551

25522552
match *base_ty.peel_refs().kind() {
25532553
ty::Array(_, len) => {
2554-
self.maybe_suggest_array_indexing(&mut err, expr, base, ident, len);
2554+
self.maybe_suggest_array_indexing(&mut err, base, ident, len);
25552555
}
25562556
ty::RawPtr(..) => {
2557-
self.suggest_first_deref_field(&mut err, expr, base, ident);
2557+
self.suggest_first_deref_field(&mut err, base, ident);
25582558
}
25592559
ty::Param(param_ty) => {
25602560
err.span_label(ident.span, "unknown field");
@@ -2721,40 +2721,48 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
27212721
fn maybe_suggest_array_indexing(
27222722
&self,
27232723
err: &mut Diag<'_>,
2724-
expr: &hir::Expr<'_>,
27252724
base: &hir::Expr<'_>,
27262725
field: Ident,
27272726
len: ty::Const<'tcx>,
27282727
) {
27292728
err.span_label(field.span, "unknown field");
27302729
if let (Some(len), Ok(user_index)) =
27312730
(len.try_eval_target_usize(self.tcx, self.param_env), field.as_str().parse::<u64>())
2732-
&& let Ok(base) = self.tcx.sess.source_map().span_to_snippet(base.span)
27332731
{
27342732
let help = "instead of using tuple indexing, use array indexing";
2735-
let suggestion = format!("{base}[{field}]");
27362733
let applicability = if len < user_index {
27372734
Applicability::MachineApplicable
27382735
} else {
27392736
Applicability::MaybeIncorrect
27402737
};
2741-
err.span_suggestion(expr.span, help, suggestion, applicability);
2738+
err.multipart_suggestion(
2739+
help,
2740+
vec![
2741+
(base.span.between(field.span), "[".to_string()),
2742+
(field.span.shrink_to_hi(), "]".to_string()),
2743+
],
2744+
applicability,
2745+
);
27422746
}
27432747
}
27442748

2745-
fn suggest_first_deref_field(
2746-
&self,
2747-
err: &mut Diag<'_>,
2748-
expr: &hir::Expr<'_>,
2749-
base: &hir::Expr<'_>,
2750-
field: Ident,
2751-
) {
2749+
fn suggest_first_deref_field(&self, err: &mut Diag<'_>, base: &hir::Expr<'_>, field: Ident) {
27522750
err.span_label(field.span, "unknown field");
2753-
if let Ok(base) = self.tcx.sess.source_map().span_to_snippet(base.span) {
2754-
let msg = format!("`{base}` is a raw pointer; try dereferencing it");
2755-
let suggestion = format!("(*{base}).{field}");
2756-
err.span_suggestion(expr.span, msg, suggestion, Applicability::MaybeIncorrect);
2757-
}
2751+
let val = if let Ok(base) = self.tcx.sess.source_map().span_to_snippet(base.span)
2752+
&& base.len() < 20
2753+
{
2754+
format!("`{base}`")
2755+
} else {
2756+
"the value".to_string()
2757+
};
2758+
err.multipart_suggestion(
2759+
format!("{val} is a raw pointer; try dereferencing it"),
2760+
vec![
2761+
(base.span.shrink_to_lo(), "(*".to_string()),
2762+
(base.span.shrink_to_hi(), ")".to_string()),
2763+
],
2764+
Applicability::MaybeIncorrect,
2765+
);
27582766
}
27592767

27602768
fn no_such_field_err(&self, field: Ident, expr_t: Ty<'tcx>, id: HirId) -> Diag<'_> {

compiler/rustc_hir_typeck/src/pat.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -2499,7 +2499,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
24992499
.any(|(ty, _)| matches!(ty.kind(), ty::Slice(..) | ty::Array(..)))
25002500
&& let Some(span) = ti.span
25012501
&& let Some(_) = ti.origin_expr
2502-
&& let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span)
25032502
{
25042503
let resolved_ty = self.resolve_vars_if_possible(ti.expected);
25052504
let (is_slice_or_array_or_vector, resolved_ty) =
@@ -2510,10 +2509,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
25102509
|| self.tcx.is_diagnostic_item(sym::Result, adt_def.did()) =>
25112510
{
25122511
// Slicing won't work here, but `.as_deref()` might (issue #91328).
2513-
err.span_suggestion(
2514-
span,
2512+
err.span_suggestion_verbose(
2513+
span.shrink_to_hi(),
25152514
"consider using `as_deref` here",
2516-
format!("{snippet}.as_deref()"),
2515+
".as_deref()",
25172516
Applicability::MaybeIncorrect,
25182517
);
25192518
}
@@ -2522,10 +2521,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
25222521

25232522
let is_top_level = current_depth <= 1;
25242523
if is_slice_or_array_or_vector && is_top_level {
2525-
err.span_suggestion(
2526-
span,
2524+
err.span_suggestion_verbose(
2525+
span.shrink_to_hi(),
25272526
"consider slicing here",
2528-
format!("{snippet}[..]"),
2527+
"[..]",
25292528
Applicability::MachineApplicable,
25302529
);
25312530
}

compiler/rustc_infer/src/infer/error_reporting/note_and_explain.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
5252
) = tcx.sess.source_map().span_to_snippet(sp) =>
5353
{
5454
if snippet.chars().all(|c| c.is_digit(10) || c == '-' || c == '_') {
55-
diag.span_suggestion(
56-
sp,
55+
diag.span_suggestion_verbose(
56+
sp.shrink_to_hi(),
5757
"use a float literal",
58-
format!("{snippet}.0"),
58+
".0",
5959
MachineApplicable,
6060
);
6161
}

src/librustdoc/passes/lint/bare_urls.rs

+17-19
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,22 @@ pub(super) fn visit_item(cx: &DocContext<'_>, item: &Item) {
1919
};
2020
let dox = item.doc_value();
2121
if !dox.is_empty() {
22-
let report_diag =
23-
|cx: &DocContext<'_>, msg: &'static str, url: &str, range: Range<usize>| {
24-
let sp =
25-
source_span_for_markdown_range(cx.tcx, &dox, &range, &item.attrs.doc_strings)
26-
.unwrap_or_else(|| item.attr_span(cx.tcx));
27-
cx.tcx.node_span_lint(crate::lint::BARE_URLS, hir_id, sp, |lint| {
28-
lint.primary_message(msg)
29-
.note("bare URLs are not automatically turned into clickable links")
30-
.span_suggestion(
31-
sp,
32-
"use an automatic link instead",
33-
format!("<{url}>"),
34-
Applicability::MachineApplicable,
35-
);
36-
});
37-
};
22+
let report_diag = |cx: &DocContext<'_>, msg: &'static str, range: Range<usize>| {
23+
let sp = source_span_for_markdown_range(cx.tcx, &dox, &range, &item.attrs.doc_strings)
24+
.unwrap_or_else(|| item.attr_span(cx.tcx));
25+
cx.tcx.node_span_lint(crate::lint::BARE_URLS, hir_id, sp, |lint| {
26+
lint.primary_message(msg)
27+
.note("bare URLs are not automatically turned into clickable links")
28+
.multipart_suggestion(
29+
"use an automatic link instead",
30+
vec![
31+
(sp.shrink_to_lo(), "<".to_string()),
32+
(sp.shrink_to_hi(), ">".to_string()),
33+
],
34+
Applicability::MachineApplicable,
35+
);
36+
});
37+
};
3838

3939
let mut p = Parser::new_ext(&dox, main_body_opts()).into_offset_iter();
4040

@@ -74,17 +74,15 @@ fn find_raw_urls(
7474
cx: &DocContext<'_>,
7575
text: &str,
7676
range: Range<usize>,
77-
f: &impl Fn(&DocContext<'_>, &'static str, &str, Range<usize>),
77+
f: &impl Fn(&DocContext<'_>, &'static str, Range<usize>),
7878
) {
7979
trace!("looking for raw urls in {text}");
8080
// For now, we only check "full" URLs (meaning, starting with "http://" or "https://").
8181
for match_ in URL_REGEX.find_iter(text) {
82-
let url = match_.as_str();
8382
let url_range = match_.range();
8483
f(
8584
cx,
8685
"this URL is not a hyperlink",
87-
url,
8886
Range { start: range.start + url_range.start, end: range.start + url_range.end },
8987
);
9088
}

src/tools/clippy/tests/ui/dbg_macro/dbg_macro.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ LL | dbg!();
8686
help: remove the invocation before committing it to a version control system
8787
|
8888
LL - dbg!();
89-
LL +
9089
|
9190

9291
error: the `dbg!` macro is intended as a debugging tool
@@ -146,7 +145,6 @@ LL | expand_to_dbg!();
146145
help: remove the invocation before committing it to a version control system
147146
|
148147
LL - dbg!();
149-
LL +
150148
|
151149

152150
error: the `dbg!` macro is intended as a debugging tool

src/tools/clippy/tests/ui/dbg_macro/dbg_macro_unfixable.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ LL | dbg!();
99
help: remove the invocation before committing it to a version control system
1010
|
1111
LL - dbg!();
12-
LL +
1312
|
1413

1514
error: the `dbg!` macro is intended as a debugging tool

0 commit comments

Comments
 (0)