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.
This commit is contained in:
许杰友 Jieyou Xu (Joe) 2024-04-08 21:18:51 +00:00
parent b14d8b2ef2
commit a809ec96f3
2 changed files with 53 additions and 0 deletions

View File

@ -0,0 +1,22 @@
// Check that we don't construct a span for `?` suggestions that point into non-local macros
// like into the stdlib where the user has no control over.
//
// FIXME(jieyouxu): this test is currently NOT run-rustfix because there are conflicting
// MaybeIncorrect suggestions:
//
// 1. adding `return ... ;`, and
// 2. adding `?`.
//
// When rustfix puts those together, the fixed file now contains uncompilable code.
#![crate_type = "lib"]
pub fn bug_report<W: std::fmt::Write>(w: &mut W) -> std::fmt::Result {
if true {
writeln!(w, "`;?` here ->")?;
} else {
writeln!(w, "but not here")
//~^ ERROR mismatched types
}
Ok(())
}

View File

@ -0,0 +1,31 @@
error[E0308]: mismatched types
--> $DIR/question-mark-operator-suggestion-span.rs:18:9
|
LL | / if true {
LL | | writeln!(w, "`;?` here ->")?;
LL | | } else {
LL | | writeln!(w, "but not here")
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found `Result<(), Error>`
LL | |
LL | | }
| |_____- expected this to be `()`
|
= note: expected unit type `()`
found enum `Result<(), std::fmt::Error>`
= note: this error originates in the macro `writeln` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider using a semicolon here
|
LL | };
| +
help: you might have meant to return this value
|
LL | return writeln!(w, "but not here");
| ++++++ +
help: use the `?` operator to extract the `Result<(), std::fmt::Error>` value, propagating a `Result::Err` value to the caller
|
LL | writeln!(w, "but not here")?
| +
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0308`.