Skip to content

Commit a809ec9

Browse files
committed
test: check that ? suggestion has local span
This can cause rustfix to crash because the `?` suggestion previously could point into non-local spans, such as into the stdlib.
1 parent b14d8b2 commit a809ec9

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Check that we don't construct a span for `?` suggestions that point into non-local macros
2+
// like into the stdlib where the user has no control over.
3+
//
4+
// FIXME(jieyouxu): this test is currently NOT run-rustfix because there are conflicting
5+
// MaybeIncorrect suggestions:
6+
//
7+
// 1. adding `return ... ;`, and
8+
// 2. adding `?`.
9+
//
10+
// When rustfix puts those together, the fixed file now contains uncompilable code.
11+
12+
#![crate_type = "lib"]
13+
14+
pub fn bug_report<W: std::fmt::Write>(w: &mut W) -> std::fmt::Result {
15+
if true {
16+
writeln!(w, "`;?` here ->")?;
17+
} else {
18+
writeln!(w, "but not here")
19+
//~^ ERROR mismatched types
20+
}
21+
Ok(())
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/question-mark-operator-suggestion-span.rs:18:9
3+
|
4+
LL | / if true {
5+
LL | | writeln!(w, "`;?` here ->")?;
6+
LL | | } else {
7+
LL | | writeln!(w, "but not here")
8+
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found `Result<(), Error>`
9+
LL | |
10+
LL | | }
11+
| |_____- expected this to be `()`
12+
|
13+
= note: expected unit type `()`
14+
found enum `Result<(), std::fmt::Error>`
15+
= note: this error originates in the macro `writeln` (in Nightly builds, run with -Z macro-backtrace for more info)
16+
help: consider using a semicolon here
17+
|
18+
LL | };
19+
| +
20+
help: you might have meant to return this value
21+
|
22+
LL | return writeln!(w, "but not here");
23+
| ++++++ +
24+
help: use the `?` operator to extract the `Result<(), std::fmt::Error>` value, propagating a `Result::Err` value to the caller
25+
|
26+
LL | writeln!(w, "but not here")?
27+
| +
28+
29+
error: aborting due to 1 previous error
30+
31+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)