Skip to content

Commit 716ea5f

Browse files
Fix use suggestion span
1 parent 4817259 commit 716ea5f

File tree

9 files changed

+22
-20
lines changed

9 files changed

+22
-20
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ use rustc_ast::visit::{self, Visitor};
55
use rustc_ast::{self as ast, Crate, ItemKind, ModKind, NodeId, Path, CRATE_NODE_ID};
66
use rustc_ast_pretty::pprust;
77
use rustc_data_structures::fx::FxHashSet;
8-
use rustc_errors::struct_span_err;
98
use rustc_errors::{
109
pluralize, Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, MultiSpan,
1110
};
11+
use rustc_errors::{struct_span_err, SuggestionStyle};
1212
use rustc_feature::BUILTIN_ATTRIBUTES;
1313
use rustc_hir::def::Namespace::{self, *};
1414
use rustc_hir::def::{self, CtorKind, CtorOf, DefKind, NonMacroAttrKind, PerNS};
@@ -2418,7 +2418,7 @@ fn show_candidates(
24182418
}
24192419

24202420
if let Some(span) = use_placement_span {
2421-
let add_use = match mode {
2421+
let (add_use, trailing) = match mode {
24222422
DiagnosticMode::Pattern => {
24232423
err.span_suggestions(
24242424
span,
@@ -2428,21 +2428,23 @@ fn show_candidates(
24282428
);
24292429
return;
24302430
}
2431-
DiagnosticMode::Import => "",
2432-
DiagnosticMode::Normal => "use ",
2431+
DiagnosticMode::Import => ("", ""),
2432+
DiagnosticMode::Normal => ("use ", ";\n"),
24332433
};
24342434
for candidate in &mut accessible_path_strings {
24352435
// produce an additional newline to separate the new use statement
24362436
// from the directly following item.
2437-
let additional_newline = if let FoundUse::Yes = found_use { "" } else { "\n" };
2438-
candidate.0 = format!("{add_use}{}{append};\n{additional_newline}", &candidate.0);
2437+
let additional_newline = if let FoundUse::No = found_use && let DiagnosticMode::Normal = mode { "\n" } else { "" };
2438+
candidate.0 =
2439+
format!("{add_use}{}{append}{trailing}{additional_newline}", &candidate.0);
24392440
}
24402441

2441-
err.span_suggestions(
2442+
err.span_suggestions_with_style(
24422443
span,
24432444
&msg,
24442445
accessible_path_strings.into_iter().map(|a| a.0),
24452446
Applicability::MaybeIncorrect,
2447+
SuggestionStyle::ShowAlways,
24462448
);
24472449
if let [first, .., last] = &path[..] {
24482450
let sp = first.ident.span.until(last.ident.span);

tests/ui/extenv/issue-55897.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ LL | use env;
3030
help: consider importing this module instead
3131
|
3232
LL | use std::env;
33-
| ~~~~~~~~~
33+
| ~~~~~~~~
3434

3535
error: cannot determine resolution for the macro `env`
3636
--> $DIR/issue-55897.rs:6:22

tests/ui/imports/bad-import-with-rename.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | use crate::D::B as _;
77
help: consider importing this type alias instead
88
|
99
LL | use A::B as _;
10-
| ~~~~~~~~~~
10+
| ~~~~~~~~~
1111

1212
error[E0432]: unresolved import `crate::D::B2`
1313
--> $DIR/bad-import-with-rename.rs:10:9
@@ -18,7 +18,7 @@ LL | use crate::D::B2;
1818
help: consider importing this type alias instead
1919
|
2020
LL | use A::B2;
21-
| ~~~~~~
21+
| ~~~~~
2222

2323
error: aborting due to 2 previous errors
2424

tests/ui/imports/issue-56125.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ LL | use empty::issue_56125;
77
help: consider importing one of these items instead
88
|
99
LL | use crate::m3::last_segment::issue_56125;
10-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1111
LL | use crate::m3::non_last_segment::non_last_segment::issue_56125;
12-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1313
LL | use issue_56125::issue_56125;
14-
| ~~~~~~~~~~~~~~~~~~~~~~~~~
14+
| ~~~~~~~~~~~~~~~~~~~~~~~~
1515
LL | use issue_56125::last_segment::issue_56125;
16-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1717
and 1 other candidate
1818

1919
error[E0659]: `issue_56125` is ambiguous

tests/ui/imports/issue-57015.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | use single_err::something;
77
help: consider importing this module instead
88
|
99
LL | use glob_ok::something;
10-
| ~~~~~~~~~~~~~~~~~~~
10+
| ~~~~~~~~~~~~~~~~~~
1111

1212
error: aborting due to previous error
1313

tests/ui/rfc-2126-extern-absolute-paths/not-allowed.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ LL | use alloc;
77
help: consider importing one of these items instead
88
|
99
LL | use core::alloc;
10-
| ~~~~~~~~~~~~
11-
LL | use std::alloc;
1210
| ~~~~~~~~~~~
11+
LL | use std::alloc;
12+
| ~~~~~~~~~~
1313

1414
error: aborting due to previous error
1515

tests/ui/simd/portable-intrinsics-arent-exposed.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ LL | use std::simd::intrinsics;
1515
help: consider importing this module instead
1616
|
1717
LL | use std::intrinsics;
18-
| ~~~~~~~~~~~~~~~~
18+
| ~~~~~~~~~~~~~~~
1919

2020
error: aborting due to 2 previous errors
2121

tests/ui/test-attrs/inaccessible-test-modules.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ LL | use test as y;
1313
help: consider importing this module instead
1414
|
1515
LL | use test::test as y;
16-
| ~~~~~~~~~~~~~~~~
16+
| ~~~~~~~~~~~~~~~
1717

1818
error: aborting due to 2 previous errors
1919

tests/ui/unresolved/unresolved-candidates.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | use Trait;
77
help: consider importing this trait instead
88
|
99
LL | use a::Trait;
10-
| ~~~~~~~~~
10+
| ~~~~~~~~
1111

1212
error[E0405]: cannot find trait `Trait` in this scope
1313
--> $DIR/unresolved-candidates.rs:10:10

0 commit comments

Comments
 (0)