forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#126187 - surechen:fix_125997, r=oli-obk
For E0277 suggest adding `Result` return type for function when using QuestionMark `?` in the body. Adding suggestions for following function in E0277. ```rust fn main() { let mut _file = File::create("foo.txt")?; } ``` to ```rust fn main() -> Result<(), Box<dyn std::error::Error>> { let mut _file = File::create("foo.txt")?; return Ok(()); } ``` According to the issue rust-lang#125997, only the code examples in the issue are targeted, but the issue covers a wider range of situations. <!-- If this PR is related to an unstable feature or an otherwise tracked effort, please link to the relevant tracking issue here. If you don't know of a related tracking issue or there are none, feel free to ignore this. This PR will get automatically assigned to a reviewer. In case you would like a specific user to review your work, you can assign it to them by using r? <reviewer name> -->
- Loading branch information
Showing
7 changed files
with
218 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
tests/ui/return/return-from-residual-sugg-issue-125997.fixed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
//@ run-rustfix | ||
|
||
#![allow(unused_imports)] | ||
#![allow(dead_code)] | ||
|
||
use std::fs::File; | ||
use std::io::prelude::*; | ||
|
||
fn test1() -> Result<(), Box<dyn std::error::Error>> { | ||
let mut _file = File::create("foo.txt")?; | ||
//~^ ERROR the `?` operator can only be used in a function | ||
|
||
Ok(()) | ||
} | ||
|
||
fn test2() -> Result<(), Box<dyn std::error::Error>> { | ||
let mut _file = File::create("foo.txt")?; | ||
//~^ ERROR the `?` operator can only be used in a function | ||
println!(); | ||
|
||
Ok(()) | ||
} | ||
|
||
macro_rules! mac { | ||
() => { | ||
fn test3() -> Result<(), Box<dyn std::error::Error>> { | ||
let mut _file = File::create("foo.txt")?; | ||
//~^ ERROR the `?` operator can only be used in a function | ||
println!(); | ||
|
||
Ok(()) | ||
} | ||
}; | ||
} | ||
|
||
fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
let mut _file = File::create("foo.txt")?; | ||
//~^ ERROR the `?` operator can only be used in a function | ||
mac!(); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//@ run-rustfix | ||
|
||
#![allow(unused_imports)] | ||
#![allow(dead_code)] | ||
|
||
use std::fs::File; | ||
use std::io::prelude::*; | ||
|
||
fn test1() { | ||
let mut _file = File::create("foo.txt")?; | ||
//~^ ERROR the `?` operator can only be used in a function | ||
} | ||
|
||
fn test2() { | ||
let mut _file = File::create("foo.txt")?; | ||
//~^ ERROR the `?` operator can only be used in a function | ||
println!(); | ||
} | ||
|
||
macro_rules! mac { | ||
() => { | ||
fn test3() { | ||
let mut _file = File::create("foo.txt")?; | ||
//~^ ERROR the `?` operator can only be used in a function | ||
println!(); | ||
} | ||
}; | ||
} | ||
|
||
fn main() { | ||
let mut _file = File::create("foo.txt")?; | ||
//~^ ERROR the `?` operator can only be used in a function | ||
mac!(); | ||
} |
86 changes: 86 additions & 0 deletions
86
tests/ui/return/return-from-residual-sugg-issue-125997.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`) | ||
--> $DIR/return-from-residual-sugg-issue-125997.rs:10:44 | ||
| | ||
LL | fn test1() { | ||
| ---------- this function should return `Result` or `Option` to accept `?` | ||
LL | let mut _file = File::create("foo.txt")?; | ||
| ^ cannot use the `?` operator in a function that returns `()` | ||
| | ||
= help: the trait `FromResidual<Result<Infallible, std::io::Error>>` is not implemented for `()` | ||
help: consider adding return type | ||
| | ||
LL ~ fn test1() -> Result<(), Box<dyn std::error::Error>> { | ||
LL | let mut _file = File::create("foo.txt")?; | ||
LL | | ||
LL + | ||
LL + Ok(()) | ||
LL + } | ||
| | ||
|
||
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`) | ||
--> $DIR/return-from-residual-sugg-issue-125997.rs:15:44 | ||
| | ||
LL | fn test2() { | ||
| ---------- this function should return `Result` or `Option` to accept `?` | ||
LL | let mut _file = File::create("foo.txt")?; | ||
| ^ cannot use the `?` operator in a function that returns `()` | ||
| | ||
= help: the trait `FromResidual<Result<Infallible, std::io::Error>>` is not implemented for `()` | ||
help: consider adding return type | ||
| | ||
LL ~ fn test2() -> Result<(), Box<dyn std::error::Error>> { | ||
LL | let mut _file = File::create("foo.txt")?; | ||
LL | | ||
LL | println!(); | ||
LL + | ||
LL + Ok(()) | ||
LL + } | ||
| | ||
|
||
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`) | ||
--> $DIR/return-from-residual-sugg-issue-125997.rs:31:44 | ||
| | ||
LL | fn main() { | ||
| --------- this function should return `Result` or `Option` to accept `?` | ||
LL | let mut _file = File::create("foo.txt")?; | ||
| ^ cannot use the `?` operator in a function that returns `()` | ||
| | ||
= help: the trait `FromResidual<Result<Infallible, std::io::Error>>` is not implemented for `()` | ||
help: consider adding return type | ||
| | ||
LL ~ fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
LL | let mut _file = File::create("foo.txt")?; | ||
LL | | ||
LL | mac!(); | ||
LL + | ||
LL + Ok(()) | ||
LL + } | ||
| | ||
|
||
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`) | ||
--> $DIR/return-from-residual-sugg-issue-125997.rs:23:52 | ||
| | ||
LL | fn test3() { | ||
| ---------- this function should return `Result` or `Option` to accept `?` | ||
LL | let mut _file = File::create("foo.txt")?; | ||
| ^ cannot use the `?` operator in a function that returns `()` | ||
... | ||
LL | mac!(); | ||
| ------ in this macro invocation | ||
| | ||
= help: the trait `FromResidual<Result<Infallible, std::io::Error>>` is not implemented for `()` | ||
= note: this error originates in the macro `mac` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
help: consider adding return type | ||
| | ||
LL ~ fn test3() -> Result<(), Box<dyn std::error::Error>> { | ||
LL | let mut _file = File::create("foo.txt")?; | ||
LL | | ||
LL | println!(); | ||
LL ~ | ||
LL + Ok(()) | ||
LL + } | ||
| | ||
|
||
error: aborting due to 4 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0277`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters