Skip to content

Commit 87dd2e1

Browse files
committed
Use hidden suggestions for unused imports lint
1 parent 235523c commit 87dd2e1

15 files changed

+73
-35
lines changed

src/librustc/lint/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ impl BuiltinLintDiagnostics {
557557
}
558558
BuiltinLintDiagnostics::UnusedImports(message, replaces) => {
559559
if !replaces.is_empty() {
560-
db.multipart_suggestion(
560+
db.tool_only_multipart_suggestion(
561561
&message,
562562
replaces,
563563
Applicability::MachineApplicable,

src/librustc_errors/diagnostic.rs

+28-2
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,32 @@ impl Diagnostic {
250250
self
251251
}
252252

253+
/// Prints out a message with for a multipart suggestion without showing the suggested code.
254+
///
255+
/// This is intended to be used for suggestions that are obvious in what the changes need to
256+
/// be from the message, showing the span label inline would be visually unpleasant
257+
/// (marginally overlapping spans or multiline spans) and showing the snippet window wouldn't
258+
/// improve understandability.
259+
pub fn tool_only_multipart_suggestion(
260+
&mut self,
261+
msg: &str,
262+
suggestion: Vec<(Span, String)>,
263+
applicability: Applicability,
264+
) -> &mut Self {
265+
self.suggestions.push(CodeSuggestion {
266+
substitutions: vec![Substitution {
267+
parts: suggestion
268+
.into_iter()
269+
.map(|(span, snippet)| SubstitutionPart { snippet, span })
270+
.collect(),
271+
}],
272+
msg: msg.to_owned(),
273+
style: SuggestionStyle::CompletelyHidden,
274+
applicability,
275+
});
276+
self
277+
}
278+
253279
/// Prints out a message with a suggested edit of the code.
254280
///
255281
/// In case of short messages and a simple suggestion, rustc displays it as a label:
@@ -318,7 +344,7 @@ impl Diagnostic {
318344
}],
319345
msg: msg.to_owned(),
320346
style: SuggestionStyle::HideCodeInline,
321-
applicability: applicability,
347+
applicability,
322348
});
323349
self
324350
}
@@ -341,7 +367,7 @@ impl Diagnostic {
341367
}],
342368
msg: msg.to_owned(),
343369
style: SuggestionStyle::HideCodeInline,
344-
applicability: applicability,
370+
applicability,
345371
});
346372
self
347373
}

src/librustc_errors/diagnostic_builder.rs

+18
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,24 @@ impl<'a> DiagnosticBuilder<'a> {
205205
self
206206
}
207207

208+
pub fn tool_only_multipart_suggestion(
209+
&mut self,
210+
msg: &str,
211+
suggestion: Vec<(Span, String)>,
212+
applicability: Applicability,
213+
) -> &mut Self {
214+
if !self.allow_suggestions {
215+
return self
216+
}
217+
self.diagnostic.tool_only_multipart_suggestion(
218+
msg,
219+
suggestion,
220+
applicability,
221+
);
222+
self
223+
}
224+
225+
208226
pub fn span_suggestion(
209227
&mut self,
210228
sp: Span,

src/librustc_errors/emitter.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ impl Emitter for EmitterWriter {
4848
// don't display multiline suggestions as labels
4949
!sugg.substitutions[0].parts[0].snippet.contains('\n') &&
5050
// when this style is set we want the suggestion to be a message, not inline
51-
sugg.style != SuggestionStyle::HideCodeAlways
51+
sugg.style != SuggestionStyle::HideCodeAlways &&
52+
// trivial suggestion for tooling's sake, never shown
53+
sugg.style != SuggestionStyle::CompletelyHidden
5254
{
5355
let substitution = &sugg.substitutions[0].parts[0].snippet.trim();
5456
let msg = if substitution.len() == 0 || sugg.style.hide_inline() {

src/test/ui/bad/bad-lint-cap2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: unused import: `std::option`
22
--> $DIR/bad-lint-cap2.rs:6:5
33
|
44
LL | use std::option; //~ ERROR
5-
| ----^^^^^^^^^^^- help: remove the whole `use` item
5+
| ^^^^^^^^^^^
66
|
77
note: lint level defined here
88
--> $DIR/bad-lint-cap2.rs:4:9

src/test/ui/bad/bad-lint-cap3.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ warning: unused import: `std::option`
22
--> $DIR/bad-lint-cap3.rs:7:5
33
|
44
LL | use std::option; //~ WARN
5-
| ----^^^^^^^^^^^- help: remove the whole `use` item
5+
| ^^^^^^^^^^^
66
|
77
note: lint level defined here
88
--> $DIR/bad-lint-cap3.rs:4:9

src/test/ui/imports/unused.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: unused import: `super::f`
22
--> $DIR/unused.rs:7:24
33
|
44
LL | pub(super) use super::f; //~ ERROR unused
5-
| ---------------^^^^^^^^- help: remove the whole `use` item
5+
| ^^^^^^^^
66
|
77
note: lint level defined here
88
--> $DIR/unused.rs:1:9

src/test/ui/issues/issue-30730.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: unused import: `std::thread`
22
--> $DIR/issue-30730.rs:3:5
33
|
44
LL | use std::thread;
5-
| ----^^^^^^^^^^^- help: remove the whole `use` item
5+
| ^^^^^^^^^^^
66
|
77
note: lint level defined here
88
--> $DIR/issue-30730.rs:2:9

src/test/ui/lint/lint-directives-on-use-items-issue-10534.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: unused import: `a::x`
22
--> $DIR/lint-directives-on-use-items-issue-10534.rs:12:9
33
|
44
LL | use a::x; //~ ERROR: unused import
5-
| ----^^^^- help: remove the whole `use` item
5+
| ^^^^
66
|
77
note: lint level defined here
88
--> $DIR/lint-directives-on-use-items-issue-10534.rs:1:9
@@ -14,7 +14,7 @@ error: unused import: `a::y`
1414
--> $DIR/lint-directives-on-use-items-issue-10534.rs:21:9
1515
|
1616
LL | use a::y; //~ ERROR: unused import
17-
| ----^^^^- help: remove the whole `use` item
17+
| ^^^^
1818
|
1919
note: lint level defined here
2020
--> $DIR/lint-directives-on-use-items-issue-10534.rs:20:12

src/test/ui/lint/lint-unused-imports.stderr

+7-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: unused import: `std::fmt::{}`
22
--> $DIR/lint-unused-imports.rs:8:5
33
|
44
LL | use std::fmt::{};
5-
| ----^^^^^^^^^^^^- help: remove the whole `use` item
5+
| ^^^^^^^^^^^^
66
|
77
note: lint level defined here
88
--> $DIR/lint-unused-imports.rs:1:9
@@ -14,39 +14,37 @@ error: unused imports: `None`, `Some`
1414
--> $DIR/lint-unused-imports.rs:12:27
1515
|
1616
LL | use std::option::Option::{Some, None};
17-
| --------------------------^^^^--^^^^-- help: remove the whole `use` item
17+
| ^^^^ ^^^^
1818

1919
error: unused import: `test::A`
2020
--> $DIR/lint-unused-imports.rs:15:5
2121
|
2222
LL | use test::A; //~ ERROR unused import: `test::A`
23-
| ----^^^^^^^- help: remove the whole `use` item
23+
| ^^^^^^^
2424

2525
error: unused import: `bar`
2626
--> $DIR/lint-unused-imports.rs:24:18
2727
|
2828
LL | use test2::{foo, bar}; //~ ERROR unused import: `bar`
29-
| --^^^
30-
| |
31-
| help: remove the unused import
29+
| ^^^
3230

3331
error: unused import: `foo::Square`
3432
--> $DIR/lint-unused-imports.rs:52:13
3533
|
3634
LL | use foo::Square; //~ ERROR unused import: `foo::Square`
37-
| ----^^^^^^^^^^^- help: remove the whole `use` item
35+
| ^^^^^^^^^^^
3836

3937
error: unused import: `self::g`
4038
--> $DIR/lint-unused-imports.rs:68:9
4139
|
4240
LL | use self::g; //~ ERROR unused import: `self::g`
43-
| ----^^^^^^^- help: remove the whole `use` item
41+
| ^^^^^^^
4442

4543
error: unused import: `test2::foo`
4644
--> $DIR/lint-unused-imports.rs:77:9
4745
|
4846
LL | use test2::foo; //~ ERROR unused import: `test2::foo`
49-
| ----^^^^^^^^^^- help: remove the whole `use` item
47+
| ^^^^^^^^^^
5048

5149
error: unused import: `test::B2`
5250
--> $DIR/lint-unused-imports.rs:20:5

src/test/ui/lint/lints-in-foreign-macros.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ warning: unused import: `std::string::ToString`
22
--> $DIR/lints-in-foreign-macros.rs:11:16
33
|
44
LL | () => {use std::string::ToString;} //~ WARN: unused import
5-
| ----^^^^^^^^^^^^^^^^^^^^^- help: remove the whole `use` item
5+
| ^^^^^^^^^^^^^^^^^^^^^
66
...
77
LL | mod a { foo!(); }
88
| ------- in this macro invocation
@@ -17,13 +17,13 @@ warning: unused import: `std::string::ToString`
1717
--> $DIR/lints-in-foreign-macros.rs:16:18
1818
|
1919
LL | mod c { baz!(use std::string::ToString;); } //~ WARN: unused import
20-
| ----^^^^^^^^^^^^^^^^^^^^^- help: remove the whole `use` item
20+
| ^^^^^^^^^^^^^^^^^^^^^
2121

2222
warning: unused import: `std::string::ToString`
2323
--> $DIR/lints-in-foreign-macros.rs:17:19
2424
|
2525
LL | mod d { baz2!(use std::string::ToString;); } //~ WARN: unused import
26-
| ----^^^^^^^^^^^^^^^^^^^^^- help: remove the whole `use` item
26+
| ^^^^^^^^^^^^^^^^^^^^^
2727

2828
warning: missing documentation for crate
2929
--> $DIR/lints-in-foreign-macros.rs:4:1

src/test/ui/rfc-2166-underscore-imports/basic.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ warning: unused import: `m::Tr1 as _`
22
--> $DIR/basic.rs:26:9
33
|
44
LL | use m::Tr1 as _; //~ WARN unused import
5-
| ----^^^^^^^^^^^- help: remove the whole `use` item
5+
| ^^^^^^^^^^^
66
|
77
note: lint level defined here
88
--> $DIR/basic.rs:4:9
@@ -14,5 +14,5 @@ warning: unused import: `S as _`
1414
--> $DIR/basic.rs:27:9
1515
|
1616
LL | use S as _; //~ WARN unused import
17-
| ----^^^^^^- help: remove the whole `use` item
17+
| ^^^^^^
1818

src/test/ui/rfc-2166-underscore-imports/unused-2018.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: unused import: `core::any`
22
--> $DIR/unused-2018.rs:6:9
33
|
44
LL | use core::any; //~ ERROR unused import: `core::any`
5-
| ----^^^^^^^^^- help: remove the whole `use` item
5+
| ^^^^^^^^^
66
|
77
note: lint level defined here
88
--> $DIR/unused-2018.rs:3:9
@@ -14,7 +14,7 @@ error: unused import: `core`
1414
--> $DIR/unused-2018.rs:10:9
1515
|
1616
LL | use core; //~ ERROR unused import: `core`
17-
| ----^^^^- help: remove the whole `use` item
17+
| ^^^^
1818

1919
error: aborting due to 2 previous errors
2020

src/test/ui/span/multispan-import-lint.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,4 @@ note: lint level defined here
1010
LL | #![warn(unused)]
1111
| ^^^^^^
1212
= note: #[warn(unused_imports)] implied by #[warn(unused)]
13-
help: remove the unused imports
14-
|
15-
LL | use std::cmp::{min};
16-
| -- --
1713

src/test/ui/use/use-nested-groups-unused-imports.stderr

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: unused imports: `*`, `Foo`, `baz::{}`, `foobar::*`
22
--> $DIR/use-nested-groups-unused-imports.rs:16:11
33
|
44
LL | use foo::{Foo, bar::{baz::{}, foobar::*}, *};
5-
| ----------^^^--------^^^^^^^--^^^^^^^^^---^-- help: remove the whole `use` item
5+
| ^^^ ^^^^^^^ ^^^^^^^^^ ^
66
|
77
note: lint level defined here
88
--> $DIR/use-nested-groups-unused-imports.rs:3:9
@@ -14,15 +14,13 @@ error: unused import: `*`
1414
--> $DIR/use-nested-groups-unused-imports.rs:18:24
1515
|
1616
LL | use foo::bar::baz::{*, *};
17-
| --^
18-
| |
19-
| help: remove the unused import
17+
| ^
2018

2119
error: unused import: `foo::{}`
2220
--> $DIR/use-nested-groups-unused-imports.rs:20:5
2321
|
2422
LL | use foo::{};
25-
| ----^^^^^^^- help: remove the whole `use` item
23+
| ^^^^^^^
2624

2725
error: aborting due to 3 previous errors
2826

0 commit comments

Comments
 (0)