b4b991e66f
For following: ```rust struct A; impl A { fn test4(&self) { let mut _file = File::create("foo.txt")?; //~^ ERROR the `?` operator can only be used in a method } ``` Suggest: ```rust impl A { fn test4(&self) -> Result<(), Box<dyn std::error::Error>> { let mut _file = File::create("foo.txt")?; //~^ ERROR the `?` operator can only be used in a method Ok(()) } } ``` For #125997
126 lines
5.0 KiB
Plaintext
126 lines
5.0 KiB
Plaintext
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 method that returns `Result` or `Option` (or another type that implements `FromResidual`)
|
|
--> $DIR/return-from-residual-sugg-issue-125997.rs:34:48
|
|
|
|
|
LL | fn test4(&self) {
|
|
| --------------- this function should return `Result` or `Option` to accept `?`
|
|
LL | let mut _file = File::create("foo.txt")?;
|
|
| ^ cannot use the `?` operator in a method that returns `()`
|
|
|
|
|
= help: the trait `FromResidual<Result<Infallible, std::io::Error>>` is not implemented for `()`
|
|
help: consider adding return type
|
|
|
|
|
LL ~ fn test4(&self) -> 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 method that returns `Result` or `Option` (or another type that implements `FromResidual`)
|
|
--> $DIR/return-from-residual-sugg-issue-125997.rs:39:48
|
|
|
|
|
LL | fn test5(&self) {
|
|
| --------------- this function should return `Result` or `Option` to accept `?`
|
|
LL | let mut _file = File::create("foo.txt")?;
|
|
| ^ cannot use the `?` operator in a method that returns `()`
|
|
|
|
|
= help: the trait `FromResidual<Result<Infallible, std::io::Error>>` is not implemented for `()`
|
|
help: consider adding return type
|
|
|
|
|
LL ~ fn test5(&self) -> 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:46: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 6 previous errors
|
|
|
|
For more information about this error, try `rustc --explain E0277`.
|