From 367126d49af3237eee24889191dfbb02dfc9320c Mon Sep 17 00:00:00 2001 From: clubby789 Date: Fri, 26 Jan 2024 15:32:59 +0000 Subject: [PATCH] If suggestion would leave an empty line, delete it --- compiler/rustc_errors/src/json.rs | 12 ++++++- .../clippy/tests/ui/derivable_impls.fixed | 8 ----- src/tools/clippy/tests/ui/empty_drop.fixed | 2 -- src/tools/clippy/tests/ui/must_use_unit.fixed | 3 -- .../ui/single_component_path_imports.fixed | 1 - .../ui/associated-types/impl-wf-cycle-6.fixed | 1 - tests/ui/generics/generic-no-mangle.fixed | 2 -- tests/ui/imports/issue-52891.fixed | 2 -- .../imports/unused-import-issue-87973.fixed | 1 - .../leading-where-clause.fixed | 2 -- tests/ui/lint/suggestions.fixed | 1 - tests/ui/lint/unused/import_remove_line.fixed | 11 +++++++ tests/ui/lint/unused/import_remove_line.rs | 13 ++++++++ .../ui/lint/unused/import_remove_line.stderr | 32 +++++++++++++++++++ .../resolve-conflict-import-vs-import.fixed | 1 - .../extern-crate-idiomatic-in-2018.fixed | 1 - ...-54400-unused-extern-crate-attr-span.fixed | 1 - 17 files changed, 67 insertions(+), 27 deletions(-) create mode 100644 tests/ui/lint/unused/import_remove_line.fixed create mode 100644 tests/ui/lint/unused/import_remove_line.rs create mode 100644 tests/ui/lint/unused/import_remove_line.stderr diff --git a/compiler/rustc_errors/src/json.rs b/compiler/rustc_errors/src/json.rs index bc1822f83fc..af82d8092c2 100644 --- a/compiler/rustc_errors/src/json.rs +++ b/compiler/rustc_errors/src/json.rs @@ -428,7 +428,7 @@ impl DiagnosticSpan { } fn from_span_full( - span: Span, + mut span: Span, is_primary: bool, label: Option, suggestion: Option<(&String, Applicability)>, @@ -436,6 +436,16 @@ impl DiagnosticSpan { je: &JsonEmitter, ) -> DiagnosticSpan { let start = je.sm.lookup_char_pos(span.lo()); + // If this goes from the start of a line to the end and the replacement + // is an empty string, increase the length to include the newline so we don't + // leave an empty line + if start.col.0 == 0 + && suggestion.map_or(false, |(s, _)| s.is_empty()) + && let Ok(after) = je.sm.span_to_next_source(span) + && after.starts_with('\n') + { + span = span.with_hi(span.hi() + rustc_span::BytePos(1)); + } let end = je.sm.lookup_char_pos(span.hi()); let backtrace_step = backtrace.next().map(|bt| { let call_site = Self::from_span_full(bt.call_site, false, None, None, backtrace, je); diff --git a/src/tools/clippy/tests/ui/derivable_impls.fixed b/src/tools/clippy/tests/ui/derivable_impls.fixed index 68c5a5c5ca4..c85f384fd6e 100644 --- a/src/tools/clippy/tests/ui/derivable_impls.fixed +++ b/src/tools/clippy/tests/ui/derivable_impls.fixed @@ -19,12 +19,10 @@ struct FooDefault<'a> { } - #[derive(Default)] struct TupleDefault(bool, i32, u64); - struct FooND1 { a: bool, } @@ -73,7 +71,6 @@ impl Default for FooNDVec { struct StrDefault<'a>(&'a str); - #[derive(Default)] struct AlreadyDerived(i32, bool); @@ -96,7 +93,6 @@ mac!(0); #[derive(Default)] struct Y(u32); - struct RustIssue26925 { a: Option, } @@ -132,12 +128,10 @@ struct WithoutSelfCurly { } - #[derive(Default)] struct WithoutSelfParan(bool); - // https://github.com/rust-lang/rust-clippy/issues/7655 pub struct SpecializedImpl2 { @@ -184,7 +178,6 @@ pub struct RepeatDefault1 { } - pub struct RepeatDefault2 { a: [i8; 33], } @@ -216,7 +209,6 @@ pub enum SimpleEnum { } - pub enum NonExhaustiveEnum { Foo, #[non_exhaustive] diff --git a/src/tools/clippy/tests/ui/empty_drop.fixed b/src/tools/clippy/tests/ui/empty_drop.fixed index 949d0d8b399..17cfdcdc9c6 100644 --- a/src/tools/clippy/tests/ui/empty_drop.fixed +++ b/src/tools/clippy/tests/ui/empty_drop.fixed @@ -5,7 +5,6 @@ struct Foo; - // shouldn't cause an error struct Bar; @@ -19,5 +18,4 @@ impl Drop for Bar { struct Baz; - fn main() {} diff --git a/src/tools/clippy/tests/ui/must_use_unit.fixed b/src/tools/clippy/tests/ui/must_use_unit.fixed index 75f91e66824..f255cb66652 100644 --- a/src/tools/clippy/tests/ui/must_use_unit.fixed +++ b/src/tools/clippy/tests/ui/must_use_unit.fixed @@ -6,13 +6,10 @@ extern crate proc_macros; use proc_macros::external; - pub fn must_use_default() {} - pub fn must_use_unit() -> () {} - pub fn must_use_with_note() {} fn main() { diff --git a/src/tools/clippy/tests/ui/single_component_path_imports.fixed b/src/tools/clippy/tests/ui/single_component_path_imports.fixed index fdff336c281..3e81bcd5e48 100644 --- a/src/tools/clippy/tests/ui/single_component_path_imports.fixed +++ b/src/tools/clippy/tests/ui/single_component_path_imports.fixed @@ -4,7 +4,6 @@ use core; - use serde as edres; pub use serde; diff --git a/tests/ui/associated-types/impl-wf-cycle-6.fixed b/tests/ui/associated-types/impl-wf-cycle-6.fixed index 45143be1e74..ce98b9c2f02 100644 --- a/tests/ui/associated-types/impl-wf-cycle-6.fixed +++ b/tests/ui/associated-types/impl-wf-cycle-6.fixed @@ -21,7 +21,6 @@ impl Grault for () { impl Grault for (T,) //~^ ERROR overflow evaluating the requirement `<(T,) as Grault>::A == _` - { type A = (); type B = bool; diff --git a/tests/ui/generics/generic-no-mangle.fixed b/tests/ui/generics/generic-no-mangle.fixed index f20ea0edaa6..69db712f9dc 100644 --- a/tests/ui/generics/generic-no-mangle.fixed +++ b/tests/ui/generics/generic-no-mangle.fixed @@ -2,10 +2,8 @@ #![allow(dead_code)] #![deny(no_mangle_generic_items)] - pub fn foo() {} //~ ERROR functions generic over types or consts must be mangled - pub extern "C" fn bar() {} //~ ERROR functions generic over types or consts must be mangled #[no_mangle] diff --git a/tests/ui/imports/issue-52891.fixed b/tests/ui/imports/issue-52891.fixed index 9faef1703ac..0382960db2f 100644 --- a/tests/ui/imports/issue-52891.fixed +++ b/tests/ui/imports/issue-52891.fixed @@ -27,10 +27,8 @@ use issue_52891::{l, use issue_52891::a::inner; use issue_52891::b::inner as other_inner; //~ ERROR `inner` is defined multiple times - //~^ ERROR `issue_52891` is defined multiple times - #[macro_use] use issue_52891::n; //~ ERROR `n` is defined multiple times diff --git a/tests/ui/imports/unused-import-issue-87973.fixed b/tests/ui/imports/unused-import-issue-87973.fixed index 96508fa3ea2..d1167d7d486 100644 --- a/tests/ui/imports/unused-import-issue-87973.fixed +++ b/tests/ui/imports/unused-import-issue-87973.fixed @@ -2,7 +2,6 @@ #![deny(unused_imports)] // Check that attributes get removed too. See #87973. - //~^ ERROR unused import fn main() {} diff --git a/tests/ui/lazy-type-alias/leading-where-clause.fixed b/tests/ui/lazy-type-alias/leading-where-clause.fixed index ca0ab7b5c7d..003eaa6c54e 100644 --- a/tests/ui/lazy-type-alias/leading-where-clause.fixed +++ b/tests/ui/lazy-type-alias/leading-where-clause.fixed @@ -7,11 +7,9 @@ // Check that we *reject* leading where-clauses on lazy type aliases. pub type Leading0 - = T where String: From; pub type Leading1 - = (T, U) where U: Copy, String: From; diff --git a/tests/ui/lint/suggestions.fixed b/tests/ui/lint/suggestions.fixed index b017438a8bd..66d097b121f 100644 --- a/tests/ui/lint/suggestions.fixed +++ b/tests/ui/lint/suggestions.fixed @@ -7,7 +7,6 @@ //~^ ERROR const items should never be `#[no_mangle]` //~| HELP try a static value - //~^ HELP remove this attribute pub fn defiant(_t: T) {} //~^ WARN functions generic over types or consts must be mangled diff --git a/tests/ui/lint/unused/import_remove_line.fixed b/tests/ui/lint/unused/import_remove_line.fixed new file mode 100644 index 00000000000..45876cd4ebc --- /dev/null +++ b/tests/ui/lint/unused/import_remove_line.fixed @@ -0,0 +1,11 @@ +//@ run-rustfix +//@ check-pass + +#![crate_type = "lib"] +#![warn(unused_imports)] + +//~^ WARN unused imports +//~^ WARN unused import + +//~^ WARN unused import +//~| WARN unused import diff --git a/tests/ui/lint/unused/import_remove_line.rs b/tests/ui/lint/unused/import_remove_line.rs new file mode 100644 index 00000000000..fd45f04eb80 --- /dev/null +++ b/tests/ui/lint/unused/import_remove_line.rs @@ -0,0 +1,13 @@ +//@ run-rustfix +//@ check-pass + +#![crate_type = "lib"] +#![warn(unused_imports)] + +use std::time::{Duration, Instant}; +//~^ WARN unused imports +use std::time::SystemTime; +//~^ WARN unused import +use std::time::SystemTimeError;use std::time::TryFromFloatSecsError; +//~^ WARN unused import +//~| WARN unused import diff --git a/tests/ui/lint/unused/import_remove_line.stderr b/tests/ui/lint/unused/import_remove_line.stderr new file mode 100644 index 00000000000..0e8c5de3558 --- /dev/null +++ b/tests/ui/lint/unused/import_remove_line.stderr @@ -0,0 +1,32 @@ +warning: unused imports: `Duration`, `Instant` + --> $DIR/import_remove_line.rs:7:17 + | +LL | use std::time::{Duration, Instant}; + | ^^^^^^^^ ^^^^^^^ + | +note: the lint level is defined here + --> $DIR/import_remove_line.rs:5:9 + | +LL | #![warn(unused_imports)] + | ^^^^^^^^^^^^^^ + +warning: unused import: `std::time::SystemTime` + --> $DIR/import_remove_line.rs:9:5 + | +LL | use std::time::SystemTime; + | ^^^^^^^^^^^^^^^^^^^^^ + +warning: unused import: `std::time::SystemTimeError` + --> $DIR/import_remove_line.rs:11:5 + | +LL | use std::time::SystemTimeError;use std::time::TryFromFloatSecsError; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + +warning: unused import: `std::time::TryFromFloatSecsError` + --> $DIR/import_remove_line.rs:11:36 + | +LL | use std::time::SystemTimeError;use std::time::TryFromFloatSecsError; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +warning: 4 warnings emitted + diff --git a/tests/ui/resolve/resolve-conflict-import-vs-import.fixed b/tests/ui/resolve/resolve-conflict-import-vs-import.fixed index 2ebf2a194b8..d981d629c46 100644 --- a/tests/ui/resolve/resolve-conflict-import-vs-import.fixed +++ b/tests/ui/resolve/resolve-conflict-import-vs-import.fixed @@ -2,7 +2,6 @@ #[allow(unused_imports)] use std::mem::transmute; - //~^ ERROR the name `transmute` is defined multiple times fn main() { diff --git a/tests/ui/rust-2018/extern-crate-idiomatic-in-2018.fixed b/tests/ui/rust-2018/extern-crate-idiomatic-in-2018.fixed index fcab56ac819..ca8422c03a3 100644 --- a/tests/ui/rust-2018/extern-crate-idiomatic-in-2018.fixed +++ b/tests/ui/rust-2018/extern-crate-idiomatic-in-2018.fixed @@ -9,7 +9,6 @@ #![deny(rust_2018_idioms)] #![allow(dead_code)] - //~^ ERROR unused extern crate // Shouldn't suggest changing to `use`, as `bar` diff --git a/tests/ui/rust-2018/issue-54400-unused-extern-crate-attr-span.fixed b/tests/ui/rust-2018/issue-54400-unused-extern-crate-attr-span.fixed index 2625cdc8b48..75b3918be1d 100644 --- a/tests/ui/rust-2018/issue-54400-unused-extern-crate-attr-span.fixed +++ b/tests/ui/rust-2018/issue-54400-unused-extern-crate-attr-span.fixed @@ -8,7 +8,6 @@ // The suggestion span should include the attribute. - //~^ ERROR unused extern crate fn main() {}