Skip to content

Commit e74ab50

Browse files
authored
Rollup merge of #73953 - JohnTitor:audit-hidden-sugg, r=estebank
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
2 parents 7942d9a + 84282fd commit e74ab50

File tree

153 files changed

+1801
-313
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

153 files changed

+1801
-313
lines changed

src/librustc_builtin_macros/format.rs

+2
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,8 @@ impl<'a, 'b> Context<'a, 'b> {
280280
("x", "LowerHex"),
281281
("X", "UpperHex"),
282282
] {
283+
// FIXME: rustfix (`run-rustfix`) fails to apply suggestions.
284+
// > "Cannot replace slice of data that was already replaced"
283285
err.tool_only_span_suggestion(
284286
sp,
285287
&format!("use the `{}` trait", name),

src/librustc_parse/parser/diagnostics.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1228,10 +1228,13 @@ impl<'a> Parser<'a> {
12281228
if let Some(sp) = unmatched.unclosed_span {
12291229
err.span_label(sp, "unclosed delimiter");
12301230
}
1231+
// Backticks should be removed to apply suggestions.
1232+
let mut delim = delim.to_string();
1233+
delim.retain(|c| c != '`');
12311234
err.span_suggestion_short(
12321235
self.prev_token.span.shrink_to_hi(),
1233-
&format!("{} may belong here", delim.to_string()),
1234-
delim.to_string(),
1236+
&format!("`{}` may belong here", delim),
1237+
delim,
12351238
Applicability::MaybeIncorrect,
12361239
);
12371240
if unmatched.found_delim.is_none() {

src/librustc_parse/parser/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ impl<'a> Parser<'a> {
699699
// misses a separator.
700700
expect_err
701701
.span_suggestion_short(
702-
sp,
702+
self.sess.source_map().next_point(sp),
703703
&format!("missing `{}`", token_str),
704704
token_str,
705705
Applicability::MaybeIncorrect,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// run-rustfix
2+
3+
fn foo() -> i32 {
4+
0
5+
}
6+
7+
fn main() {
8+
let _x: i32 = {
9+
//~^ ERROR mismatched types
10+
foo() //~ HELP consider removing this semicolon
11+
};
12+
}

src/test/ui/block-expression-remove-semicolon.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
// run-rustfix
2+
13
fn foo() -> i32 {
2-
0
4+
0
35
}
46

57
fn main() {
6-
let x: i32 = {
8+
let _x: i32 = {
79
//~^ ERROR mismatched types
810
foo(); //~ HELP consider removing this semicolon
911
};

src/test/ui/block-expression-remove-semicolon.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0308]: mismatched types
2-
--> $DIR/block-expression-remove-semicolon.rs:6:18
2+
--> $DIR/block-expression-remove-semicolon.rs:8:19
33
|
4-
LL | let x: i32 = {
5-
| __________________^
4+
LL | let _x: i32 = {
5+
| ___________________^
66
LL | |
77
LL | | foo();
88
| | - help: consider removing this semicolon
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// run-rustfix
2+
3+
pub fn f() -> String { //~ ERROR mismatched types
4+
0u8;
5+
"bla".to_string()
6+
}
7+
8+
pub fn g() -> String { //~ ERROR mismatched types
9+
"this won't work".to_string();
10+
"removeme".to_string()
11+
}
12+
13+
fn main() {}

src/test/ui/block-result/consider-removing-last-semi.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
fn f() -> String { //~ ERROR mismatched types
1+
// run-rustfix
2+
3+
pub fn f() -> String { //~ ERROR mismatched types
24
0u8;
35
"bla".to_string();
46
}
57

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

src/test/ui/block-result/consider-removing-last-semi.stderr

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
error[E0308]: mismatched types
2-
--> $DIR/consider-removing-last-semi.rs:1:11
2+
--> $DIR/consider-removing-last-semi.rs:3:15
33
|
4-
LL | fn f() -> String {
5-
| - ^^^^^^ expected struct `std::string::String`, found `()`
6-
| |
7-
| implicitly returns `()` as its body has no tail or `return` expression
4+
LL | pub fn f() -> String {
5+
| - ^^^^^^ expected struct `std::string::String`, found `()`
6+
| |
7+
| implicitly returns `()` as its body has no tail or `return` expression
88
LL | 0u8;
99
LL | "bla".to_string();
1010
| - help: consider removing this semicolon
1111

1212
error[E0308]: mismatched types
13-
--> $DIR/consider-removing-last-semi.rs:6:11
13+
--> $DIR/consider-removing-last-semi.rs:8:15
1414
|
15-
LL | fn g() -> String {
16-
| - ^^^^^^ expected struct `std::string::String`, found `()`
17-
| |
18-
| implicitly returns `()` as its body has no tail or `return` expression
15+
LL | pub fn g() -> String {
16+
| - ^^^^^^ expected struct `std::string::String`, found `()`
17+
| |
18+
| implicitly returns `()` as its body has no tail or `return` expression
1919
LL | "this won't work".to_string();
2020
LL | "removeme".to_string();
2121
| - help: consider removing this semicolon
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// #41425 -- error message "mismatched types" has wrong types
2+
// run-rustfix
3+
4+
fn plus_one(x: i32) -> i32 { //~ ERROR mismatched types
5+
x + 1
6+
}
7+
8+
fn foo() -> Result<u8, u64> { //~ ERROR mismatched types
9+
Ok(1)
10+
}
11+
12+
fn main() {
13+
let x = plus_one(5);
14+
let _ = foo();
15+
println!("X = {}", x);
16+
}

src/test/ui/coercion/coercion-missing-tail-expected-type.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// #41425 -- error message "mismatched types" has wrong types
2+
// run-rustfix
23

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

1112
fn main() {
1213
let x = plus_one(5);
14+
let _ = foo();
1315
println!("X = {}", x);
1416
}

src/test/ui/coercion/coercion-missing-tail-expected-type.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0308]: mismatched types
2-
--> $DIR/coercion-missing-tail-expected-type.rs:3:24
2+
--> $DIR/coercion-missing-tail-expected-type.rs:4:24
33
|
44
LL | fn plus_one(x: i32) -> i32 {
55
| -------- ^^^ expected `i32`, found `()`
@@ -9,7 +9,7 @@ LL | x + 1;
99
| - help: consider removing this semicolon
1010

1111
error[E0308]: mismatched types
12-
--> $DIR/coercion-missing-tail-expected-type.rs:7:13
12+
--> $DIR/coercion-missing-tail-expected-type.rs:8:13
1313
|
1414
LL | fn foo() -> Result<u8, u64> {
1515
| --- ^^^^^^^^^^^^^^^ expected enum `std::result::Result`, found `()`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// check-pass
2+
// run-rustfix
3+
4+
#![allow(incomplete_features)]
5+
#![warn(unused_braces)]
6+
7+
#![feature(const_generics)]
8+
9+
struct A<const N: usize>;
10+
11+
fn main() {
12+
let _: A<7>; // ok
13+
let _: A< 7 >; //~ WARN unnecessary braces
14+
let _: A<{ 3 + 5 }>; // ok
15+
}

src/test/ui/const-generics/unused_braces.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// check-pass
2+
// run-rustfix
3+
4+
#![allow(incomplete_features)]
25
#![warn(unused_braces)]
36

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

79
struct A<const N: usize>;
810

Original file line numberDiff line numberDiff line change
@@ -1,23 +1,14 @@
1-
warning: the feature `const_generics` is incomplete and may not be safe to use and/or cause compiler crashes
2-
--> $DIR/unused_braces.rs:4:12
3-
|
4-
LL | #![feature(const_generics)]
5-
| ^^^^^^^^^^^^^^
6-
|
7-
= note: `#[warn(incomplete_features)]` on by default
8-
= note: see issue #44580 <https://github.com/rust-lang/rust/issues/44580> for more information
9-
101
warning: unnecessary braces around const expression
11-
--> $DIR/unused_braces.rs:11:14
2+
--> $DIR/unused_braces.rs:13:14
123
|
134
LL | let _: A<{ 7 }>;
145
| ^^^^^ help: remove these braces
156
|
167
note: the lint level is defined here
17-
--> $DIR/unused_braces.rs:2:9
8+
--> $DIR/unused_braces.rs:5:9
189
|
1910
LL | #![warn(unused_braces)]
2011
| ^^^^^^^^^^^^^
2112

22-
warning: 2 warnings emitted
13+
warning: 1 warning emitted
2314

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

77
error: aborting due to previous error
88

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// run-rustfix
2+
3+
// This test is to check if suggestions can be applied automatically.
4+
5+
#![allow(dead_code, unused_parens)]
6+
7+
fn main() {}
8+
9+
fn test_and() {
10+
let a = true;
11+
let b = false;
12+
13+
let _ = a && b; //~ ERROR `and` is not a logical operator
14+
//~| ERROR `and` is not a logical operator
15+
16+
if a && b { //~ ERROR `and` is not a logical operator
17+
//~| ERROR `and` is not a logical operator
18+
println!("both");
19+
}
20+
}
21+
22+
fn test_or() {
23+
let a = true;
24+
let b = false;
25+
26+
let _ = a || b; //~ ERROR `or` is not a logical operator
27+
//~| ERROR `or` is not a logical operator
28+
29+
if a || b { //~ ERROR `or` is not a logical operator
30+
//~| ERROR `or` is not a logical operator
31+
println!("both");
32+
}
33+
}
34+
35+
fn test_and_par() {
36+
let a = true;
37+
let b = false;
38+
if (a && b) { //~ ERROR `and` is not a logical operator
39+
//~| ERROR `and` is not a logical operator
40+
println!("both");
41+
}
42+
}
43+
44+
fn test_or_par() {
45+
let a = true;
46+
let b = false;
47+
if (a || b) { //~ ERROR `or` is not a logical operator
48+
//~| ERROR `or` is not a logical operator
49+
println!("both");
50+
}
51+
}
52+
53+
fn test_while_and() {
54+
let a = true;
55+
let b = false;
56+
while a && b { //~ ERROR `and` is not a logical operator
57+
//~| ERROR `and` is not a logical operator
58+
println!("both");
59+
}
60+
}
61+
62+
fn test_while_or() {
63+
let a = true;
64+
let b = false;
65+
while a || b { //~ ERROR `or` is not a logical operator
66+
//~| ERROR `or` is not a logical operator
67+
println!("both");
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// run-rustfix
2+
3+
// This test is to check if suggestions can be applied automatically.
4+
5+
#![allow(dead_code, unused_parens)]
6+
7+
fn main() {}
8+
9+
fn test_and() {
10+
let a = true;
11+
let b = false;
12+
13+
let _ = a and b; //~ ERROR `and` is not a logical operator
14+
//~| ERROR `and` is not a logical operator
15+
16+
if a and b { //~ ERROR `and` is not a logical operator
17+
//~| ERROR `and` is not a logical operator
18+
println!("both");
19+
}
20+
}
21+
22+
fn test_or() {
23+
let a = true;
24+
let b = false;
25+
26+
let _ = a or b; //~ ERROR `or` is not a logical operator
27+
//~| ERROR `or` is not a logical operator
28+
29+
if a or b { //~ ERROR `or` is not a logical operator
30+
//~| ERROR `or` is not a logical operator
31+
println!("both");
32+
}
33+
}
34+
35+
fn test_and_par() {
36+
let a = true;
37+
let b = false;
38+
if (a and b) { //~ ERROR `and` is not a logical operator
39+
//~| ERROR `and` is not a logical operator
40+
println!("both");
41+
}
42+
}
43+
44+
fn test_or_par() {
45+
let a = true;
46+
let b = false;
47+
if (a or b) { //~ ERROR `or` is not a logical operator
48+
//~| ERROR `or` is not a logical operator
49+
println!("both");
50+
}
51+
}
52+
53+
fn test_while_and() {
54+
let a = true;
55+
let b = false;
56+
while a and b { //~ ERROR `and` is not a logical operator
57+
//~| ERROR `and` is not a logical operator
58+
println!("both");
59+
}
60+
}
61+
62+
fn test_while_or() {
63+
let a = true;
64+
let b = false;
65+
while a or b { //~ ERROR `or` is not a logical operator
66+
//~| ERROR `or` is not a logical operator
67+
println!("both");
68+
}
69+
}

0 commit comments

Comments
 (0)