Skip to content

Commit a1eda8f

Browse files
committed
If suggestion would leave an empty line, delete it
1 parent cdd4ff8 commit a1eda8f

13 files changed

+67
-12
lines changed

compiler/rustc_errors/src/json.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -479,14 +479,24 @@ impl DiagnosticSpan {
479479
}
480480

481481
fn from_span_full(
482-
span: Span,
482+
mut span: Span,
483483
is_primary: bool,
484484
label: Option<String>,
485485
suggestion: Option<(&String, Applicability)>,
486486
mut backtrace: impl Iterator<Item = ExpnData>,
487487
je: &JsonEmitter,
488488
) -> DiagnosticSpan {
489489
let start = je.sm.lookup_char_pos(span.lo());
490+
// If this goes from the start of a line to the end and the replacement
491+
// is an empty string, increase the length to include the newline so we don't
492+
// leave an empty line
493+
if start.col.0 == 0
494+
&& suggestion.map_or(false, |(s, _)| s.is_empty())
495+
&& let Ok(after) = je.sm.span_to_next_source(span)
496+
&& after.starts_with('\n')
497+
{
498+
span = span.with_hi(span.hi() + rustc_span::BytePos(1));
499+
}
490500
let end = je.sm.lookup_char_pos(span.hi());
491501
let backtrace_step = backtrace.next().map(|bt| {
492502
let call_site = Self::from_span_full(bt.call_site, false, None, None, backtrace, je);

tests/ui/associated-types/impl-wf-cycle-6.fixed

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ impl Grault for () {
1919

2020
impl<T: Grault> Grault for (T,)
2121
//~^ ERROR overflow evaluating the requirement `<(T,) as Grault>::A == _`
22-
2322
{
2423
type A = ();
2524
type B = bool;

tests/ui/generics/generic-no-mangle.fixed

-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
#![deny(no_mangle_generic_items)]
44

5-
65
pub fn foo<T>() {} //~ ERROR functions generic over types or consts must be mangled
76

8-
97
pub extern "C" fn bar<T>() {} //~ ERROR functions generic over types or consts must be mangled
108

119
#[no_mangle]

tests/ui/imports/issue-52891.fixed

-2
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@ use issue_52891::{l,
2727
use issue_52891::a::inner;
2828
use issue_52891::b::inner as other_inner; //~ ERROR `inner` is defined multiple times
2929

30-
3130
//~^ ERROR `issue_52891` is defined multiple times
3231

33-
3432
#[macro_use]
3533
use issue_52891::n; //~ ERROR `n` is defined multiple times
3634

tests/ui/imports/unused-import-issue-87973.fixed

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#![deny(unused_imports)]
33

44
// Check that attributes get removed too. See #87973.
5-
65
//~^ ERROR unused import
76

87
fn main() {}

tests/ui/lazy-type-alias/leading-where-clause.fixed

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
// Check that we *reject* leading where-clauses on lazy type aliases.
77

88
type Alias<T>
9-
109
= T where String: From<T>;
1110
//~^^^ ERROR where clauses are not allowed before the type for type aliases
1211

tests/ui/lint/suggestions.fixed

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
//~^ ERROR const items should never be `#[no_mangle]`
88
//~| HELP try a static value
99

10-
1110
//~^ HELP remove this attribute
1211
pub fn defiant<T>(_t: T) {}
1312
//~^ WARN functions generic over types or consts must be mangled
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// run-rustfix
2+
// check-pass
3+
4+
#![crate_type = "lib"]
5+
#![warn(unused_imports)]
6+
7+
//~^ WARN unused imports
8+
//~^ WARN unused import
9+
10+
//~^ WARN unused import
11+
//~| WARN unused import
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// run-rustfix
2+
// check-pass
3+
4+
#![crate_type = "lib"]
5+
#![warn(unused_imports)]
6+
7+
use std::time::{Duration, Instant};
8+
//~^ WARN unused imports
9+
use std::time::SystemTime;
10+
//~^ WARN unused import
11+
use std::time::SystemTimeError;use std::time::TryFromFloatSecsError;
12+
//~^ WARN unused import
13+
//~| WARN unused import
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
warning: unused imports: `Duration`, `Instant`
2+
--> $DIR/import_remove_line.rs:7:17
3+
|
4+
LL | use std::time::{Duration, Instant};
5+
| ^^^^^^^^ ^^^^^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/import_remove_line.rs:5:9
9+
|
10+
LL | #![warn(unused_imports)]
11+
| ^^^^^^^^^^^^^^
12+
13+
warning: unused import: `std::time::SystemTime`
14+
--> $DIR/import_remove_line.rs:9:5
15+
|
16+
LL | use std::time::SystemTime;
17+
| ^^^^^^^^^^^^^^^^^^^^^
18+
19+
warning: unused import: `std::time::SystemTimeError`
20+
--> $DIR/import_remove_line.rs:11:5
21+
|
22+
LL | use std::time::SystemTimeError;use std::time::TryFromFloatSecsError;
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
24+
25+
warning: unused import: `std::time::TryFromFloatSecsError`
26+
--> $DIR/import_remove_line.rs:11:36
27+
|
28+
LL | use std::time::SystemTimeError;use std::time::TryFromFloatSecsError;
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
30+
31+
warning: 4 warnings emitted
32+

tests/ui/resolve/resolve-conflict-import-vs-import.fixed

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#[allow(unused_imports)]
44
use std::mem::transmute;
5-
65
//~^ ERROR the name `transmute` is defined multiple times
76

87
fn main() {

tests/ui/rust-2018/extern-crate-idiomatic-in-2018.fixed

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#![deny(rust_2018_idioms)]
1010
#![allow(dead_code)]
1111

12-
1312
//~^ ERROR unused extern crate
1413

1514
// Shouldn't suggest changing to `use`, as `bar`

tests/ui/rust-2018/issue-54400-unused-extern-crate-attr-span.fixed

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
// The suggestion span should include the attribute.
1010

11-
1211
//~^ ERROR unused extern crate
1312

1413
fn main() {}

0 commit comments

Comments
 (0)