1
Fork 0

Rollup merge of #128084 - surechen:fix_125997_v1, r=cjgillot

Suggest adding Result return type for associated method in E0277.

Recommit #126515 because I messed up during rebase,

Suggest adding Result return type for associated method in E0277.

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

r? `@cjgillot`
This commit is contained in:
Trevor Gross 2024-08-18 23:41:46 -05:00 committed by GitHub
commit d21b6f2715
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 111 additions and 7 deletions

View file

@ -33,6 +33,25 @@ macro_rules! mac {
};
}
struct A;
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(())
}
fn test5(&self) -> Result<(), Box<dyn std::error::Error>> {
let mut _file = File::create("foo.txt")?;
//~^ ERROR the `?` operator can only be used in a method
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

View file

@ -27,6 +27,21 @@ macro_rules! mac {
};
}
struct A;
impl A {
fn test4(&self) {
let mut _file = File::create("foo.txt")?;
//~^ ERROR the `?` operator can only be used in a method
}
fn test5(&self) {
let mut _file = File::create("foo.txt")?;
//~^ ERROR the `?` operator can only be used in a method
println!();
}
}
fn main() {
let mut _file = File::create("foo.txt")?;
//~^ ERROR the `?` operator can only be used in a function

View file

@ -37,8 +37,47 @@ 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:31:44
--> $DIR/return-from-residual-sugg-issue-125997.rs:46:44
|
LL | fn main() {
| --------- this function should return `Result` or `Option` to accept `?`
@ -81,6 +120,6 @@ LL + Ok(())
LL + }
|
error: aborting due to 4 previous errors
error: aborting due to 6 previous errors
For more information about this error, try `rustc --explain E0277`.