Skip to content

Commit

Permalink
Rollup merge of #73953 - JohnTitor:audit-hidden-sugg, r=estebank
Browse files Browse the repository at this point in the history
Audit hidden/short code suggestions

Should fix #73641.
Audit uses of `span_suggestion_short` and `tool_only_span_suggestion` (`span_suggestion_hidden` is already tested with `run-rustfix`). Leave some FIXMEs for futher improvements/fixes.
r? @estebank
  • Loading branch information
Manishearth authored Jul 7, 2020
2 parents 7942d9a + 84282fd commit e74ab50
Show file tree
Hide file tree
Showing 153 changed files with 1,801 additions and 313 deletions.
2 changes: 2 additions & 0 deletions src/librustc_builtin_macros/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,8 @@ impl<'a, 'b> Context<'a, 'b> {
("x", "LowerHex"),
("X", "UpperHex"),
] {
// FIXME: rustfix (`run-rustfix`) fails to apply suggestions.
// > "Cannot replace slice of data that was already replaced"
err.tool_only_span_suggestion(
sp,
&format!("use the `{}` trait", name),
Expand Down
7 changes: 5 additions & 2 deletions src/librustc_parse/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1228,10 +1228,13 @@ impl<'a> Parser<'a> {
if let Some(sp) = unmatched.unclosed_span {
err.span_label(sp, "unclosed delimiter");
}
// Backticks should be removed to apply suggestions.
let mut delim = delim.to_string();
delim.retain(|c| c != '`');
err.span_suggestion_short(
self.prev_token.span.shrink_to_hi(),
&format!("{} may belong here", delim.to_string()),
delim.to_string(),
&format!("`{}` may belong here", delim),
delim,
Applicability::MaybeIncorrect,
);
if unmatched.found_delim.is_none() {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_parse/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ impl<'a> Parser<'a> {
// misses a separator.
expect_err
.span_suggestion_short(
sp,
self.sess.source_map().next_point(sp),
&format!("missing `{}`", token_str),
token_str,
Applicability::MaybeIncorrect,
Expand Down
12 changes: 12 additions & 0 deletions src/test/ui/block-expression-remove-semicolon.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// run-rustfix

fn foo() -> i32 {
0
}

fn main() {
let _x: i32 = {
//~^ ERROR mismatched types
foo() //~ HELP consider removing this semicolon
};
}
6 changes: 4 additions & 2 deletions src/test/ui/block-expression-remove-semicolon.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// run-rustfix

fn foo() -> i32 {
0
0
}

fn main() {
let x: i32 = {
let _x: i32 = {
//~^ ERROR mismatched types
foo(); //~ HELP consider removing this semicolon
};
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/block-expression-remove-semicolon.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0308]: mismatched types
--> $DIR/block-expression-remove-semicolon.rs:6:18
--> $DIR/block-expression-remove-semicolon.rs:8:19
|
LL | let x: i32 = {
| __________________^
LL | let _x: i32 = {
| ___________________^
LL | |
LL | | foo();
| | - help: consider removing this semicolon
Expand Down
13 changes: 13 additions & 0 deletions src/test/ui/block-result/consider-removing-last-semi.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// run-rustfix

pub fn f() -> String { //~ ERROR mismatched types
0u8;
"bla".to_string()
}

pub fn g() -> String { //~ ERROR mismatched types
"this won't work".to_string();
"removeme".to_string()
}

fn main() {}
6 changes: 4 additions & 2 deletions src/test/ui/block-result/consider-removing-last-semi.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
fn f() -> String { //~ ERROR mismatched types
// run-rustfix

pub fn f() -> String { //~ ERROR mismatched types
0u8;
"bla".to_string();
}

fn g() -> String { //~ ERROR mismatched types
pub fn g() -> String { //~ ERROR mismatched types
"this won't work".to_string();
"removeme".to_string();
}
Expand Down
20 changes: 10 additions & 10 deletions src/test/ui/block-result/consider-removing-last-semi.stderr
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
error[E0308]: mismatched types
--> $DIR/consider-removing-last-semi.rs:1:11
--> $DIR/consider-removing-last-semi.rs:3:15
|
LL | fn f() -> String {
| - ^^^^^^ expected struct `std::string::String`, found `()`
| |
| implicitly returns `()` as its body has no tail or `return` expression
LL | pub fn f() -> String {
| - ^^^^^^ expected struct `std::string::String`, found `()`
| |
| implicitly returns `()` as its body has no tail or `return` expression
LL | 0u8;
LL | "bla".to_string();
| - help: consider removing this semicolon

error[E0308]: mismatched types
--> $DIR/consider-removing-last-semi.rs:6:11
--> $DIR/consider-removing-last-semi.rs:8:15
|
LL | fn g() -> String {
| - ^^^^^^ expected struct `std::string::String`, found `()`
| |
| implicitly returns `()` as its body has no tail or `return` expression
LL | pub fn g() -> String {
| - ^^^^^^ expected struct `std::string::String`, found `()`
| |
| implicitly returns `()` as its body has no tail or `return` expression
LL | "this won't work".to_string();
LL | "removeme".to_string();
| - help: consider removing this semicolon
Expand Down
16 changes: 16 additions & 0 deletions src/test/ui/coercion/coercion-missing-tail-expected-type.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// #41425 -- error message "mismatched types" has wrong types
// run-rustfix

fn plus_one(x: i32) -> i32 { //~ ERROR mismatched types
x + 1
}

fn foo() -> Result<u8, u64> { //~ ERROR mismatched types
Ok(1)
}

fn main() {
let x = plus_one(5);
let _ = foo();
println!("X = {}", x);
}
2 changes: 2 additions & 0 deletions src/test/ui/coercion/coercion-missing-tail-expected-type.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// #41425 -- error message "mismatched types" has wrong types
// run-rustfix

fn plus_one(x: i32) -> i32 { //~ ERROR mismatched types
x + 1;
Expand All @@ -10,5 +11,6 @@ fn foo() -> Result<u8, u64> { //~ ERROR mismatched types

fn main() {
let x = plus_one(5);
let _ = foo();
println!("X = {}", x);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0308]: mismatched types
--> $DIR/coercion-missing-tail-expected-type.rs:3:24
--> $DIR/coercion-missing-tail-expected-type.rs:4:24
|
LL | fn plus_one(x: i32) -> i32 {
| -------- ^^^ expected `i32`, found `()`
Expand All @@ -9,7 +9,7 @@ LL | x + 1;
| - help: consider removing this semicolon

error[E0308]: mismatched types
--> $DIR/coercion-missing-tail-expected-type.rs:7:13
--> $DIR/coercion-missing-tail-expected-type.rs:8:13
|
LL | fn foo() -> Result<u8, u64> {
| --- ^^^^^^^^^^^^^^^ expected enum `std::result::Result`, found `()`
Expand Down
15 changes: 15 additions & 0 deletions src/test/ui/const-generics/unused_braces.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// check-pass
// run-rustfix

#![allow(incomplete_features)]
#![warn(unused_braces)]

#![feature(const_generics)]

struct A<const N: usize>;

fn main() {
let _: A<7>; // ok
let _: A< 7 >; //~ WARN unnecessary braces
let _: A<{ 3 + 5 }>; // ok
}
4 changes: 3 additions & 1 deletion src/test/ui/const-generics/unused_braces.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// check-pass
// run-rustfix

#![allow(incomplete_features)]
#![warn(unused_braces)]

#![feature(const_generics)]
//~^ WARN the feature `const_generics` is incomplete

struct A<const N: usize>;

Expand Down
15 changes: 3 additions & 12 deletions src/test/ui/const-generics/unused_braces.stderr
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
warning: the feature `const_generics` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/unused_braces.rs:4:12
|
LL | #![feature(const_generics)]
| ^^^^^^^^^^^^^^
|
= note: `#[warn(incomplete_features)]` on by default
= note: see issue #44580 <https://github.com/rust-lang/rust/issues/44580> for more information

warning: unnecessary braces around const expression
--> $DIR/unused_braces.rs:11:14
--> $DIR/unused_braces.rs:13:14
|
LL | let _: A<{ 7 }>;
| ^^^^^ help: remove these braces
|
note: the lint level is defined here
--> $DIR/unused_braces.rs:2:9
--> $DIR/unused_braces.rs:5:9
|
LL | #![warn(unused_braces)]
| ^^^^^^^^^^^^^

warning: 2 warnings emitted
warning: 1 warning emitted

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// run-rustfix

fn main() {
let _x = !1; //~ ERROR cannot be used as a unary operator
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// run-rustfix

fn main() {
let x = ~1; //~ ERROR cannot be used as a unary operator
let _x = ~1; //~ ERROR cannot be used as a unary operator
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: `~` cannot be used as a unary operator
--> $DIR/issue-41679-tilde-bitwise-negation-attempt.rs:2:13
--> $DIR/issue-41679-tilde-bitwise-negation-attempt.rs:4:14
|
LL | let x = ~1;
| ^ help: use `!` to perform bitwise not
LL | let _x = ~1;
| ^ help: use `!` to perform bitwise not

error: aborting due to previous error

69 changes: 69 additions & 0 deletions src/test/ui/did_you_mean/issue-54109-without-witness.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// run-rustfix

// This test is to check if suggestions can be applied automatically.

#![allow(dead_code, unused_parens)]

fn main() {}

fn test_and() {
let a = true;
let b = false;

let _ = a && b; //~ ERROR `and` is not a logical operator
//~| ERROR `and` is not a logical operator

if a && b { //~ ERROR `and` is not a logical operator
//~| ERROR `and` is not a logical operator
println!("both");
}
}

fn test_or() {
let a = true;
let b = false;

let _ = a || b; //~ ERROR `or` is not a logical operator
//~| ERROR `or` is not a logical operator

if a || b { //~ ERROR `or` is not a logical operator
//~| ERROR `or` is not a logical operator
println!("both");
}
}

fn test_and_par() {
let a = true;
let b = false;
if (a && b) { //~ ERROR `and` is not a logical operator
//~| ERROR `and` is not a logical operator
println!("both");
}
}

fn test_or_par() {
let a = true;
let b = false;
if (a || b) { //~ ERROR `or` is not a logical operator
//~| ERROR `or` is not a logical operator
println!("both");
}
}

fn test_while_and() {
let a = true;
let b = false;
while a && b { //~ ERROR `and` is not a logical operator
//~| ERROR `and` is not a logical operator
println!("both");
}
}

fn test_while_or() {
let a = true;
let b = false;
while a || b { //~ ERROR `or` is not a logical operator
//~| ERROR `or` is not a logical operator
println!("both");
}
}
69 changes: 69 additions & 0 deletions src/test/ui/did_you_mean/issue-54109-without-witness.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// run-rustfix

// This test is to check if suggestions can be applied automatically.

#![allow(dead_code, unused_parens)]

fn main() {}

fn test_and() {
let a = true;
let b = false;

let _ = a and b; //~ ERROR `and` is not a logical operator
//~| ERROR `and` is not a logical operator

if a and b { //~ ERROR `and` is not a logical operator
//~| ERROR `and` is not a logical operator
println!("both");
}
}

fn test_or() {
let a = true;
let b = false;

let _ = a or b; //~ ERROR `or` is not a logical operator
//~| ERROR `or` is not a logical operator

if a or b { //~ ERROR `or` is not a logical operator
//~| ERROR `or` is not a logical operator
println!("both");
}
}

fn test_and_par() {
let a = true;
let b = false;
if (a and b) { //~ ERROR `and` is not a logical operator
//~| ERROR `and` is not a logical operator
println!("both");
}
}

fn test_or_par() {
let a = true;
let b = false;
if (a or b) { //~ ERROR `or` is not a logical operator
//~| ERROR `or` is not a logical operator
println!("both");
}
}

fn test_while_and() {
let a = true;
let b = false;
while a and b { //~ ERROR `and` is not a logical operator
//~| ERROR `and` is not a logical operator
println!("both");
}
}

fn test_while_or() {
let a = true;
let b = false;
while a or b { //~ ERROR `or` is not a logical operator
//~| ERROR `or` is not a logical operator
println!("both");
}
}
Loading

0 comments on commit e74ab50

Please sign in to comment.